From 69df80f18a5c755851f20420e09963e56fa67ade Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 26 Nov 2012 17:08:55 +0100 Subject: verbesserungen --- detect_road_borders.cpp | 61 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/detect_road_borders.cpp b/detect_road_borders.cpp index 95c6475..5a7c31d 100644 --- a/detect_road_borders.cpp +++ b/detect_road_borders.cpp @@ -1,3 +1,5 @@ +//frame 275: da hängts!! + #include #include #include @@ -8,7 +10,7 @@ using namespace std; using namespace cv; -int find_intersection_index(const vector& pts, int x0, int y0, int x1, int y1) // bresenham aus der dt. wikipedia +int find_intersection_index(int x0, int y0, int x1, int y1, int** contour_map, bool stop_at_endpoint=true) // bresenham aus der dt. wikipedia // returns: the point's index where the intersection happened, or a negative number if no intersection. { int dx = abs(x1-x0), sx = x0& pts, int x0, int y0, int x1, in for(;;){ /* loop */ //setPixel(x0,y0); - for (int i=0; i0) return contour_map[x0][y0]; // found intersection? + if (contour_map[x0][y0+1]>0) return contour_map[x0][y0+1]; + if (contour_map[x0+1][y0]>0) return contour_map[x0+1][y0]; + + - if (x0==x1 && y0==y1) break; + if (stop_at_endpoint && x0==x1 && y0==y1) break; e2 = 2*err; if (e2 > dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */ if (e2 < dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */ @@ -145,7 +149,10 @@ int main(int argc, char* argv[]) Mat thres(frame.rows, frame.cols, CV_8UC1); Mat tmp(frame.rows, frame.cols, CV_8UC1); - + int** contour_map; + contour_map=new int*[frame.cols]; + for (int i=0;i hierarchy; findContours(thres_tmp, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0)); + /// Draw contours Mat drawing = Mat::zeros( thres_tmp.size(), CV_8UC3 ); @@ -289,7 +297,7 @@ int main(int argc, char* argv[]) for( int i = 0; i< contours.size(); i++ ) { if (hierarchy[i][3]<0) - { + { int lowy=0, lowj=-1; int highy=drawing.rows; @@ -307,6 +315,12 @@ int main(int argc, char* argv[]) if (lowj!=-1) { std::rotate(contours[i].begin(),contours[i].begin()+lowj,contours[i].end()); + + // create contour map + for (int j=0;j=0 && intersection!=bestquality_j) + int intersection = find_intersection_index(drawing.cols/2, drawing.rows-drawing.rows/5, contours[i][bestquality_j].x, contours[i][bestquality_j].y, contour_map); + if (intersection>=0) // should always be true { circle(drawing, contours[i][intersection], 2, Scalar(0,0,0)); circle(drawing, contours[i][intersection], 1, Scalar(0,0,0)); - int xx; + int xx=contours[i][bestquality_j].x; + int lastheight=-1; if (intersection < bestquality_j) // im pinken bereich, also zu weit rechts { - for (xx = contours[i][bestquality_j].x; xx>=0; xx--) + for (; xx>=0; xx--) { - int intersection2 = find_intersection_index(contours[i], drawing.cols/2, drawing.rows-drawing.rows/5, xx, contours[i][bestquality_j].y); + int intersection2 = find_intersection_index(drawing.cols/2, drawing.rows-drawing.rows/5, xx, contours[i][bestquality_j].y, contour_map); if (intersection2<0) break; - if (intersection2>=bestquality_j) + if (intersection2>=bestquality_j) // im gegenüberliegenden bereich? { - xx++; // undo last step + if (contours[i][intersection2].y>=lastheight) xx++; // undo last step break; } + lastheight=contours[i][intersection2].y; } } else if (intersection > bestquality_j) // im grünen bereich, also zu weit links { - for (xx = contours[i][bestquality_j].x; xx=lastheight) xx--; // undo last step break; } + lastheight=contours[i][intersection2].y; } } + // else // genau den horizontpunkt getroffen + // do nothing - line(drawing, Point(xx, contours[i][bestquality_j].y), Point(drawing.cols/2, drawing.rows-drawing.rows/5), Scalar(127,255,255)); + int steering_point = find_intersection_index(drawing.cols/2, drawing.rows-drawing.rows/5, xx, contours[i][bestquality_j].y, contour_map, false); + if (steering_point>=0) // should be always true + line(drawing, contours[i][steering_point], Point(drawing.cols/2, drawing.rows-drawing.rows/5), Scalar(0,255,255)); } cout << "bestquality_width="<