繁体   English   中英

了解Python OpenCV(cv2)中的HoughCircles

[英]Understanding HoughCircles in Python OpenCV (cv2)

我正在使用此代码, 链接代码字体

import cv2
import numpy as np

img = cv2.imread('opencv_logo.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                        param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

可能是如此简单,但是有人可以帮助我理解for循环吗?

谢谢!

circle for i in circles[0,:]:中的每个i in是代表一个圆的列表。 i由三个值组成:其中心的x坐标,其中心的y坐标及其半径。

如果查看cv2.circle文档,您将看到如何使用中心和半径绘制圆。

在此处查看有关Hough Circles的工作示例的链接。 https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

我本人并不熟悉它们,但是几个学期前我确实参加了计算机视觉课程,发现该站点总体上很有帮助。

在文章中,他提供了一些带有注释的代码。 似乎circles是图像中检测到的所有circles的列表。 看来,圆是一个包含圆心坐标和半径的对象。

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)

关于您的代码,有关i值的更多详细信息,请尝试打印i以及获取类型,这将为您提供提示。

print i
print type(i)

暂无
暂无

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

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