I am trying to detect all the circles in images like this.
I have many different images like this but in all the circles will be black (or almost black) and of the same size (+/- a couple of pixels). I believe there are exactly 2943 circles in each image. These conditions never change. I may not be able to control the sizes of the circles across images (usually ranges between 15-45 pixels in radii - the example image provided above has radii of 20-21 pixels).
I need to be able to detect the exact locations of the centers of these circles as accurately and precisely as possible (if possible, the radii too).
I tried using the cv2.HoughCircles
function to do so, but got very inconsistent and unreliable results. Here is the code that I used:
from pylab import *
import sys, cv2
filename = sys.argv[1];
img = cv2.imread(filename,0);
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR);
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,2,15,param1=100,param2=30,minRadius=15,maxRadius=25);
circles = np.uint16(np.around(circles));
for i in circles[0,:]:
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1);
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3);
cv2.imwrite('1-%s'%(filename),cimg)
print "%d circles found."%(len(circles[0]));
The result was this image and this output: 2806 circles found.
There are many false circles and many of the true circles have been missed/ignored.
I am starting to believe that the HoughCircle
method is not the most optimal way to go if all my circles are identical in a single image, and there might be some better object detection method available.
What do you suggest I use to detect every circle precisely and accurately across thousands of images if I can control the properties of the circles tightly enough?