I was trying to detect circles from a black background with red circular kind objects.
我试图用红色圆形物体检测黑色背景中的圆圈。
import cv2
import cv2.cv as cv
import numpy as np
img = cv2.imread('extracted.jpg',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint8(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()
I have loaded the image in grayscale mode,still it gives me an error
我已经以灰度模式加载了图像,但它仍然给我一个错误
"circles = np.uint8(np.around(circles))
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2277, in around
return _wrapit(a, 'round', decimals, out)
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 37, in _wrapit
result = getattr(asarray(obj),method)(*args, **kwds)
AttributeError: rint"
I cannot post the image because of my present reputation.
由于我目前的声誉,我无法发布图片。
1 个解决方案
#1
21
There is a small correction to be made in your code.
您的代码中有一个小的更正。
You are loading image in grayscale, and then again converting it to grayscale using cv2.cvtColor
which is invalid operation.
您正在以灰度加载图像,然后使用cv2.cvtColor再次将其转换为灰度,这是无效操作。
Alternatively, OpenCV provides a sample for circle detection using Hough Circles method. You can try that.
或者,OpenCV使用Hough Circles方法提供圆检测的样本。你可以试试。
If you are using OpenCV 2.x version, just change the cv2.LINE_AA
to cv2.CV_AA
or any other lineType you prefer.
如果您使用的是OpenCV 2.x版本,只需将cv2.LINE_AA更改为cv2.CV_AA或您喜欢的任何其他线型。
#1
21
There is a small correction to be made in your code.
您的代码中有一个小的更正。
You are loading image in grayscale, and then again converting it to grayscale using cv2.cvtColor
which is invalid operation.
您正在以灰度加载图像,然后使用cv2.cvtColor再次将其转换为灰度,这是无效操作。
Alternatively, OpenCV provides a sample for circle detection using Hough Circles method. You can try that.
或者,OpenCV使用Hough Circles方法提供圆检测的样本。你可以试试。
If you are using OpenCV 2.x version, just change the cv2.LINE_AA
to cv2.CV_AA
or any other lineType you prefer.
如果您使用的是OpenCV 2.x版本,只需将cv2.LINE_AA更改为cv2.CV_AA或您喜欢的任何其他线型。