简体   繁体   English

Python-OpenCv检测产生奇怪结果的圆

[英]Python - openCv to detect circles giving strange results

I'm using the code explained in https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/#comment-480634 and trying to basically detect the small rounded profile images (to be precise 5) displayed lower half of this sample instagram page (attached). 我正在使用https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/#comment-480634中解释的代码,并试图从根本上检测出小圆角个人资料图片(精确到5)显示在此示例instagram页面的下半部分(已附加)。 What I can't figure out is why: 1. only one out of the 5 small rounded profile circles is captured by the code 2. how come there's a big circle displayed on the page and seems quite absurd to me. 我不知道为什么:1.代码捕获了5个小圆形轮廓圆圈中的一个。2.为什么页面上显示了一个大圆圈,这在我看来是很荒谬的。 Here is the code I'm using: 这是我正在使用的代码:

# 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()

I use minDist = 1 to make sure those close circles are potentially captured. 我使用minDist = 1来确保可能捕获到那些闭合的圆。 Does anybody see something completely wrong with my parameters? 有人看到我的参数完全错误吗? 在此处输入图片说明

I played around with the parameters and managed to detect all circles (Ubuntu 16.04 LTS x64, Python 3.7, numpy==1.15.1 , python-opencv==3.4.3 ): 我玩弄了参数并设法检测到所有圆圈(Ubuntu 16.04 LTS x64,Python 3.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
)

在此处输入图片说明

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

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