Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Help tracking objects (beginner)

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. (My objects are billiard balls from different colors) What functions should I use to track the detected balls ? findContours or HoughCircles ? And how can I track these objects (I saw some projects using moments ???) ?

I created a class Ball where I would like to store the x, y positions and also the radius.

Can someone put me on the right path ?

Thanks!

Help tracking objects (beginner)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. (My objects are billiard 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 from different colors) What functions should were always updated, even when the ball wasn't moving. This is why the circle that I use to track the detected balls ? findContours or HoughCircles ? And how can I track these objects (I saw some projects using moments ???) ? draw around the ball is always changing of place.

I created decided to go for a class 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 where I would like to store the x, y positions and also the radius.

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

Can someone put me on the right path 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!

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!