Python openCv检测出奇怪结果的圆

2024-04-25 17:40:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/#comment-480634中解释的代码,并试图基本上检测到instagram示例页面(附文)下半部分显示的小圆形轮廓图像(精确地说是5)。我不明白为什么: 1代码只捕获了5个小的圆形轮廓圆中的一个 2为什么页面上有一个大圆圈,在我看来很荒谬。 下面是我使用的代码:

# we create a copy of the original image so we can draw our detected circles 
# without destroying the original image.
image = cv2.imread("instagram_page.png")

# the cv2.HoughCircles function requires an 8-bit, single channel image, 
# so we’ll convert from the RGB color space to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# detect circles in the image. We pass in the image we want to detect circles as the first argument, 
# the circle detection method as the second argument (currently, the cv2.cv.HOUGH_GRADIENT method 
# is the only circle detection method supported by OpenCV and will likely be the only method for some time),
# an accumulator value of 1.5 as the third argument, and finally a minDist of 100 pixels.
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.7, minDist= 1, param1 = 300, param2 = 100, minRadius=3, maxRadius=150)

print("Circles len -> {}".format(len(circles)))


# ensure at least some circles were found
if circles is not None:    
    # convert the (x, y) coordinates and radius of the circles to integers
    # converting our circles from floating point (x, y) coordinates to integers, 
    # allowing us to draw them on our output image.
    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
        orange = (39, 127, 255)
        cv2.circle(output, (x, y), r, orange, 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)


img_name = "Output"
cv2.namedWindow(img_name,cv2.WINDOW_NORMAL)
cv2.resizeWindow(img_name, 800,800)
cv2.imshow(img_name, output)
cv2.waitKey(0)    
cv2.destroyAllWindows()

我使用minDist=1来确保这些紧密的圆被潜在地捕捉到。有人看到我的参数有什么问题吗?enter image description here


Tags: andofthetonameinimageimg
1条回答
网友
1楼 · 发布于 2024-04-25 17:40:39

我试了一下这些参数,成功地检测出了所有的圆(ubuntu16.04ltsx64,python3.7,numpy==1.15.1python-opencv==3.4.3):

circles = cv2.HoughCircles(
    gray,
    cv2.HOUGH_GRADIENT,
    1.7,
    minDist=100,
    param1=48,
    param2=100,
    minRadius=2,
    maxRadius=100
)

enter image description here

相关问题 更多 >