# Revision history [back]

### Detecting small colored spheres

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

 2 updated with answer Guanta 6686 ●6 ●24 ●78

### Detecting small colored spheres

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

Update

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);

cv::inRange(imgHSV,cv::Scalar(20,100,100),cv::Scalar(30,255,255),thresholdHSV);

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.