Chào mọi người!
Mình xin cần được giúp đỡ, mình đang làm một cái ứng dụng phát hiện làn đường nhưng bị lỗi “vector subscript out of range”. Cụ thể như sau:
- Đầu tiên, đầu vào là một vector lines được tìm thấy bằng hàm houghline trong opencv. Nó gồm tập hợp các đường thẳng biểu diễn bởi (r,theta). Mình cần convert các line này sang không gian Cartesian với vector endPoints bằng hàm sau:
// 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);
}
}
Sau đó mình tìm điểm giao nhau của 2 line này, rồi chỉ hiển thị phần từ đầu đến điểm giao nhau này (nếu tìm thấy):
///// 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;
}
Vấn đề là mình luôn bị gặp lỗi “vector subscript out of range” khi chương trình chạy tới hàm
bool laneDetection::findIntersection(vector<Point> endP, Point& pi).
Xin mọi người giúp đỡ mình! Cám ơn rất nhiều!!!