00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00039 #include "main.hh"
00040 #include "cmdline.h"
00041 #include "LaneDetector.hh"
00042 #include "main.hh"
00043 #include "cmdline.h"
00044 #include "LaneDetector.hh"
00045 #include <stdio.h>
00046 #include <fstream>
00047 #include <sstream>
00048 #include <iostream>
00049 #include <iomanip>
00050 #include <ctime>
00051
00052
00053 #include <cv.h>
00054 #include <highgui.h>
00055
00056
00057
00058 #define MSG(fmt, ...) \
00059 (fprintf(stdout, "%s:%d msg " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) ? 0 : 0)
00060
00061
00062 #define ERROR(fmt, ...) \
00063 (fprintf(stderr, "%s:%d error " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) ? -1 : -1)
00064
00065
00066 using namespace LaneDetector;
00067 using namespace std;
00068 using namespace cv;
00069
00070 class Procecess
00071 {
00072 private:
00073
00074 gengetopt_args_info options;
00075
00076 CameraInfo cameraInfo;
00077
00078 LaneDetectorConf lanesConf, stoplinesConf;
00079
00080
00081 public:
00082
00083 int Load_config(int argc, char** argv)
00084 {
00085
00086
00087 if (cmdline_parser (argc, argv, &options) < 0)
00088 return -1;
00089
00090
00091 mcvInitCameraInfo(options.camera_conf_arg,&cameraInfo);
00092 MSG("Loaded camera file");
00093
00094
00095 if (!options.no_lanes_flag)
00096 {
00097 mcvInitLaneDetectorConf(options.lanes_conf_arg,&lanesConf);
00098 MSG("Loaded lanes config file");
00099 }
00100
00101 if (!options.no_stoplines_flag)
00102 {
00103 mcvInitLaneDetectorConf(options.stoplines_conf_arg, &stoplinesConf);
00104 MSG("Loaded stop lines config file");
00105 }
00106
00107
00108 if (options.debug_flag)
00109 DEBUG_LINES = 1;
00110
00111
00112 return 1;
00113 }
00114
00115
00124 void ProcessImage(Mat &raw_mat, ofstream* outputFile,int index, clock_t *elapsedTime)
00125 {
00126
00127
00128 Mat eq_img = raw_mat;
00129 cvtColor(eq_img, eq_img, CV_BGR2GRAY);
00130
00131 if ( DEBUG_LINES == 1)
00132 imshow("grey_img",eq_img);
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 Mat mat = eq_img;
00152 CvMat clrImage = mat;
00153 CvMat cv_raw_mat = raw_mat;
00154
00155 CvMat *channelImage;
00156
00157
00158
00159
00160
00161
00162
00163
00164 channelImage = cvCreateMat(raw_mat.rows, raw_mat.cols, FLOAT_MAT_TYPE);
00165 cvConvertScale(&clrImage, channelImage, 1./255);
00166
00167
00168
00169
00170
00171
00172
00173 vector<FLOAT> lineScores, splineScores;
00174 vector<Line> lanes;
00175 vector<Spline> splines;
00176 clock_t startTime = clock();
00177
00178 mcvGetLanes(channelImage, &cv_raw_mat, &lanes, &lineScores, &splines, &splineScores,&cameraInfo, &lanesConf, NULL);
00179
00180
00181 clock_t endTime = clock();
00182 MSG("Found %d lanes in %f msec", splines.size(), static_cast<double>(endTime - startTime) / CLOCKS_PER_SEC * 1000.);
00183
00184
00185
00186 if (elapsedTime)
00187 (*elapsedTime) += endTime - startTime;
00188
00189
00190
00191 if (options.save_lanes_flag && outputFile && outputFile->is_open())
00192 {
00193 (*outputFile) << "frame#" << setw(8) << setfill('0') << index <<
00194 " has " << splines.size() << " splines" << endl;
00195 for (uint i=0; i<splines.size(); ++i)
00196 {
00197 (*outputFile) << "\tspline#" << i+1 << " has " <<splines[i].degree+1 << " points and score " << splineScores[i] << endl;
00198 for (int j=0; j<=splines[i].degree; ++j)
00199 (*outputFile) << "\t\t" <<splines[i].points[j].x << ", " <<splines[i].points[j].y << endl;
00200 }
00201 }
00202
00203
00204
00205 if (options.show_flag || options.save_images_flag)
00206 {
00207
00208
00209 CvMat *imDisplay = cvCloneMat(&cv_raw_mat);
00210
00211
00212
00213 if (lanesConf.ransacLine && !lanesConf.ransacSpline)
00214 for(uint i=0; i<lanes.size(); i++)
00215 mcvDrawLine(imDisplay, lanes[i], CV_RGB(0,125,0), 3);
00216
00217
00218 if (lanesConf.ransacSpline)
00219 {
00220 for(uint i=0; i<splines.size(); i++)
00221 {
00222 if (splines[i].color == LINE_COLOR_YELLOW)
00223 {
00224 mcvDrawSpline(imDisplay, splines[i], CV_RGB(255,255,0), 3);
00225
00226
00227 }
00228 else
00229 {
00230 mcvDrawSpline(imDisplay, splines[i], CV_RGB(0,255,0), 3);
00231
00232
00233 double x_max=0,y_max=0;
00234 double x_min = cameraInfo.imageWidth;
00235 double y_min = cameraInfo.imageHeight;
00236
00237 for (uint n=0 ; n<4 ; n++)
00238 {
00239 if (DEBUG_LINES)
00240 {
00241 cout<<"spline->"<<i<<endl;
00242 cout<<"splines.points.x--> "<<splines[i].points[n].x<<endl;
00243 cout<<"splines.points.y--> "<<splines[i].points[n].y<<endl;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 if (n==3)
00263 {
00264 if (DEBUG_LINES)
00265 {
00266 cout<<"Image size: "<<cameraInfo.imageHeight<<"x"<<cameraInfo.imageWidth<<endl;
00267 cout<<"min point x = ("<<x_min<<" , "<<y_min<<" )"<<endl;
00268 cout<<"max point x = ("<<x_max<<" , "<<y_max<<" )"<<endl;
00269 }
00271
00273
00274 if ( i==1 )
00275 {
00276 int right_splines = 0 ,left_spline = 0;
00277
00278 for (uint s = 0 ; s<splines.size() ; s++)
00279 {
00280 if ( splines[s].points[3].x <= splines[left_spline].points[3].x )
00281 left_spline = s;
00282
00283 if ( splines[s].points[3].x >= splines[right_splines].points[3].x )
00284 right_splines = s;
00285
00286 if ( DEBUG_LINES )
00287 cout<<"spline "<<s<<" X-end point ="<<splines[s].points[3].x<<endl;
00288 }
00289
00290 if ( DEBUG_LINES )
00291 {
00292 cout <<"Right splines "<<right_splines<<endl;
00293 cout <<"Left spline "<<left_spline<<endl;
00294 }
00295
00296 int npts = 12;
00297 int npt[] = { 12 };
00298
00299 Point Pts_poly[1][npts];
00300
00301
00302 for (uint pt = 0 ; pt<4 ; pt++)
00303 Pts_poly[0][pt] = Point( splines[left_spline].points[pt].x , splines[left_spline].points[pt].y );
00304
00305 if ( DEBUG_LINES )
00306 cout<<"size "<<splines.size()<<endl;
00307
00308
00309 Pts_poly[0][4] = Point( 0 , cameraInfo.imageHeight-80 );
00310 Pts_poly[0][5] = Point( 0 , cameraInfo.imageHeight-1 );
00311 Pts_poly[0][6] = Point( cameraInfo.imageWidth , cameraInfo.imageHeight );
00312 Pts_poly[0][7] = Point( cameraInfo.imageWidth , cameraInfo.imageHeight - 80);
00313
00314
00315 Pts_poly[0][8] = Point( splines[right_splines].points[3].x , splines[right_splines].points[3].y );
00316 Pts_poly[0][9] = Point( splines[right_splines].points[2].x , splines[right_splines].points[2].y );
00317 Pts_poly[0][10] = Point( splines[right_splines].points[1].x , splines[right_splines].points[1].y );
00318 Pts_poly[0][11] = Point( splines[right_splines].points[0].x , splines[right_splines].points[0].y );
00319
00320
00321 if ( splines[left_spline].points[3].x < 10 )
00322 Pts_poly[0][3] = Point( 0, splines[left_spline].points[3].y );
00323
00324 if (splines[right_splines].points[3].x > cameraInfo.imageWidth-10)
00325 Pts_poly[0][8] = Point( cameraInfo.imageWidth , splines[right_splines].points[3].y );
00326
00327 if ( DEBUG_LINES )
00328 for (uint z=0;z<npts ; z++)
00329 cout<<"Pts_poly "<<z<<"->"<<Pts_poly[0][z]<<endl;
00330
00331
00332 ofstream myfile;
00333 myfile.open ("/home/morais/Bags/Piscinas_Ilhavo/dados.txt",ios::app);
00334
00335 for (uint n=0 ; n<12 ; n++)
00336 myfile << Pts_poly[0][n].x << " " << Pts_poly[0][n].y << " ";
00337
00338 myfile << "\n";
00339 myfile.close();
00340 using namespace std;
00341
00342
00343 Mat disp = cv::Mat(imDisplay , true);
00344
00345 const Point *ppt[1]={ Pts_poly[0] };
00346
00347 fillPoly( disp, ppt, npt, 1, cvScalar( 200, 200, 200, 200),8);
00348 imshow("Poly",disp);
00349 waitKey(3);
00350 }
00351 }
00352
00353 }
00354
00355 }
00356
00357 if (options.show_lane_numbers_flag)
00358 {
00359 char str[256];
00360 sprintf(str, "%d", i);
00361 mcvDrawText(imDisplay, str,cvPointFrom32f(splines[i].points[splines[i].degree]),1, CV_RGB(0, 0, 255));
00362 }
00363 }
00364 }
00365
00366 if (options.show_flag)
00367 {
00368
00369 int wait = options.step_flag ? 0 : options.wait_arg;
00370
00371 SHOW_IMAGE(imDisplay, "Detected Lanes", wait);
00372 }
00373
00374
00375 cvReleaseMat(&imDisplay);
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 }
00387
00388
00395 bool ReadLines(const char* filename, vector<string> *lines)
00396 {
00397
00398 if (!lines)
00399 return false;
00400
00401 lines->clear();
00402
00403 ifstream file;
00404 file.open(filename, ifstream::in);
00405 char buf[5000];
00406
00407 while (file.getline(buf, 5000))
00408 {
00409 string str(buf);
00410 lines->push_back(str);
00411 }
00412
00413 file.close();
00414 return true;
00415 }
00416
00417 };