【问题标题】:openCV: detecting a circle using findContoursopenCV:使用 findContours 检测圆
【发布时间】:2018-03-20 08:20:23
【问题描述】:

我正在开发一个程序,我应该检测相同类型的形状,并用不同的颜色为每种类型着色。

我使用cv2.findCountours 然后cv2.approxPolyDP 来检测每个形状。

程序检测到任何具有 8 条边的形状为圆形,因此我决定添加一些检查 - 我正在使用 cv2.contourArea 检查当前轮廓的区域,我也在检查 cv2.minEnclosingCircle(cnt) 的区域当前轮廓的。

如果它们相等,我们就有一个圆。

import numpy as np
import cv2

img = cv2.imread('1.jpg')
gray = cv2.imread('1.jpg',0)

ret,thresh = cv2.threshold(gray,127,255,1)

contours,h = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt, .03 * cv2.arcLength(cnt, True), True)
    print len(approx)
    if len(approx)==3:
        print "triangle"
        cv2.drawContours(img,[cnt],0,(122,212,78),-1)
    elif len(approx)==4:
        print "square"
        cv2.drawContours(img,[cnt],0,(94,234,255),-1)
    elif len(approx)==8:
        area = cv2.contourArea(cnt)
        (cx, cy), radius = cv2.minEnclosingCircle(cnt)
        circleArea = radius * radius * np.pi
        print circleArea
        print area
        if circleArea == area:
            cv2.drawContours(img, [cnt], 0, (220, 152, 91), -1)


cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我打印了每个区域,发现结果不同 - 即使形状明显是圆形。

例如,对于相同的形状,minEnclosureCircle 区域为 628.254637106,contourArea 为 569。 另一个例子:minEnclosureCircle 区域为 2220.55512328,contourArea 为 2032.0。

如何正确计算这个面积?

我将不胜感激!

我使用的图像:

以及检测到的形状:

【问题讨论】:

  • 看看blob detector它已经可以按面积、圆度、凸度过滤并正确选择一个圆。
  • 斑点检测器将无法检测填充不同颜色的圆圈。它只能检测填充区域。

标签: python opencv numpy cv2


【解决方案1】:

【讨论】:

  • 谢谢。我是 python 新手,到目前为止我还不清楚这个解决方案。有没有更简单的方法来检测圆圈?
  • minEnclosureCircle 和 huMoments 的平均执行时间:huMoments:2.43 毫秒 minEnclosureCircle:13.35 毫秒
【解决方案2】:

您应该检查计数器的凸度,而不是比较区域

elif len(approx)==8:
        k=cv2.isContourConvex(approx)
        if k:
        #now you select a circle

【讨论】:

    【解决方案3】:

    您可以尝试使用属于每个 blob 的一些属性。偏心、坚固、致密等特征可以帮助您区分形状。

    您可以在下面的链接中找到一些信息(但在 C++ 中)给您一些提示

    https://github.com/mribrahim/Blob-Detection

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 2021-03-31
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多