# 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.

edit retag close merge delete

Sort by ยป oldest newest most voted

You're only looking at contour[0]. Ever.

Your outer for loop checks every contour at the top level of the hirearchy, and either disqualifies them based on area or looks further.

Your inner loop is supposed to check every point in the contour, but it only looks at contour[0].

more

I have tried to look for the contour[1] manually, but when I did that I got this error "segmentation fault", so I assumed there were only points in contour[0] for some reason. I'll try to code the inner loop as the outer one. I'll let you know, when I try (I only have access to the robot in college, and I'll be there only tomorrow morning). Thanks for your help, if you notice anything else, please notice me.

( 2017-01-03 19:36:53 -0500 )edit

I'm pretty sure you should be looking at contour[index]. Seeing as it's contour[index] that has the area needed to pass the if statement.

( 2017-01-03 20:01:18 -0500 )edit

I have tryed this, and yes the results got better, but it's still not really something I can use, as you can see in this video: https://www.youtube.com/watch?v=m-0lu.... I just changed the contour[0] to contour[index]. I really don't know what to do to make it better.

( 2017-01-04 17:48:08 -0500 )edit

Official site

GitHub

Wiki

Documentation