pedfuncs.cpp
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 ***************************************************************************************************/
27 #include "peddetect.h"
28 
29 
30 Mat GradientMagnitude(Mat src)
31 
32 {
33  Mat xsobel, ysobel, xsobel2, ysobel2, xysobel, GradMag, GrayImg;
34 
35  cvtColor(src,GrayImg, CV_RGB2GRAY,0);
36 
37  Sobel(GrayImg, xsobel, CV_32F, 1, 0, 3, 1, 0, BORDER_DEFAULT);
38  Sobel(GrayImg, ysobel, CV_32F, 0, 1, 3, 1, 0, BORDER_DEFAULT);
39 
40  multiply(xsobel, xsobel, xsobel2, 1, -1 );
41  multiply(ysobel, ysobel, ysobel2, 1, -1 );
42 
43  xysobel=xsobel2+ysobel2;
44  sqrt(xysobel,GradMag);
45 
46  return GradMag;
47 
48 }
49 
50 Mat GradientMagnitude(Mat xsobel, Mat ysobel)
51 
52 {
53  Mat xsobel2, ysobel2, xysobel, GradMag, GrayImg;
54 
55  multiply(xsobel, xsobel, xsobel2, 1, -1 );
56  multiply(ysobel, ysobel, ysobel2, 1, -1 );
57 
58  xysobel=xsobel2+ysobel2;
59  sqrt(xysobel,GradMag);
60 
61  return GradMag;
62 
63 }
64 
65 
66 MatVector OrientedGradientsDiagram(Mat GradMag, Mat xsobel, Mat ysobel)
67 {
68  Mat xsobeldiv=xsobel + 0.00001;
69  Mat div;
70  float angl;
71 
72  MatVector BinVector;
73 
74  divide(ysobel,xsobeldiv,div);
75 
76 
77  Mat Bins[6];
78 
79  for (int n=0; n<6; n++)
80  {
81  Bins[n].create(GradMag.rows,GradMag.cols,CV_32FC1);
82  Bins[n] = Mat::zeros(GradMag.rows,GradMag.cols,CV_32FC1);
83  }
84  for( int r = 0; r < div.rows; r++ )
85  {
86  for( int c = 0; c < div.cols; c++ )
87  {
88 
89  angl = ((atan (div.at<float>(r,c))) * (180 / PI)) + 90;
90  if (angl<=30)
91  {
92  Bins[0].at<float>(r,c) = GradMag.at<float>(r,c);
93 
94  }
95 
96  else if (angl<=60)
97  {
98  Bins[1].at<float>(r,c) = GradMag.at<float>(r,c);
99 
100  }
101  else if (angl<=90)
102  {
103  Bins[2].at<float>(r,c) = GradMag.at<float>(r,c);
104 
105  }
106  else if (angl<=120)
107  {
108  Bins[3].at<float>(r,c) = GradMag.at<float>(r,c);
109 
110  }
111 
112  else if (angl<=150)
113  {
114  Bins[4].at<float>(r,c) = GradMag.at<float>(r,c);
115 
116  }
117  else
118  {
119  Bins[5].at<float>(r,c) = GradMag.at<float>(r,c);
120 
121  }
122  }
123 
124  }
125 
126  for(int i=0; i<6; i++)
127  {
128  BinVector.push_back(Bins[i]);
129 
130  }
131 
132  return BinVector;
133 
134 }
135 
137 {
138  Mat LUV(Img.rows, Img.cols, CV_32FC3);
139  MatVector LUVchannels;
140 
141  cvtColor(Img,LUV, CV_RGB2Luv,CV_32F);
142 
143  LUV.assignTo(LUV, CV_32F );
144 
145  split(LUV,LUVchannels);
146 
147 
148  return LUVchannels;
149 
150 }
151 
152 
153  void GetFileList(string folder_path, PVector & vect)
154 {
155 
156  path p = folder_path;
157 
158  try
159  {
160  if (exists(p)) // does p actually exist?
161  {
162 
163  if (is_directory(p)) // is p a directory?
164  {
165 
166  copy(directory_iterator(p), directory_iterator(), back_inserter(vect));
167 
168  }
169  else
170  cout << p << " is not a folder"<<endl;
171 
172  }
173 
174  }
175 
176 
177  catch (const filesystem_error& ex)
178  {
179  cout << ex.what() << '\n';
180  }
181 
182 
183 }
184 
185 
186 void ComputeChannels(Mat Image, Mat & MergedChannels)
187 {
188 
189  Mat xsobel, ysobel, GradMag, GrayImg;
190  MatVector BinVect, LUVchannels;
191 
192  cvtColor (Image, GrayImg, CV_RGB2GRAY, 0);
193 
194  Sobel (GrayImg, xsobel, CV_32FC1, 1, 0, 3, 1, 0, BORDER_DEFAULT);
195  Sobel (GrayImg, ysobel, CV_32FC1, 0, 1, 3, 1, 0, BORDER_DEFAULT);
196 
197  GradMag = GradientMagnitude (xsobel, ysobel);
198 
199  BinVect = OrientedGradientsDiagram (GradMag, xsobel, ysobel);
200 
201  LUVchannels = LUVcolourchannels (Image);
202 
203 
204  Mat ToBeMergedChannels[] =
205  { GradMag, BinVect[0], BinVect[1], BinVect[2], BinVect[3], BinVect[4],
206  BinVect[5], LUVchannels[0], LUVchannels[1], LUVchannels[2]
207  };
208 
209 
210 
211  merge (ToBeMergedChannels, CHANNELNR, MergedChannels);
212 
213 }
214 
215 
216 void GetRandParams(int seed, int NrFtrs, FtrVecParams2 & randparams, Rect region)
217 {
218 
219  cv::RNG rng(seed);
220 
221 // vector <int> record;
222 // ifstream infile("/home/pedrobatista/workingcopy/lar3/perception/pedestrians/PedestrianDetect/PredictorImportance.txt");
223 //
224 // while (infile)
225 // {
226 // string s;
227 // if (!getline( infile, s )) break;
228 //
229 // istringstream ss( s );
230 //
231 // while (ss)
232 // {
233 // string s;
234 // if (!getline( ss, s, ',' )) break;
235 // record.push_back( atoi(s.c_str() ));
236 // }
237 //
238 // }
239 //
240 // if (!infile.eof())
241 // {
242 // cerr << "Fooey!\n";
243 // }
244 //
245 
246 
247  FtrParams2 Params;
248  Params.nFtrs=NRFEATURE2;
249 
250  for (int n=0; n<NrFtrs; n++)
251  {
252 // Params.channelidx=rng.uniform(0,CHANNELNR);
253 
254 // if(std::find(record.begin(), record.end(), n) != record.end())
255 //
256 // {
257 //
258  Params.width=rng.uniform(MINAREA,MAXAREAWIDTH+1);
259  Params.height=rng.uniform(MINAREA,MAXAREAHEIGHT+1);
260 
261  Params.x=rng.uniform(0,region.width-Params.width+1);
262  Params.y=rng.uniform(0,region.height-Params.height+1);
263 
264  randparams.push_back(Params);
265 // }
266 
267  }
268 
269 
270 
271 }
272 
273 void GetChnFtrsOverImagePyramid(Mat Image , CvRect & region , vector<float> & features,int nOctUp, Size minSize, int nPerOct, FtrVecParams2 Params,PedRoiVec & PedRect, CvBoost & boost_classifier)
274 {
275 // int numero=0;
276  Size currSize=Image.size();
277 
278  float scaledown = pow(2,(-1.0/nPerOct)), scaleup = pow(2,1.0/nPerOct);
279 
280  Mat currScaledImg=Image , IntegralChns;
281 
282  while (currSize.width>=minSize.width && currSize.height>=minSize.height)
283  {
284  Mat MergedChannels (currScaledImg.rows, currScaledImg.cols, CV_32FC (CHANNELNR));
285 
286  ComputeChannels(currScaledImg,MergedChannels);
287 
288  integral(MergedChannels, IntegralChns, CV_32FC (CHANNELNR));
289 
290  boost::this_thread::sleep(boost::posix_time::microseconds(0.1));
291 
292  GetChnFtrsOverImage (IntegralChns, region, features, Params,PedRect,boost_classifier);
293 
294  currSize.width *=scaledown; currSize.height *=scaledown;
295 
296  resize(Image, currScaledImg, currSize, 0, 0, INTER_LINEAR);
297 
298  boost::this_thread::sleep(boost::posix_time::microseconds(0.1));
299  }
300 
301 
302 }
303 
304 void GetChnFtrsOverImagePyramid(Mat Image , CvRect & region , vector<float> & features,vector<DVector> & WindowFtrs,int nOctUp, Size minSize, int nPerOct, FtrVecParams2 Params)
305 {
306  Size currSize=Image.size();
307  float scaledown = pow(2,(-1.0/nPerOct)), scaleup = pow(2,1.0/nPerOct);
308 
309  Mat currScaledImg=Image , IntegralChns;
310 
311  // cout<<"currsize w: "<<currSize.width<<" minSize w: "<<minSize.width<<" currsize height: "<<currSize.height<< " minsize height: "<<minSize.height<<endl;
312 
313  while (currSize.width>=minSize.width && currSize.height>=minSize.height)
314  {
315  Mat MergedChannels (currScaledImg.rows, currScaledImg.cols, CV_32FC (CHANNELNR));
316 
317  ComputeChannels(currScaledImg,MergedChannels);
318 
319  integral (MergedChannels, IntegralChns, CV_32FC (CHANNELNR));
320  GetChnFtrsOverImage (IntegralChns, region, features,WindowFtrs, Params);
321 
322  currSize.width *=scaledown; currSize.height *=scaledown;
323  resize(Image, currScaledImg, currSize, 0, 0, INTER_LINEAR);
324  }
325 }
326 
327 
328 void GetChnFtrsOverImage(Mat IntegralChannels , CvRect & region ,vector<float> & features , FtrVecParams2 Params,PedRoiVec & PedRect,CvBoost & boost_classifier)
329 {
330  int Rows=0, Cols=0;
331 
332  for (Rows=0; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
333  {
334  for (Cols=0; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
335  {
336  region.x=Cols ; region.y=Rows;
337 
338  GetChnFtrsOverWindow(IntegralChannels ,features,Params,region,PedRect,boost_classifier);
339  }
340  }
341 
342 
343  for (Cols=0 ; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
344  {
345 
346  region.x=Cols; region.y=IntegralChannels.rows-region.height-1;
347  GetChnFtrsOverWindow(IntegralChannels ,features ,Params,region,PedRect,boost_classifier);
348 
349  }
350 
351 
352  for (Rows=0 ; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
353  {
354 
355  region.y=Rows; region.x=IntegralChannels.cols-region.width-1;
356  GetChnFtrsOverWindow(IntegralChannels ,features,Params,region,PedRect,boost_classifier);
357 
358 
359 
360  }
361 
362 
363  region.y=IntegralChannels.rows-region.height-1; region.x=IntegralChannels.cols-region.width-1;
364  GetChnFtrsOverWindow(IntegralChannels ,features,Params,region,PedRect,boost_classifier);
365 
366 }
367 
368 void GetChnFtrsOverImage(Mat IntegralChannels , CvRect & region ,vector<float> & features ,vector<DVector> & WindowFtrs , FtrVecParams2 Params)
369 {
370 
371  int Rows=0, Cols=0;
372 
373  for (Rows=0; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
374  {
375 
376  for (Cols=0; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
377  {
378 
379 
380  region.x=Cols ; region.y=Rows;
381  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs,Params,region);
382 
383 
384  }
385 
386 
387  }
388 
389 
390 
391 
392  for (Cols=0 ; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
393  {
394 
395  region.x=Cols; region.y=IntegralChannels.rows-region.height-1;
396  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs ,Params,region);
397 
398  }
399 
400 
401 
402 
403  for (Rows=0 ; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
404  {
405 
406  region.y=Rows; region.x=IntegralChannels.cols-region.width-1;
407  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs,Params,region);
408 
409 
410  }
411 
412  region.y=IntegralChannels.rows-region.height-1; region.x=IntegralChannels.cols-region.width-1;
413 
414  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs,Params,region);
415 
416 
417 
418 }
419 
420 void GetIntegralSum(Mat IntegralChannels, vector<float> & features,FtrParams2 Params, CvRect region)
421 {
422  vec10d tl, tr, bl, br , sum;
423 // cout << "estou aqui" << endl;
424  int sR=Params.y+region.y, sC=Params.x+region.x, eR=sR+Params.height, eC=sC+Params.width;
425 // int sR=region.y, sC=region.x, eR=sR+region.height-1, eC=sC+region.width-1;
426 // cout << "sR: " << sR << "sC: " << sC << "eR: " << eR<< "eC: " << eC << endl;
427 
428  tl= IntegralChannels.at<vec10d>(sR,sC);
429  tr= IntegralChannels.at<vec10d>(sR,eC);
430  bl= IntegralChannels.at<vec10d>(eR,sC);
431  br= IntegralChannels.at<vec10d>(eR,eC);
432  sum=br-bl-tr+tl;
433 
434 // cout << sum << endl;
435 
436 // for (int n=0;n<CHANNELNR;n++)
437 // cout << "channelnr" << sum << endl;
438  for (int n=0;n<CHANNELNR;n++)
439  {
440  features.push_back(sum[n]);
441  }
442 }
443 
444 
445 
446 void GetChnFtrsOverWindow(Mat IntegralChannels , vector<float> & features ,FtrVecParams2 Params, CvRect region , PedRoiVec & PedRect,CvBoost & boost_classifier)
447 {
448  Mat Test;
449 
450  Test=Mat::zeros(1,NRFEATURE,CV_32FC1);
451 
452  PedRoi ROI;
453  // cout << "estou aqui1" << endl;
454  for (int n=0; n< Params[0].nFtrs ; n++)
455  {
456 
457  GetIntegralSum(IntegralChannels,features ,Params[n],region);
458 
459  }
460 
461  boost::this_thread::sleep(boost::posix_time::microseconds(0.001));
462 
463  for(int n=0; n<NRFEATURE; n++)
464  {
465  Test.at<float>(0,n)=features[features.size()-NRFEATURE+n];
466  // aux.push_back(features[features.size()-NRFEATURE+n]);
467 
468  }
469 // imshow("Test", Test);
470 // waitKey(2);
471 
472  float x = boost_classifier.predict(Test,Mat(),Range::all(),false,true);
473 
474  // cout << "Predict: " << x << endl;
475 
476  if (x<=THRESHOLD)
477  {
478  ROI.x=region.x;
479  ROI.y=region.y;
480  ROI.Scale=IntegralChannels.size();
481  PedRect.push_back(ROI);
482 
483  }
484 
485 
486 }
487 
488 void GetChnFtrsOverWindow(Mat IntegralChannels , vector<float> & features,vector<DVector> & WindowFtrs ,FtrVecParams2 Params, CvRect region)
489 {
490  vector<float> aux;
491 
492  for (int n=0; n< Params[0].nFtrs ; n++)
493  {
494 
495  GetIntegralSum(IntegralChannels,features ,Params[n],region);
496 
497  }
498 
499  for(int n=0; n<NRFEATURE; n++)
500  {
501  aux.push_back(features[features.size()-NRFEATURE+n]);
502  }
503 
504  WindowFtrs.push_back(aux);
505 
506 
507 
508 
509 }
510 
511 void PostProcess(Mat Img, vector<Rect> PedRect, FtrVecParams2 randparams, CvBoost & boost_classifier, PedRoiVec & PedRect_Post)
512 {
513  // MAKE POST PROCESSING
514 
515 
516 
517 }
518 
#define CHANNELNR
Definition: peddetect.h:52
vector< PedRoi > PedRoiVec
Definition: peddetect.h:91
Mat GradientMagnitude(Mat src)
Definition: pedfuncs.cpp:30
vector< Mat > MatVector
Definition: peddetect.h:93
#define NRFEATURE
Definition: peddetect.h:54
void GetChnFtrsOverWindow(Mat IntegralChannels, vector< float > &features, FtrVecParams2 Params, CvRect region, PedRoiVec &PedRect, CvBoost &boost_classifier)
Definition: pedfuncs.cpp:446
#define NRFEATURE2
Definition: peddetect.h:55
int nFtrs
Definition: peddetect.h:73
void GetIntegralSum(Mat IntegralChannels, vector< float > &features, FtrParams2 Params, CvRect region)
Definition: pedfuncs.cpp:420
Size Scale
Definition: peddetect.h:84
#define STEP
Definition: peddetect.h:53
#define MINAREA
Definition: peddetect.h:59
void GetChnFtrsOverImagePyramid(Mat Image, CvRect &region, vector< float > &features, int nOctUp, Size minSize, int nPerOct, FtrVecParams2 Params, PedRoiVec &PedRect, CvBoost &boost_classifier)
Definition: pedfuncs.cpp:273
int x
Definition: peddetect.h:82
#define PI
Definition: peddetect.h:51
void PostProcess(Mat Img, vector< Rect > PedRect, FtrVecParams2 randparams, CvBoost &boost_classifier, PedRoiVec &PedRect_Post)
Definition: pedfuncs.cpp:511
int height
Definition: peddetect.h:75
Vec< float, CHANNELNR > vec10d
Definition: peddetect.h:94
void GetRandParams(int seed, int NrFtrs, FtrVecParams2 &randparams, Rect region)
Definition: pedfuncs.cpp:216
#define MAXAREAHEIGHT
Definition: peddetect.h:61
void GetFileList(string folder_path, PVector &vect)
Definition: pedfuncs.cpp:153
#define THRESHOLD
Definition: peddetect.h:62
void GetChnFtrsOverImage(Mat IntegralChannels, CvRect &region, vector< float > &features, FtrVecParams2 Params, PedRoiVec &PedRect, CvBoost &boost_classifier)
Definition: pedfuncs.cpp:328
int width
Definition: peddetect.h:74
vector< path > PVector
Definition: peddetect.h:92
vector< FtrParams2 > FtrVecParams2
Definition: peddetect.h:96
MatVector LUVcolourchannels(Mat Img)
Definition: pedfuncs.cpp:136
void ComputeChannels(Mat Image, Mat &MergedChannels)
Definition: pedfuncs.cpp:186
MatVector OrientedGradientsDiagram(Mat GradMag, Mat xsobel, Mat ysobel)
Definition: pedfuncs.cpp:66
#define MAXAREAWIDTH
Definition: peddetect.h:60
int y
Definition: peddetect.h:83


multimodal_pedestrian_detect
Author(s): Rui Azevedo
autogenerated on Mon Mar 2 2015 01:32:27