Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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;

}