Help detecting/tracking circles with findContours
Hello everyone,
I am a beginner with OpenCV and need some help for my project. For my project I need to detect color based objects and track them.
The detection part is done, I am using cvtColor to convert a RGB image to an HSV, and use then the inRange function. Now, for the detections of balls, I first made use of houghCircles, but it seems to be unstable. The (x,y) coordinates of the balls were always updated, even when the ball wasn't moving. This is why the circle that I draw around the ball is always changing of place.
I decided to go for a second approach, with findContours. Here the code:
//these two vectors needed for output of findContours
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
//find contours of filtered image using openCV findContours function
findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
double refArea = 0;
bool objectFound = false;
if (hierarchy.size() > 0) {
int numObjects = hierarchy.size();
//if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
if(numObjects<MAX_NUM_OBJECTS){
for (int index = 0; index >= 0; index = hierarchy[index][0]) {
Moments moment = moments((cv::Mat)contours[index]);
double area = moment.m00;
//if the area is less than 20 px by 20px then it is probably just noise
//if the area is the same as the 3/2 of the image size, probably just a bad filter
//we only want the object with the largest area so we safe a reference area each
//iteration and compare it to the area in the next iteration.
if(area>MIN_OBJECT_AREA){
Ball yellowBall;
yellowBall.setXPos(moment.m10/area);
yellowBall.setYPos(moment.m01/area);
yellowBall.setType(yellowBall.getType());
yellowBall.setColour(yellowBall.getColour());
yBalls.push_back(yellowBall);
objectFound = true;
}else objectFound = false;
}
//let user know you found an object
if(objectFound ==true){
//draw object location on screen
drawObject(yBalls,cameraFeed);}
}else putText(cameraFeed,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);
}
It works fine! Now my question is, is it possible to get the radius of the detected objects with this code ? If not, is it possible to get houghCircles more stable ?
Thanks!
You will need to fit a houghCircle to your binary findContours output to get the radius of your blob. You can also retrieve the boundingbox directly from a contour and get parameters from that. This is not implemented for a circular ROI.
Thanks StevenPuttemans. I will try the 2 approaches to get the radius. But can you briefly explain how to do the first one with the HoughCircles and the findContours ? because in the vector Contours I only get points and not an image (so far i understood).
Is it normal that i can't see the others answers ? I see only my question and the comment of StevenPuttemans ...
Be patient, the Q&A forum has some bugs for the moment which are being solved as we speak. Someone needs to accept your answer as admin, but the page to do so is broken. Only admin messages (like mine) get displayed directly. As to the findContours operation, your image on which you create your findContours is binary right? That can also be used to create houghCircles.
Thanks for your reply! I tried the HoughCircle method but it seems to be very unstable. Example: in frame x the (x,y, radius) of the detect ball is (193, 202, 20). In the frame x+y: the (x,y, radius) of the detect ball is (195, 205, 23). In the frame x+y+z: it detects no balls at all.
This is what I mean by beeing unstable. I also make use of Morphological operations for the thresholded image but I didn't fixed it.