我在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++示例:
Contour width measurement along its entire lendth
关于c++ - 像素距离取决于轮廓,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50832327/