我在opencv中有一个二进制图像,其中显示了一个对象的轮廓。我的老板告诉我,OpenCV中有一个功能可以测量图像所有像素和轮廓之间的最小距离,有人可以告诉我这个功能是什么吗?或者,如果有可用于对该图像执行卷积的内核?
感谢
答案 0 :(得分:2)
我认为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 ++示例:
答案 1 :(得分:0)
您可以使用pointPolygonTest。您将需要一个“for”循环来迭代图像中的所有点。