c++ - 取决于轮廓的像素距离
问题描述
我在 opencv 中有一个二进制图像,其中显示了对象的轮廓。我的老板告诉我,OpenCV 中有一个函数可以测量图像的所有像素与轮廓之间的最小距离,有人能告诉我这个函数是什么吗?或者,如果有一个内核可用于对该图像执行卷积?
谢谢
解决方案
我认为这是cv2.distanceTransform
计算与轮廓的距离。
distanceTransform(...)
distanceTransform(src, distanceType, maskSize[, dst[, dstType]]) -> dst
. @overload
. @param src 8-bit, single-channel (binary) source image.
. @param dst Output image with calculated distances. It is a 8-bit or 32-bit floating-point,
. single-channel image of the same size as src .
. @param distanceType Type of distance, see #DistanceTypes
. @param maskSize Size of the distance transform mask, see #DistanceTransformMasks. In case of the
. #DIST_L1 or #DIST_C distance type, the parameter is forced to 3 because a \f$3\times 3\f$ mask gives
. the same result as \f$5\times 5\f$ or any larger aperture.
. @param dstType Type of output image. It can be CV_8U or CV_32F. Type CV_8U can be used only for
. the first variant of the function and distanceType == #DIST_L1.
一个简单的例子(在python中)在这里:
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((400, 800), np.uint8)
cv2.circle(img, (300, 200), 100, (255,0,0), 3, cv2.LINE_AA)
cv2.circle(img, (500, 200), 100, (255,0,0), 3, cv2.LINE_AA)
img = 255 - img
#cv2.imshow("x", img);cv2.waitKey();cv2.destroyAllWindows()
dist = cv2.distanceTransform(img, cv2.DIST_L2, maskSize=0)
plt.subplot(211)
plt.imshow(img, cmap="gray")
plt.subplot(212)
plt.imshow(dist)
plt.show()
一个 C++ 示例在这里:
推荐阅读
- excel - Excel公式从行返回最后xth数值
- android - Intellij Idea 2018.2.5 CE Android 开发问题,android sdk v28 不稳定/渲染问题
- generics - 为什么我会收到错误“预期类型参数,找到整数变量”?
- node.js - mongodb 数据已更新,但 axios 正在获取旧数据
- testing - 不能作为不可变借用,因为在验证闭包被调用时它也被作为可变借用
- python - Python 在从 C 加载时会抛出 ModuleNotFoundError,但在直接执行时不会
- python - python Tkinter中带有系统时间的七段数字时钟与strftime
- python - 如何为日程表中的星期几名称创建动态模型?
- javascript - 数组访问时未调用 addEventListener() 方法
- python - 我的代码有效,但是如何让这段代码在确定的时间内运行?