From bd758519c960f0f69a2a4f0488b4b5ef19f1b998 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Wed, 28 Nov 2012 23:34:34 +0100 Subject: =?UTF-8?q?detect=5Froad=5Fborders=20etwas=20versch=C3=B6nert=20(W?= =?UTF-8?q?IP)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- detect_road_borders.cpp | 221 +++++++++++++++++++++++++++--------------------- mariokart01.cpp | 3 +- 2 files changed, 125 insertions(+), 99 deletions(-) diff --git a/detect_road_borders.cpp b/detect_road_borders.cpp index 5a7c31d..33d3d2f 100644 --- a/detect_road_borders.cpp +++ b/detect_road_borders.cpp @@ -124,86 +124,72 @@ double linear(double x, double x1, double y1, double x2, double y2, bool clip=fa } -#define AREA_HISTORY 10 -int alertcnt=21; -int main(int argc, char* argv[]) +int annotate_regions(Mat img) //img is treated as black/white (0, !=0) +// changes img, and returns the number of found areas { - if (argc!=2) {printf("usage: %s videofile\n",argv[0]); exit(1);} - VideoCapture capture(argv[1]); - - if (!capture.isOpened()) + int region_index=1; // "0" means "no area" + for (int row = 0; row(row); + + for (int col=0; col> frame; - - + return region_index-1; +} +Mat nicely_draw_regions(Mat annotated, int* area_cnt, int total_area_cnt, int largest_region) +{ + Mat result; + annotated.copyTo(result); - - 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> frame; - - if (frameno<190) - { - frameno++; - continue; - } - - cvtColor(frame, tmp, CV_RGB2GRAY); - threshold(tmp, thres, 132, 255, THRESH_BINARY); - dilate(thres,tmp,Mat()); - erode(tmp,thres,Mat()); - erode(thres,tmp,Mat()); - dilate(tmp,thres,Mat()); - - int area_index=1; // "0" means "no area" - for (int row = 0; row(row); + uchar* data=result.ptr(row); - for (int col=0; col200) tmp=200; + if (*data==largest_region) tmp=255; + *data=tmp; } + + data++; } + } +} + +double only_retain_largest_region(Mat img, int* size) +// img is a binary image +// in *size, if non-NULL, the size of the largest area is stored. +// returns: ratio between the second-largest and largest region +// 0.0 means "that's the only region", 1.0 means "both had the same size!" +// can be interpreted as 1.0 - "confidence". +{ + int n_regions = annotate_regions(img); - - int* area_cnt = new int[area_index-1]; + // calculate the area of each region + int* area_cnt = new int[n_regions]; + for (int i=0;i(row); + uchar* data=img.ptr(row); - for (int col=0; col(row); - - for (int col=0; col200) tmp=255; - *data=tmp; - } - - data++; - } - } - */ // finde die größte und zweitgrößte fläche int maxi=0, maxa=area_cnt[0], maxi2=-1; - for (int i=1;imaxa) { @@ -251,11 +218,11 @@ int main(int argc, char* argv[]) // lösche alle bis auf die größte fläche - for (int row = 0; row(row); + uchar* data=img.ptr(row); - for (int col=0; col> frame; + + + + + + 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> frame; + + if (frameno<190) + { + frameno++; + continue; + } + + cvtColor(frame, tmp, CV_RGB2GRAY); + threshold(tmp, thres, 132, 255, THRESH_BINARY); + dilate(thres,tmp,Mat()); + erode(tmp,thres,Mat()); + erode(thres,tmp,Mat()); + dilate(tmp,thres,Mat()); + + int area_abs; + double area_ratio = only_retain_largest_region(thres, &area_abs); dilate(thres, tmp, erode_kernel); @@ -273,9 +301,8 @@ int main(int argc, char* argv[]) - Mat thres_tmp, thres_tmp_; - thres.copyTo(thres_tmp); - //thres_tmp=thres_tmp_.rowRange(0, 0.6*thres_tmp_.rows ); + Mat thres_tmp; + thres.copyTo(thres_tmp); // this is needed because findContours destroys its input. vector > contours; vector hierarchy; @@ -521,21 +548,21 @@ int main(int argc, char* argv[]) area_history_sum-=area_history[area_history_ptr]; - area_history[area_history_ptr]=area_cnt[maxi]; - area_history_sum+=area_cnt[maxi]; + area_history[area_history_ptr]=area_abs; + area_history_sum+=area_abs; area_history_ptr=(area_history_ptr+1)%AREA_HISTORY; int prev_area=area_history_sum/AREA_HISTORY; - cout << "\r\e[2A area = "<0.1) { cout << "\nALERT: possibly split road!\n\n\n" << flush; alertcnt=0; } - if (abs(100*area_cnt[maxi]/prev_area -100) >=10) + if (abs(100*area_abs/prev_area -100) >=10) { cout << "\nALERT: too fast road area change!\n\n\n" << flush; alertcnt=0; diff --git a/mariokart01.cpp b/mariokart01.cpp index 955bbe7..3e4d98b 100644 --- a/mariokart01.cpp +++ b/mariokart01.cpp @@ -697,8 +697,7 @@ try { joystick.reset(); getchar(); joystick.reset(); -joystick.press_a(true); -getchar(); + #ifdef LINUX XorgGrabber capture("glN64"); #endif -- cgit v1.2.1