3

I'm trying to detect the white dots in the following image using OpenCV and Python.

enter image description here

I tried using the function cv2.HoughCircles but without any success.

Do I need to use a different method?

This is my code:

import cv2, cv
import numpy as np
import sys

if len(sys.argv)>1:
    filename = sys.argv[1]
else:
    filename = 'p.png'

img_gray = cv2.imread(filename,cv2.CV_LOAD_IMAGE_GRAYSCALE)

if img_gray==None:
    print "cannot open ",filename

else:
    img = cv2.GaussianBlur(img_gray, (0,0), 2)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,4,10,param1=200,param2=100,minRadius=3,maxRadius=100)
if circles:
    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.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
1
  • 2
    There are a lot of white dots around the border of the image. Can you clarify which dots you want to find ? Maybe the ones closer to the center which are not dots at all ?
    – mmgp
    Commented Feb 8, 2013 at 17:06

3 Answers 3

1

If you can reproduce a morphological reconstruction in OpenCV, you can easily build a h-dome transform which simplifies the task significantly. Otherwise, a simple threshold on a gaussian filtering might be enough too.

enter image description here

Binarize[FillingTransform[GaussianFilter[f, 2], 0.4, Padding -> 1]]

The gaussian filtering was done in the code above to effectively suppress the noise around the border of the input, which would remain after the h-dome transform otherwise.

Next there is the result of a simple threshold after a gaussian filtering (Binarize[GaussianFilter[f, 2], 0.5]) as well another result that is given by a direct binarization using Kapur's thresholding method (see the paper "A new method for gray-level picture thresholding using the entropy of the histogram" (which is no longer a new method, it is from 1985)):

enter image description here enter image description here

The right image above has a lot of small points all over the border (which cannot be seen at this image resolution), but is fully automatic. From these 3 options, only the second one is already present in OpenCV.

0

I think a median filter will improve your image. Try to experiment with some kernels, 3x3 or 7x7. Then after that some (local) thresholding algorithm will get you shapes. You can either you HoughCircles, or just find contours and check them for roundness.

0

Convert the image to binary image using a suitable threshold technique (Otsu might help). Then use morphological operations like erosion to make circles smaller and then you can easily find their centers.

4
  • Otsu will give a very bad result here, you can test it yourself or at least take a look into the image's histogram (tip: it is unimodal).
    – mmgp
    Commented Feb 8, 2013 at 17:30
  • yes, maybe you are right. i worked on something like this in the past but i cant remember which binarization i used, whether it was kittler or otsu.. Commented Feb 8, 2013 at 17:33
  • I'm not asking you to trust me, try it yourself. At i.imgur.com/w0CnlC3.png you will see the result after a binarization by Otsu.
    – mmgp
    Commented Feb 8, 2013 at 17:37
  • But Kapur's method work pretty well, and morphological operations are not needed then.
    – mmgp
    Commented Feb 8, 2013 at 17:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.