Ask Your Question
0

vector subscript out of range when finding the intersection point of two lines

asked 2015-05-13 22:33:23 -0600

duc.d.k gravatar image

Hi all, I alway see the above error when running findIntersection function, bool laneDetection::findIntersection(vector<point> endP, Point& pi). Please see to my code and give me your advances. I appreciated for your helps!

// Draw Lines on the image

Mat laneDetection::drawLines(Mat img, vector<vec2f> lines, string name){

Mat imgRGB;
cvtColor(img,imgRGB,CV_GRAY2RGB); // converting the image to RGB for display
vector<Point> endPoints;

// Here, I convert the polar coordinates to Cartesian coordinates.
// Then, I extend the line to meet the boundary of the image.
for (int i = 0;i < lines.size();i++){
    float r = lines[i][0];
    float t = lines[i][1];

    float x = r*cos(t);
    float y = r*sin(t);

    Point p1(cvRound(x - 1.0*sin(t)*1000), cvRound(y + cos(t)*1000));
    Point p2(cvRound(x + 1.0*sin(t)*1000), cvRound(y - cos(t)*1000));

    clipLine(img.size(),p1,p2);
    if (p1.y > p2.y){
        endPoints.push_back(p1);
        endPoints.push_back(p2);
    }
    else{
        endPoints.push_back(p2);
        endPoints.push_back(p1);
    }

}

///// Finding the intersection point of two lines to plot only lane markings till the intersection
Point pint;
bool check = findIntersection(endPoints,pint);

if (check){
    line(imgRGB,endPoints[0],pint,Scalar(0,255,255),5);
    line(imgRGB,endPoints[2],pint,Scalar(0,255,255),5);
}   
/////

// Saving to intercepts.csv
float xIntercept = min(endPoints[0].x,endPoints[2].x);
myfile << name << "," << xIntercept * 2 << "," << pint.x * 2 << endl;

visualize(imgRGB); // Visualize the final result

return imgRGB;

}

// Finding the Vanishing Point bool laneDetection::findIntersection(vector<point> endP, Point& pi){

Point x = endP[2] - endP[0];
Point d1 = endP[1] - endP[0];
Point d2 = endP[3] - endP[2];

float cross = d1.x*d2.y - d1.y*d2.x;
if (abs(cross) < 1e-8) // No intersection
    return false;

double t1 = (x.x * d2.y - x.y * d2.x)/cross;
pi = endP[0] + d1 * t1;
return true;

}

edit retag flag offensive close merge delete

Comments

you probably need a check, if it found any lines at all, before assuming, you got at least 4 endpoints

berak gravatar imageberak ( 2015-05-14 01:51:08 -0600 )edit

Hi berak, Lines was found by the houghline function as before and these are converted the polar coordinates to Cartesian coordinates by vector<point> endPoints. We had 3 points (x,d1,d2) to finding the Vanishing Point. Could you please give your advance more detail? What the change that I need to do?

duc.d.k gravatar imageduc.d.k ( 2015-05-14 03:31:01 -0600 )edit

tbh, i do not understand all of your code, but findIntersection() assumes, you have at least 4 points in your endp vector (so you need to find at least 2 lines before)

berak gravatar imageberak ( 2015-05-14 03:48:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-05-14 04:02:29 -0600

duc.d.k gravatar image

updated 2015-05-14 04:06:06 -0600

oh, the input line has lines.size =2. So vector<point> endP has 4 points (endP[0], endP[1], endP[2],endP[3]) as your comment. You can refer the code at https://github.com/NamanCMU/Lane-Mark.... I use it for my lane tracking project.</point>

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-05-13 22:33:23 -0600

Seen: 375 times

Last updated: May 13 '15