简体   繁体   English

OpenCV-检测圆形

[英]OpenCV - Detecting circular shapes

I have some code which detects circular shapes but I am unable to understand how it works. 我有一些代码可以检测圆形,但是我无法理解它是如何工作的。

From this code: 从此代码:

  1. How can i find the radius and center point of the circle? 如何找到圆的半径和中心点?
  2. What is the behaviour of `cv2.approxPolyDP' for detecting circles? “ cv2.approxPolyDP”检测圆的行为是什么?

Now find the contours in the segmented mask 现在在分割蒙版中找到轮廓

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

Sorting the contours wrt contour rect X 轮廓轮廓与轮廓矩形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, ", ",

Don't think approxPolyDP is the right way to go here. 不要以为roximatePolyDP是正确的选择。

If you have an image where you only have circles and you want to find center and radius, try minEnclosingCircle() 如果您的图像中只有圆圈,并且想查找中心和半径,请尝试使用minEnclosingCircle()

If you have an image where you have various shapes and you want to find the circles, try Hough transform (may take a long time) or fitEllipse() where you check if the bounding box it returns is square. 如果您有一个具有各种形状的图像并且想要找到圆,请尝试进行Hough变换(可能需要花费很长时间)或fitEllipse(),在此检查返回的边界框是否为正方形。

See documentation for both these functions 请参阅有关这两个功能的文档

So I have figured out the answer to your first question: determining the center and radius of circles in the image . 因此,我已经找到了第一个问题的答案: 确定图像中圆的中心和半径

Initially I am finding all the contours present in the image. 最初,我发现图像中存在所有轮廓。 Then using a for loop , I found the center and radius using cv2.minEnclosingCircle for every contour in the image. 然后使用for loop ,使用cv2.minEnclosingCircle找到图像中每个轮廓的中心半径 I printed them in the console screen. 我在控制台屏幕上打印了它们。

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)

To answer your second question on cv2.approxPolyDP() ; 回答关于cv2.approxPolyDP()第二个问题; this function draws an approximate contour around the object in the image based on a parameter called 'epsilon'. 此函数基于称为“ε”的参数在图像中的对象周围绘制近似轮廓。 Higher the value of 'epsilon', the contour is roughly approximated. “ε”的值越高,轮廓就越近似。 For a lower value of epsilon , the contour grazes almost every edge of the object in the image. 对于较低的epsilon值,轮廓几乎会掠过图像中对象的每个边缘。 Visit THIS PAGE for a better understanding. 访问此页面以获得更好的理解。

Hope this helped!! 希望这有所帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM