【问题标题】:OpenCV - Detecting circular shapesOpenCV - 检测圆形
【发布时间】:2017-05-24 12:33:56
【问题描述】:

我有一些检测圆形的代码,但我无法理解它是如何工作的。

从此代码:

  1. 如何找到圆的半径和中心点?
  2. `cv2.approxPolyDP' 检测圆的行为是什么?

现在在分段掩码中找到轮廓

contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

按照轮廓矩形 X 对轮廓进行排序

contours.sort(key = lambda x:cv2.boundingRect(x)[0])

for contour in contours:
        approx = cv2.approxPolyDP(contour, 0.01*cv2.arcLength(contour,True), True)
        if len(approx) > 8:
            # Find the bounding rect of contour.
            contour_bounding_rect = cv2.boundingRect(contour)
            mid_point = contour_bounding_rect[0] + contour_bounding_rect[2]/2, contour_bounding_rect[1] + contour_bounding_rect[3]/2
            print mid_point[1]/single_element_height, ", ",

【问题讨论】:

    标签: python opencv image-processing geometry


    【解决方案1】:

    不要认为 approxPolyDP 是正确的方法。

    如果你有一个只有圆的图像并且你想找到中心和半径,请尝试 minEnclosureCircle()

    如果您的图像具有各种形状并且您想找到圆圈,请尝试 Hough 变换(可能需要很长时间)或 fitEllipse(),检查它返回的边界框是否为正方形。

    查看这两个函数的文档

    【讨论】:

      【解决方案2】:

      所以我已经找到了您第一个问题的答案:确定图像中圆的中心和半径

      最初我发现图像中存在的所有轮廓。然后使用for loop,我为图像中的每个轮廓使用cv2.minEnclosingCircle 找到了centerradius。我在控制台屏幕上打印了它们。

      contours,hierarchy = cv2.findContours(thresh,2,1)
      print len(contours)
      cnt = contours
      
      for i in range (len(cnt)):
          (x,y),radius = cv2.minEnclosingCircle(cnt[i])
          center = (int(x),int(y))
          radius = int(radius)
          cv2.circle(img,center,radius,(0,255,0),2)
          print 'Circle' + str(i) + ': Center =' + str(center) + 'Radius =' + str(radius)
      

      回答你在cv2.approxPolyDP()上的第二个问题;此函数根据名为“epsilon”的参数在图像中的对象周围绘制近似轮廓。 'epsilon' 的值越高,轮廓就越近似。对于较低的epsilon 值,轮廓几乎掠过图像中对象的每个边缘。请访问THIS PAGE 以获得更好的理解。

      希望这有帮助! :)

      【讨论】:

        猜你喜欢
        • 2016-06-01
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多