【发布时间】:2019-01-21 11:25:45
【问题描述】:
我想在凸包下组合这张图片(阈值)上的对象:
import cv2
img = cv2.imread('test.png') #thresh1
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
220, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
# get convex hull
hull = cv2.convexHull(cnt)
cv2.drawContours(img, [hull], -1, (0, 0, 255), 1)
cv2.imwrite("output.png", img)
我可以通过控制阈值来制作一个凸包:
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 30))
threshed = cv2.morphologyEx(threshed_img, cv2.MORPH_CLOSE, rect_kernel)
cv2.imwrite('thresh2.png', threshed)
imgContours, Contours, Hierarchy = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in Contours:
hull = cv2.convexHull(cnt)
cv2.drawContours(img, [hull], -1, (0, 0, 255), 1)
cv2.imwrite('output2.png', img)
我想要的是这个:
- 有没有比操纵阈值更好的方法来组合凸包下的对象?
- 如何实现显示的凸包?
【问题讨论】:
-
首先找到单个轮廓(如在第一个图像对中),然后遍历所有找到的轮廓点,将它们聚合到一个列表中,并从这组点中检测凸包。 (如果需要,我可以稍后添加代码示例)。这是最方便的编码方式,就性能和复杂性而言,还有更好的方式。如果调整后的阈值适用于您的应用程序,请使用轮廓的层次结构并确定外部轮廓。但是,您呈现的期望结果不是凸包,因为它不是凸形状。
-
@ThomasBergmueller 是的,请您添加示例代码吗?