ProcessImage.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
39 #include "main.hh"
40 #include "cmdline.h"
41 #include "LaneDetector.hh"
42 #include "main.hh"
43 #include "cmdline.h"
44 #include "LaneDetector.hh"
45 #include <stdio.h>
46 #include <fstream>
47 #include <sstream>
48 #include <iostream>
49 #include <iomanip>
50 #include <ctime>
51 
52 // includes do opencv antigo
53 #include <cv.h>
54 #include <highgui.h>
55 // #include <opencv2/imgproc/imgproc.hpp>
56 // #include <opencv2/highgui/highgui.hpp>
57 // Useful message macro
58 #define MSG(fmt, ...) \
59 (fprintf(stdout, "%s:%d msg " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) ? 0 : 0)
60 
61 // Useful error macro
62 #define ERROR(fmt, ...) \
63 (fprintf(stderr, "%s:%d error " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) ? -1 : -1)
64 // int DEBUG_LINES = 1;
65 
66 using namespace LaneDetector;
67 using namespace std;
68 using namespace cv;
69 
70 class Procecess
71 {
72  private:
73  // parse the command line paramters
75  // read the camera configurations
77  // read the configurations
79 
80 
81  public:
82  // Load the configuration files to global variables
83  int Load_config(int argc, char** argv)
84  {
85  // parse the command line paramters
86 // gengetopt_args_info options_local;
87  if (cmdline_parser (argc, argv, &options) < 0)
88  return -1;
89 
90  // read the camera configurations
91  mcvInitCameraInfo(options.camera_conf_arg,&cameraInfo);
92  MSG("Loaded camera file");
93 
94  // read the configurations
95  if (!options.no_lanes_flag)
96  {
97  mcvInitLaneDetectorConf(options.lanes_conf_arg,&lanesConf);
98  MSG("Loaded lanes config file");
99  }
100 
101  if (!options.no_stoplines_flag)
102  {
103  mcvInitLaneDetectorConf(options.stoplines_conf_arg, &stoplinesConf);
104  MSG("Loaded stop lines config file");
105  }
106 
107  // set debug to true
108  if (options.debug_flag)
109  DEBUG_LINES = 1;
110 
111 
112  return 1;
113  }
114 
115 
124  void ProcessImage(Mat &raw_mat, ofstream* outputFile,int index, clock_t *elapsedTime)
125  {
126 
127 
128  Mat eq_img = raw_mat;
129  cvtColor(eq_img, eq_img, CV_BGR2GRAY);
130 
131  if ( DEBUG_LINES == 1)
132  imshow("grey_img",eq_img);
133 
134 // // // // // // Fazer o pré-processamento da imagem em C++
135  /* Equalização de histograma */
136  equalizeHist( eq_img , eq_img );
137  if ( DEBUG_LINES == 1)
138  imshow("eq_img",eq_img);
139 
140  // Since MORPH_X : 2,3,4,5 and 6
141  int operation = 2;
142 
143  Mat element = getStructuringElement( 0, Size( 2*0 + 1, 2*0+1 ), Point( 0, 0 ) );
144 // morphologyEx(eq_img,eq_img,operation,element);
145 
146 
147  /* TopHat */
148  if ( DEBUG_LINES == 1)
149  imshow("top_hat",eq_img);
150 
151 
152  // convert cv::Mat to cvMat
153  Mat mat = eq_img;
154  CvMat clrImage = mat;
155  CvMat cv_raw_mat = raw_mat;
156  // convert to mat and get first channel
157  CvMat *channelImage;
158 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
159 
160  // convert to single channel
161 // CvMat* tchannelImage = cvCreateMat(raw_mat.rows, raw_mat.cols, INT_MAT_TYPE);
162 // cvSplit(raw_mat, tchannelImage, NULL, NULL, NULL);
163 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
164 
165  // convert to float
166  channelImage = cvCreateMat(raw_mat.rows, raw_mat.cols, FLOAT_MAT_TYPE);
167  cvConvertScale(&clrImage, channelImage, 1./255);
168 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
169 
170  // destroy
171 // cvReleaseMat(&tchannelImage);
172 
173 // cout<<"Enter - ProcessImage"<<endl;
174  // detect lanes
175  vector<FLOAT> lineScores, splineScores;
176  vector<Line> lanes;
177  vector<Spline> splines;
178  clock_t startTime = clock();
179 
180  mcvGetLanes(channelImage, &cv_raw_mat, &lanes, &lineScores, &splines, &splineScores,&cameraInfo, &lanesConf, NULL);
181 // cout<<"-> Saiu do mcvGetLanes"<<endl;
182 
183  clock_t endTime = clock();
184  MSG("Found %d lanes in %f msec", splines.size(), static_cast<double>(endTime - startTime) / CLOCKS_PER_SEC * 1000.);
185 
186 
187  // update elapsed time
188  if (elapsedTime)
189  (*elapsedTime) += endTime - startTime;
190 
191 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
192  // save results?
193  if (options.save_lanes_flag && outputFile && outputFile->is_open())
194  {
195  (*outputFile) << "frame#" << setw(8) << setfill('0') << index <<
196  " has " << splines.size() << " splines" << endl;
197  for (uint i=0; i<splines.size(); ++i)
198  {
199  (*outputFile) << "\tspline#" << i+1 << " has " <<splines[i].degree+1 << " points and score " << splineScores[i] << endl;
200  for (int j=0; j<=splines[i].degree; ++j)
201  (*outputFile) << "\t\t" <<splines[i].points[j].x << ", " <<splines[i].points[j].y << endl;
202  }
203  }
204 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
205 
206  // show or save
207  if (options.show_flag || options.save_images_flag)
208  {
209 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
210  // show detected lanes
211  CvMat *imDisplay = cvCloneMat(&cv_raw_mat);
212 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
213  // convert to BGR
214  // cvCvtColor(raw_mat, imDisplay, CV_RGB2BGR);
215  if (lanesConf.ransacLine && !lanesConf.ransacSpline)
216  for(uint i=0; i<lanes.size(); i++)
217  mcvDrawLine(imDisplay, lanes[i], CV_RGB(0,125,0), 3);
218 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
219  // print lanes
220  if (lanesConf.ransacSpline)
221  {
222  for(uint i=0; i<splines.size(); i++)
223  {
224  if (splines[i].color == LINE_COLOR_YELLOW)
225  {
226  mcvDrawSpline(imDisplay, splines[i], CV_RGB(255,255,0), 3);
227 
228 
229  }
230  else
231  {
232  mcvDrawSpline(imDisplay, splines[i], CV_RGB(0,255,0), 3);
233 
234 // cout<<"splines.degree--> "<<splines[i].degree<<endl;
235  double x_max=0,y_max=0;
236  double x_min = cameraInfo.imageWidth;
237  double y_min = cameraInfo.imageHeight;
238 
239  for (uint n=0 ; n<4 ; n++)
240  {
241  if (DEBUG_LINES)
242  {
243  cout<<"spline->"<<i<<endl;
244  cout<<"splines.points.x--> "<<splines[i].points[n].x<<endl;
245  cout<<"splines.points.y--> "<<splines[i].points[n].y<<endl;
246  }
247 
248  /* Escolher os pontos extremos, para desenhar o poligono que reprensenta a estrada */
249  if ( (splines[i].points[n].x < x_min) )
250  {
251  x_min = splines[i].points[n].x;
252  y_min = splines[i].points[n].y;
253  }
254 
255  if ( (splines[i].points[n].x > x_max) )
256  {
257  x_max = splines[n].points[i].x;
258  y_max = splines[n].points[i].y;
259  }
260 
261  /* display dos pontos extremos */
262 // if (DEBUG_LINES)
263 // {
264  if (n==3)
265  {
266  cout<<"Image size: "<<cameraInfo.imageHeight<<"x"<<cameraInfo.imageWidth<<endl;
267  cout<<"min point x = ("<<x_min<<" , "<<y_min<<" )"<<endl;
268  cout<<"max point x = ("<<x_max<<" , "<<y_max<<" )"<<endl;
269  cout<<"i=="<<i<<endl;
271  /* Creat a poligom image with the road */
273 // int npts = 6;
274 // int npt[] = { 6 };
275 //
276 // Point Pts_poly[1][npts];
277 // Pts_poly[0][0] = cv::Point( TwoLanes1(0,0) , TwoLanes1(1,0) );
278 // Pts_poly[0][1] = cv::Point( TwoLanes1(2,0) , TwoLanes1(3,0) );
279 // Pts_poly[0][2] = cv::Point( Templ(0) , Templ(1) );
280 // Pts_poly[0][3] = cv::Point( Tempr(0) , Tempr(1) );
281 // Pts_poly[0][4] = cv::Point( TwoLanes1(2,1),TwoLanes1(3,1) );
282 // Pts_poly[0][5] = cv::Point( TwoLanes1(0,1),TwoLanes1(1,1) );
283 //
284 // const Point *ppt[1]={ Pts_poly[0] };
285 
286 // fillPoly( cv_clone, ppt, npt, 1, Scalar( 200, 200, 200 ),8);
288  if ( i==1 ) /* Só desenhar o poligono caso existam mais de uma linha encontrada */
289  {
290  int npts = 4;
291  int npt[] = { 4 };
292 
293  Point Pts_poly[1][npts];
294 
295  for (uint pt = 0; pt<npts ; pt++)
296  Pts_poly[0][0] = Point( splines[0].points[0].x , splines[0].points[0].y ) ;
297 //
298  Mat disp = cv::Mat(imDisplay, true); // to copy the data
299 
300  const Point *ppt[1]={ Pts_poly[0] };
301  fillPoly( disp, ppt, npt, 1, Scalar( 200, 200, 200 ),8);
302  }
303  }
304 // }
305  }
306 
307  }
308  // print numbers?
309  if (options.show_lane_numbers_flag)
310  {
311  char str[256];
312  sprintf(str, "%d", i);
313  mcvDrawText(imDisplay, str,cvPointFrom32f(splines[i].points[splines[i].degree]),1, CV_RGB(0, 0, 255));
314  }
315  }
316  }
317  // show?
318  if (options.show_flag)
319  {
320  // set the wait value
321  int wait = options.step_flag ? 0 : options.wait_arg;
322  // show image with detected lanes
323  SHOW_IMAGE(imDisplay, "Detected Lanes", wait);
324  }
325 
326  // clear
327  cvReleaseMat(&imDisplay);
328  }
329 
330 //
331 
332 
333 
334 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
335 // cvReleaseMat(&raw_mat);
336 // cvReleaseMat(&channelImage);
337 // cout<<"------> Debug at in "<<__FILE__<<" at line "<<__LINE__<<endl;
338  }
339 
340 
347  bool ReadLines(const char* filename, vector<string> *lines)
348  {
349  // make sure it's not NULL
350  if (!lines)
351  return false;
352  // resize
353  lines->clear();
354 
355  ifstream file;
356  file.open(filename, ifstream::in);
357  char buf[5000];
358  // read lines and process
359  while (file.getline(buf, 5000))
360  {
361  string str(buf);
362  lines->push_back(str);
363  }
364  // close
365  file.close();
366  return true;
367  }
368 
369 }; //end class0xd6c474
void mcvInitCameraInfo(char *const fileName, CameraInfo *cameraInfo)
gengetopt_args_info options
Definition: ProcessImage.h:74
void mcvDrawText(CvMat *image, char *str, CvPoint point, float size, CvScalar color)
unsigned int uint
Definition: ranker.h:38
int cmdline_parser(int argc, char *const *argv, struct gengetopt_args_info *args_info)
Definition: cmdline.cpp:443
void mcvDrawLine(CvMat *image, Line line, CvScalar color, int width)
void mcvGetLanes(const CvMat *inImage, const CvMat *clrImage, vector< Line > *lanes, vector< FLOAT > *lineScores, vector< Spline > *splines, vector< float > *splineScores, CameraInfo *cameraInfo, LaneDetectorConf *stopLineConf, LineState *state)
void mcvInitLaneDetectorConf(char *const fileName, LaneDetectorConf *stopLineConf)
void SHOW_IMAGE(const CvMat *pmat, const char str[], int wait)
Definition: mcv.cpp:67
void mcvDrawSpline(CvMat *image, Spline spline, CvScalar color, int width)
#define FLOAT_MAT_TYPE
Definition: mcv.hh:21
bool ReadLines(const char *filename, vector< string > *lines)
Definition: ProcessImage.h:347
LaneDetectorConf stoplinesConf
Definition: ProcessImage.h:78
Structure to hold lane detector settings.
Definition: LaneDetector.hh:84
#define MSG(fmt,...)
Definition: ProcessImage.h:58
int Load_config(int argc, char **argv)
Definition: ProcessImage.h:83
Camera Calibration info.
CameraInfo cameraInfo
Definition: ProcessImage.h:76
void ProcessImage(Mat &raw_mat, ofstream *outputFile, int index, clock_t *elapsedTime)
Definition: ProcessImage.h:124


caltech_lanes
Author(s): Ricardo Morais
autogenerated on Mon Mar 2 2015 01:31:31