Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 控制opencv凸包_Python_Opencv - Fatal编程技术网

Python 控制opencv凸包

Python 控制opencv凸包,python,opencv,Python,Opencv,我想将此图像上的对象(阈值)合并到凸面外壳下: 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

我想将此图像上的对象(阈值)合并到凸面外壳下:

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)

我想要的是:

  • 有没有比操纵阈值更好的方法来组合凸面外壳下的对象
  • 我将如何实现凸面外壳

  • 您可以计算小零件的凸面外壳,如图所示

    然后将一些凸面外壳点添加到您创建的轮廓中。这些要点应该是:

    a) 每个凸包的最左端点(具有最低x坐标)

    b) 每个凸包的最右点(具有最高x坐标)

    c) 所有5个凸包的最高点(y坐标最低)

    d) 所有5个凸包的最低点(y坐标最高)

    您的结果轮廓将类似于


    一些点将位于轮廓之外。如果所有点都应该在轮廓内,您可以检查凸包之前计算的5个点中的所有点是否存在内部结果轮廓(使用
    pointpolyContest
    函数)。它不仅仅是将它们添加到结果轮廓。

    首先查找单个轮廓(如第一个图像对中的轮廓),然后迭代所有找到的轮廓点,将它们聚合到一个列表中,并从这组点检测凸包。(如果需要,我可以稍后添加代码示例)。这是最方便的编码方式,就性能和复杂性而言,还有更好的方式。如果调整的阈值适用于您的应用程序,请使用轮廓的层次结构并确定外部轮廓。然而,您所呈现的理想结果不是凸面外壳,因为它不是凸面形状。@ThomasBergmueller是的,请您添加一个示例代码好吗?