Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How con a find a specif point through a contour?

Hey, guys. I've been working on a project lately, I'm trying to create a line follower robot using the raspberry pi and it's camera module. I'm really new to opencv and I've been struggling to actually solve a problem I'm having. I'm able to track the black line that is placed over a white floor, using this tutorial. I was using it to track the center point of the line and it was working quite nicely, but I've recently found a problem with this aproach. It only works in straight lines, not in angled ones. So I have to chage it. In order to solve my issue, I tought I could track the two boundaries of the line (left side and right side), in the midle of the screen. The frame height is 480px, so I tought it'd points in the contour vector with y=240 and I tried to check this vector to find the x coordinates of said points. It works sometimes, but I can't find it consistently, as can be seen in this video. This is the code I'm using to track the line: double trackFilteredObject(int &x, int &y, Mat threshold, Mat &cameraFeed){ double distanceleft, distanceright, diference;

Mat temp;
//Rect bounding;
double dist;
Point teste;
threshold.copyTo(temp);
int contornox=0, contornoy=0, contornox2 = 0, contornoy2 = 0;

vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
double refArea = 0;
bool objectFound = false;
//drawContours(temp,contours,0,Scalar(0,255,0),1,8,hierarchy);
if(hierarchy.size() > 0){
    int numOjbects = hierarchy.size();

    if(numOjbects <MAX_NUM_OBJECTS){
        for(int index = 0; index >=0; index = hierarchy[index][0]){
            Moments moment = moments((Mat)contours[index]);
            double area = moment.m00;
            if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea){
                x = moment.m10/area;
                y = moment.m01/area;
                objectFound = true;
                refArea = area;
                for (int i=0; i< contours[0].size(); i++){
                                Point coordinate_i_ofcontour = contours[0][i];
                        //usleep(2000000);
                       // cout<<coordinate_i_ofcontour<<endl;
                            if(coordinate_i_ofcontour.y == FRAME_HEIGHT/2){
                                    cout<<"I'm here"<<endl;
                            if(contornoy == 0){
                            contornoy = FRAME_HEIGHT/2;
                                        contornox = coordinate_i_ofcontour.x;
                            }else{
                            contornoy2 = FRAME_HEIGHT/2;
                            contornox2 = coordinate_i_ofcontour.x;
                            }
                             }

                }
                cout<<"("<<contornox<<","<<contornoy<<endl;
            }else objectFound = false;
        }
        if(objectFound == true){

            putText(cameraFeed, "Tracking Object", Point(0,50),2,1, Scalar(0,255,0),2);
            drawLine(contornox,contornoy, cameraFeed);

            distanceleft = std::pow((pow(x-FRAME_WIDTH,2)+pow(y-(FRAME_HEIGHT/2),2)),0.5);
            distanceright = std::pow((pow(x,2)+pow(y-(FRAME_HEIGHT/2),2)),0.5);
            diference = distanceleft-distanceright;
            return(diference);
        }else if(objectFound == false){
            putText(cameraFeed, "TOO MUCH NOISE!", Point(0,50),1,2,Scalar(0,0,255),2);
            return(-10000);
        }
    }
}

}

If any of you guys could help me to find what I'm doing wrong, I'd be very happy. Thanks in advance.

click to hide/show revision 2
No.2 Revision

How con a find a specif point through a contour?

Hey, guys. I've been working on a project lately, I'm trying to create a line follower robot using the raspberry pi and it's camera module. I'm really new to opencv and I've been struggling to actually solve a problem I'm having. I'm able to track the black line that is placed over a white floor, using this tutorial. I was using it to track the center point of the line and it was working quite nicely, but I've recently found a problem with this aproach. It only works in straight lines, not in angled ones. So I have to chage it. In order to solve my issue, I tought I could track the two boundaries of the line (left side and right side), in the midle of the screen. The frame height is 480px, so I tought it'd points in the contour vector with y=240 and I tried to check this vector to find the x coordinates of said points. It works sometimes, but I can't find it consistently, as can be seen in this video. This is the code I'm using to track the line: line:

double trackFilteredObject(int &x, int &y, Mat threshold, Mat &cameraFeed){
    double distanceleft, distanceright, diference;

diference;
 Mat temp;
 //Rect bounding;
 double dist;
 Point teste;
 threshold.copyTo(temp);
 int contornox=0, contornoy=0, contornox2 = 0, contornoy2 = 0;

 vector< vector<Point> > contours;
 vector<Vec4i> hierarchy;
 findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
 double refArea = 0;
 bool objectFound = false;
 //drawContours(temp,contours,0,Scalar(0,255,0),1,8,hierarchy);
 if(hierarchy.size() > 0){
     int numOjbects = hierarchy.size();

     if(numOjbects <MAX_NUM_OBJECTS){
         for(int index = 0; index >=0; index = hierarchy[index][0]){
             Moments moment = moments((Mat)contours[index]);
             double area = moment.m00;
             if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea){
                 x = moment.m10/area;
                 y = moment.m01/area;
                 objectFound = true;
                 refArea = area;
                 for (int i=0; i< contours[0].size(); i++){
                                 Point coordinate_i_ofcontour = contours[0][i];
                         //usleep(2000000);
                        // cout<<coordinate_i_ofcontour<<endl;
                             if(coordinate_i_ofcontour.y == FRAME_HEIGHT/2){
                                     cout<<"I'm here"<<endl;
                             if(contornoy == 0){
                             contornoy = FRAME_HEIGHT/2;
                                         contornox = coordinate_i_ofcontour.x;
                             }else{
                             contornoy2 = FRAME_HEIGHT/2;
                             contornox2 = coordinate_i_ofcontour.x;
                             }
                              }

                 }
                 cout<<"("<<contornox<<","<<contornoy<<endl;
             }else objectFound = false;
         }
         if(objectFound == true){

             putText(cameraFeed, "Tracking Object", Point(0,50),2,1, Scalar(0,255,0),2);
             drawLine(contornox,contornoy, cameraFeed);

             distanceleft = std::pow((pow(x-FRAME_WIDTH,2)+pow(y-(FRAME_HEIGHT/2),2)),0.5);
             distanceright = std::pow((pow(x,2)+pow(y-(FRAME_HEIGHT/2),2)),0.5);
             diference = distanceleft-distanceright;
             return(diference);
         }else if(objectFound == false){
             putText(cameraFeed, "TOO MUCH NOISE!", Point(0,50),1,2,Scalar(0,0,255),2);
             return(-10000);
         }
     }
    }

}

}

If any of you guys could help me to find what I'm doing wrong, I'd be very happy. Thanks in advance.