【问题标题】:Fit Quadrilateral (Tetragon) to a blob将四边形(四边形)拟合到 blob
【发布时间】:2017-04-29 12:16:48
【问题描述】:

应用不同的过滤和分割技术后,我最终得到了这样的图像:

我可以访问一些轮廓检测函数,这些函数返回该对象边缘上的点列表,或者返回一个拟合的多边形(虽然有很多边,但远不止 4 个)。 我想要一种使四边形适合该形状的方法,因为我知道它是应该是四边形的鞋盒的正面。由于透视图,平行度不守恒,所以我现在没有限制,只需要包含这个框的四个线段。

到目前为止我能找到的只是矩形拟合,它并没有真正返回我需要的结果,因为它强制拟合的四边形为矩形。

如果我可以访问相机与鞋盒的相对角度,并且知道鞋盒与相机的距离,我可以生成一个单应矩阵并扭曲图像,使鞋盒再次显示为矩形,但现在我无法访问此类信息,并且希望仅基于愿景来进行。

任何已知的解决此类问题的方法?

【问题讨论】:

    标签: opencv image-processing computer-vision model-fitting


    【解决方案1】:

    我推荐以下步骤:

    1. threshold()图片
    2. dilate() the image - 这将删除分割顶部和底部的黑线以及下部较暗的伪影
    3. findContours() 使用设置仅检索外部轮廓(RETR_EXTERNAL)并简化输出(CHAIN_APPROX_SIMPLE
    4. 进一步处理轮廓

    第 1 步:阈值

    # threshold image
    ret,thresh = cv2.threshold(img,127,255,0)
    cv2.imshow('threshold ',thresh)
    

    第 2 步:扩张

    # dilate thresholded image - merges top/bottom 
    kernel = np.ones((3,3), np.uint8)
    dilated = cv2.dilate(thresh, kernel, iterations=3)
    cv2.imshow('threshold dilated',dilated)
    

    第 3 步:寻找轮廓

    # find contours
    contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(img, contours, 0, (255,255,255), 3)
    print "contours:",len(contours)
    print "largest contour has ",len(contours[0]),"points"
    

    请注意,首先扩张,然后使用简单的外部轮廓可以得到你想要的形状,但它仍然非常复杂(包含 279 个点)

    从现在开始,您可以继续process the contour features。 有几个选项可用,例如:

    a:得到最小值。区域矩形

    # minAreaRect
    rect = cv2.minAreaRect(contours[0])
    box = cv2.cv.BoxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img,[box],0,(255,255,255),3)
    

    可能有用,但不完全是您所需要的。

    b:凸包

    # convexHull
    hull = cv2.convexHull(contours[0])
    cv2.drawContours(img, [hull], 0, (255,255,255), 3)
    print "convex hull has ",len(hull),"points"
    

    更好,但你还有 22 点要处理,而且还不是很紧

    c:简化轮廓

    # simplify contours
    
    epsilon = 0.1*cv2.arcLength(contours[0],True)
    approx = cv2.approxPolyDP(contours[0],epsilon,True)
    cv2.drawContours(img, [approx], 0, (255,255,255), 3)
    print "simplified contour has",len(approx),"points"
    

    这可能就是您所追求的:只需 4 分。 如果您需要更多积分,可以使用 epsilon 值。

    记住,现在你有一个四边形,但图片是扁平的:没有关于透视/3d 旋转的信息。

    完整的OpenCV Python代码清单(根据需要注释/取消注释,使用参考适应c++/java/等):

    import numpy as np
    import cv2
    
    img = cv2.imread('XwzWQ.png',0)
    
    # threshold image
    ret,thresh = cv2.threshold(img,127,255,0)
    cv2.imshow('threshold ',thresh)
    
    # dilate thresholded image - merges top/bottom 
    kernel = np.ones((3,3), np.uint8)
    dilated = cv2.dilate(thresh, kernel, iterations=3)
    cv2.imshow('threshold dilated',dilated)
    
    # find contours
    contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    # cv2.drawContours(img, contours, 0, (255,255,255), 3)
    print "contours:",len(contours)
    print "largest contour has ",len(contours[0]),"points"
    
    # minAreaRect
    # rect = cv2.minAreaRect(contours[0])
    # box = cv2.cv.BoxPoints(rect)
    # box = np.int0(box)
    # cv2.drawContours(img,[box],0,(255,255,255),3)
    
    # convexHull
    # hull = cv2.convexHull(contours[0])
    # cv2.drawContours(img, [hull], 0, (255,255,255), 3)
    # print "convex hull has ",len(hull),"points"
    
    # simplify contours
    epsilon = 0.1*cv2.arcLength(contours[0],True)
    approx = cv2.approxPolyDP(contours[0],epsilon,True)
    cv2.drawContours(img, [approx], 0, (255,255,255), 3)
    print "simplified contour has",len(approx),"points"
    
    
    # display output 
    cv2.imshow('image',img)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • 这是一个很好的答案!非常感谢!这就是我要找的!
    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多