7

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.

8
  • plz ignore the indentation errors Commented Feb 20, 2014 at 6:09
  • try this code : github.com/Itseez/opencv/blob/master/samples/python2/… Commented Feb 20, 2014 at 6:48
  • Abid thanks, I tried the code but it gives me cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv.LINE_AA) AttributeError: 'module' object has no attribute 'LINE_AA' Commented Feb 20, 2014 at 9:20
  • 1
    Just remove that cv2.LINE_AA. Commented Feb 20, 2014 at 9:26
  • thanks a lot Abid, it worked. How can I set the minimum and maximum radius to be detected? means which parameter indicates it? Commented Feb 20, 2014 at 9:32

1 Answer 1

26

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.

Alternatively, OpenCV provides a sample for circle detection using Hough Circles method. You can try that.

If you are using OpenCV 2.x version, just change the cv2.LINE_AA to cv2.CV_AA or any other lineType you prefer.

Sign up to request clarification or add additional context in comments.

1 Comment

The link you posted is dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.