【问题标题】:Differentiating a trapezoid from squares and rectangles将梯形与正方形和矩形区分开来
【发布时间】:2020-06-07 02:07:14
【问题描述】:

我正在尝试开发一种算法来检测梯形形状。我正在尝试找出一种方法来检测梯形的倒角以区分正方形和梯形。

我只是简单地遍历轮廓,并尝试通过其他特征来区分梯形,因为我不确定如何检查梯形上的线条是否反转。有什么想法吗?

以下是我正在跟踪的示例:

这是我目前想出的:

for cnt in contourList:
        hull = cv2.convexHull(cnt)
        x, y, w, h = cv2.boundingRect(cnt)
        area = h * w * 1.0
        originalArea = cv2.contourArea(hull)

        #skip if not minimum area
        if(originalArea < 200 ):
            continue #too small!

        kMinTargetWidth = 20
        kMaxTargetWidth = 300
        kMinTargetHeight = 6
        kMaxTargetHeight = 60
        if(w < kMinTargetWidth or w  > kMaxTargetWidth):
            continue
        if(h < kMinTargetHeight or h > kMaxTargetHeight):
            continue


        #Ratio of width to height
        #ratio should be 1 or greater
        #target is 43 cm tall, 100 cm wide
        ratio = float(w) / float(h)
        if(ratio < 1.3):
            continue #skip

        kMinFullness = .50
        kMaxFullness = .95
        fullness = originalArea / area
        if(fullness < kMinFullness or fullness > kMaxFullness):
            continue

        if(cv2.isContourConvex(cnt) == True):
            continue

我希望这些梯形物体的凹面性质能够与矩形/正方形区分开来。我现在想我也应该以某种方式检查侧角。

或者取凸包并找到角点。但我觉得 OpenCV 已经内置了类似的东西?基本上我在问我错过了什么!

【问题讨论】:

  • 参见docs.opencv.org/4.1.1/d3/dc0/…geeksforgeeks.org/… 的cv2.approxPolyDP()。找到轮廓,近似于四边形。获取顶点。画线。检查角度和坡度。梯形是只有一对平行边的四边形。一个矩形将有 90 度角和两对平行边。
  • 在您的示例图片中,哪些对象是梯形,哪些不是? (如果你能描述它们会有所帮助,并添加更多示例)

标签: python opencv


【解决方案1】:

如果您定义contourListcv2,并确定您导入的包,那么我的建议可能会更有针对性。

仅使用梯形,我建议使用梯形功能:一组相反的线是平行的,而另一组不平行。这需要知道四条线的斜率。如果你不直接知道那些斜率,但你知道顶点,那么你可以计算四个斜率。

基本上,使用梯形的定义是可靠的,并且有多种方法可以确定平行和非平行。

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 2016-08-23
    • 2014-05-20
    • 2011-06-24
    相关资源
    最近更新 更多