Ask Your Question

Detecting small colored spheres

asked 2013-04-16 04:34:27 -0500

Zimon gravatar image

updated 2013-04-18 12:21:24 -0500

Guanta gravatar image

Hi guys,

I recently started with openCV and since this is a very powerful library I was wondering if someone can give me some advice in which direction I should be heading. I want to detect small colored spheres like marbles and I was wondering which combination of algorithms are best suited for this purpose. Since this should run on a mobile device I have also to take the performance into account and that the camera won't be static.

Any help is highly appreciated


I did not completely solve this problem but since I made some progress I figured I post it here as an answer.

For the first testcase I try to detect a yellow marble using an iPhone 4S. I am using HSV and findContours which already gave me some great results.Later on I am also deleting some noise by iterating over the contour vector<vector> and erasing contours with big or very small size().

cv::Mat thresholdHSV;
cv::Mat imgHSV;
cv::Mat fu;

cv::cvtColor(inputFrame, imgHSV, CV_BGR2HSV);


std::vector<std::vector<cv::Point> > contours;

findContours(thresholdHSV.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
 //Draw them
cv::Mat dst = cv::Mat::zeros(inputFrame.size(), inputFrame.type());
drawContours(dst, contours, -1, cv::Scalar(255,0,0), CV_FILLED);

However sometimes there is still noise which gets displayed so I figured since the image is so much reduced and the marble is a perfect circle, HoughCircle can give me the best result. Unfortunately I can't just use dst(from the above code) for an input for HoughCircle (not 8bit single channel).I also tried this with no matches:

HoughCircles(thresholdHSV, detectedCircles, CV_HOUGH_GRADIENT, 1, thresholdHSV.rows / 8, 200, 100, 0, 0);

I also tried to smooth the image with no result. Since the image is so much reduced, I thought there must be away to use HoughCircles on it in a performant way.

Another thought was using

void minEnclosingCircle(InputArray points, Point2f& center, float& radius)

and somehow comparing the enclosing circle to the contours,(but thats actually what this is doing so...)

I really hope you guys can help me and thanks for your time.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-04-16 05:47:34 -0500

Guanta gravatar image

updated 2013-04-18 12:23:13 -0500

One idea: First etect circular objects via Hough-Trransform, then check each circle if it looks like your object you are searching for.

Two other posts which might give you some ideas:


Afaik you can just define dst as a one-column matrix and draw in that, then use it for the input to HoughCircles.

edit flag offensive delete link more


thank you for the quick reply. I am wondering if the Hough circle Transform would work given the perspective of the camera. When the camera is nearly on the same plane as the sphere, the sphere might look more like an ellipse than a circle. I hope my explanation make sense.

Zimon gravatar imageZimon ( 2013-04-16 09:03:38 -0500 )edit

Makes totally sense, unfortunately, in OpenCV no ellipse-detection is implemented. So you'd need to code it yourself. Maybe this good paper helps you: Ahjua et al.: "A real-time ellipse detection based on edge grouping" -

Guanta gravatar imageGuanta ( 2013-04-16 19:02:45 -0500 )edit

thank you for that hint.However I want to try to solve this problem using the given OpenCV algorithms first. I used this approach to implement the hough circle transform: However I get no results at all. I even draw a black circle on a white paper which also does not get detected. I was wondering if I also could use cv::Cranny and cv::findContours since this correctly draws the countour around the colored spheres.(ofcourse everything else is been drawn swell)

Zimon gravatar imageZimon ( 2013-04-18 06:04:44 -0500 )edit

I can't figure out how to define a one collumn matrix to use it with HoughCircles. I tried adding CV_8UC1 as type instead of the input.type() but this did not work.

Zimon gravatar imageZimon ( 2013-04-19 08:05:12 -0500 )edit

Args, I didn't mean one column but one channel, sry for the confusion! Weired that CV_8UC1 doesn't work, have you looked at the result after drawContours?

Guanta gravatar imageGuanta ( 2013-04-19 08:34:54 -0500 )edit

When I just replace input.type() with CV_8UC1 I get an OpenCV error "OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') "

Zimon gravatar imageZimon ( 2013-04-19 09:24:44 -0500 )edit

Please try: cv::Mat1b dst = cv::Mat1b::zeros(inputFrame.rows, inputFrame.cols); drawContours(dst, contours, -1, 255, CV_FILLED);

Guanta gravatar imageGuanta ( 2013-04-19 09:33:01 -0500 )edit

this worked somehow but I dont get any detected circles from HoughCircles. I also can't copy my destinationsource to the outputframe. When I do this a BAD_ACCESS exception is been thrown.

Zimon gravatar imageZimon ( 2013-04-19 09:51:51 -0500 )edit

HoughCircles won't work if you use CV_FILLED, use '1' instead, to just draw the contours.

Guanta gravatar imageGuanta ( 2013-04-19 09:58:09 -0500 )edit

Unfortunately no results when I replaced CV_FILLED with 1.

Zimon gravatar imageZimon ( 2013-04-19 10:08:02 -0500 )edit

Question Tools

1 follower


Asked: 2013-04-16 04:34:27 -0500

Seen: 1,855 times

Last updated: Apr 18 '13