OPENCV Detection of close objects

1.4k Views Asked by At

I'm learning opencv and I have a question for you. I got this final processed image. objects detection

The original image is two balls that are near and they are the same color. I would get the center and the radius of each ball. If the balls are distant I do this using findContours and drawContours with option CV_FILLED, so I can do the average of the pixel position of each ball and I get the center. The problem is when the balls are close so I don't get two different contours and I can't do the average of the pixel position. I obtain this image (the center is indicated with a circle): enter image description here

Could you give some advice? I do the edge detection using Canny algorithm.

2

There are 2 best solutions below

2
On

A Hough transformation is a common approach to detect geometrical entities in the image. OpenCV has a function implemented for circles. Check this out:

http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

6
On

I wrote a simple matlab code to detect two circles:

filen='https://i.stack.imgur.com/sDNS8.jpg'  % your image
I=imread(filen);

I=im2bw(I(:,:,3)); % gray scale

Rmin=20;Rmax=70;  % circle range

% find circles in the image with Hough transform
[centersDark, radiiDark] = imfindcircles(I, [Rmin Rmax], ...
                                        'ObjectPolarity','dark','sensitivity',0.93)
                                    imagesc(I),hold on
 viscircles(centersDark, radiiDark,'LineStyle','--');hold off

Result: enter image description here

As @ChronoTrigger mentioned above, hough transform package in opencv should work for your case.