用openCV和HoughCircles无助地迷失了

时间:2017-03-07 17:30:42

标签: python opencv

我试图检测到这个黑圈here。不应该太难,但出于某种原因,我只是得到0圈或大约500圈,取决于参数。但没有中间立场。感觉就像我试图玩了几个小时的争论,但绝对没有成功。使用HoughCircles和黑白图片有问题吗?对于人眼来说,这项任务似乎很简单,但出于某种原因这对计算机来说是否很难?

这是我的代码:

import numpy as np
import cv2

image = cv2.imread('temp.png')
output = image.copy()
blurred = cv2.blur(image,(10,10))

gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)


circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.5, 20, 100, 600, 10, 100)


if circles is not None:

    circles = np.round(circles[0, :]).astype("int")
        print len(circles)

    for (x, y, r) in circles:
        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([output]))
cv2.waitKey(0)

1 个答案:

答案 0 :(得分:1)

您的方法中存在一些小错误。

以下是我在文档中使用的代码:

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

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,255,255),3)

cv2.imshow('detected circles.jpg',cimg)

enter image description here

joint = np.hstack([cimg1, cimg])  #---Posting the original image along with the image having the detected circle
cv2.imshow('detected circle and output', joint )

enter image description here