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 
276 
277  Size currSize=Image.size();
278  float scaledown = pow(2,(-1.0/nPerOct)), scaleup = pow(2,1.0/nPerOct);
279 
280  Mat currScaledImg=Image , IntegralChns;
281 
282 // cout<<"currsize w: "<<currSize.width<<" minSize w: "<<minSize.width<<" currsize height: "<<currSize.height<< " minsize height: "<<minSize.height<<endl;
283 
284  while (currSize.width>=minSize.width && currSize.height>=minSize.height)
285  {
286 
287  Mat MergedChannels (currScaledImg.rows, currScaledImg.cols, CV_32FC (CHANNELNR));
288 
289  ComputeChannels(currScaledImg,MergedChannels);
290 
291  integral (MergedChannels, IntegralChns, CV_32FC (CHANNELNR));
292  GetChnFtrsOverImage (IntegralChns, region, features, Params,PedRect,boost_classifier);
293 
294  currSize.width *=scaledown; currSize.height *=scaledown;
295  resize(Image, currScaledImg, currSize, 0, 0, INTER_LINEAR);
296 
297 
298  }
299 
300  if (nOctUp !=0)
301  {
302 
303  currSize = Image.size();
304  Size maxSize;
305 
306  maxSize.width = currSize.width+currSize.width*0.5*nOctUp;
307  maxSize.height = currSize.height+currSize.height*0.5*nOctUp;
308  currScaledImg=Image;
309 
310  while (currSize.width<maxSize.width && currSize.height<maxSize.height)
311  {
312  currSize.width *=scaleup; currSize.height *=scaleup;
313  resize(Image, currScaledImg, currSize, 0, 0, INTER_LINEAR);
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 
321  GetChnFtrsOverImage (IntegralChns, region, features, Params,PedRect,boost_classifier);
322 
323  }
324 
325  }
326 
327 
328 }
329 
330 void GetChnFtrsOverImagePyramid(Mat Image , CvRect & region , vector<float> & features,vector<DVector> & WindowFtrs,int nOctUp, Size minSize, int nPerOct, FtrVecParams2 Params)
331 {
332 
333 
334  Size currSize=Image.size();
335  float scaledown = pow(2,(-1.0/nPerOct)), scaleup = pow(2,1.0/nPerOct);
336 
337  Mat currScaledImg=Image , IntegralChns;
338 
339 // cout<<"currsize w: "<<currSize.width<<" minSize w: "<<minSize.width<<" currsize height: "<<currSize.height<< " minsize height: "<<minSize.height<<endl;
340 
341  while (currSize.width>=minSize.width && currSize.height>=minSize.height)
342  {
343 
344  Mat MergedChannels (currScaledImg.rows, currScaledImg.cols, CV_32FC (CHANNELNR));
345 
346  ComputeChannels(currScaledImg,MergedChannels);
347 
348  integral (MergedChannels, IntegralChns, CV_32FC (CHANNELNR));
349  GetChnFtrsOverImage (IntegralChns, region, features,WindowFtrs, Params);
350 
351  currSize.width *=scaledown; currSize.height *=scaledown;
352  resize(Image, currScaledImg, currSize, 0, 0, INTER_LINEAR);
353 
354 
355  }
356 
357  if (nOctUp !=0)
358  {
359 
360  currSize = Image.size();
361  Size maxSize;
362 
363  maxSize.width = currSize.width+currSize.width*0.5*nOctUp;
364  maxSize.height = currSize.height+currSize.height*0.5*nOctUp;
365  currScaledImg=Image;
366 
367  while (currSize.width<maxSize.width && currSize.height<maxSize.height)
368  {
369  currSize.width *=scaleup; currSize.height *=scaleup;
370  resize(Image, currScaledImg, currSize, 0, 0, INTER_LINEAR);
371 
372  Mat MergedChannels (currScaledImg.rows, currScaledImg.cols, CV_32FC (CHANNELNR));
373 
374  ComputeChannels(currScaledImg,MergedChannels);
375 
376  integral (MergedChannels, IntegralChns, CV_32FC (CHANNELNR));
377 
378  GetChnFtrsOverImage (IntegralChns, region, features, WindowFtrs ,Params);
379 
380  }
381 
382  }
383 
384 
385 }
386 
387 
388 void GetChnFtrsOverImage(Mat IntegralChannels , CvRect & region ,vector<float> & features , FtrVecParams2 Params,PedRoiVec & PedRect,CvBoost & boost_classifier)
389 {
390 
391  int Rows=0, Cols=0;
392 
393  for (Rows=0; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
394  {
395 
396  for (Cols=0; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
397  {
398 
399 
400  region.x=Cols ; region.y=Rows;
401  GetChnFtrsOverWindow(IntegralChannels ,features,Params,region,PedRect,boost_classifier);
402 
403 
404  }
405 
406 
407  }
408 
409 
410 
411 
412  for (Cols=0 ; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
413  {
414 
415  region.x=Cols; region.y=IntegralChannels.rows-region.height-1;
416  GetChnFtrsOverWindow(IntegralChannels ,features ,Params,region,PedRect,boost_classifier);
417 
418  }
419 
420 
421 
422 
423  for (Rows=0 ; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
424  {
425 
426  region.y=Rows; region.x=IntegralChannels.cols-region.width-1;
427  GetChnFtrsOverWindow(IntegralChannels ,features,Params,region,PedRect,boost_classifier);
428 
429 
430 
431  }
432 
433  region.y=IntegralChannels.rows-region.height-1; region.x=IntegralChannels.cols-region.width-1;
434 
435  GetChnFtrsOverWindow(IntegralChannels ,features,Params,region,PedRect,boost_classifier);
436 
437 
438 
439 }
440 
441 void GetChnFtrsOverImage(Mat IntegralChannels , CvRect & region ,vector<float> & features ,vector<DVector> & WindowFtrs , FtrVecParams2 Params)
442 {
443 
444  int Rows=0, Cols=0;
445 
446  for (Rows=0; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
447  {
448 
449  for (Cols=0; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
450  {
451 
452 
453  region.x=Cols ; region.y=Rows;
454  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs,Params,region);
455 
456 
457  }
458 
459 
460  }
461 
462 
463 
464 
465  for (Cols=0 ; Cols<IntegralChannels.cols-region.width-1; Cols+=STEP)
466  {
467 
468  region.x=Cols; region.y=IntegralChannels.rows-region.height-1;
469  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs ,Params,region);
470 
471  }
472 
473 
474 
475 
476  for (Rows=0 ; Rows<IntegralChannels.rows-region.height-1; Rows+=STEP)
477  {
478 
479  region.y=Rows; region.x=IntegralChannels.cols-region.width-1;
480  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs,Params,region);
481 
482 
483  }
484 
485  region.y=IntegralChannels.rows-region.height-1; region.x=IntegralChannels.cols-region.width-1;
486 
487  GetChnFtrsOverWindow(IntegralChannels ,features,WindowFtrs,Params,region);
488 
489 
490 
491 }
492 
493 void GetIntegralSum(Mat IntegralChannels, vector<float> & features,FtrParams2 Params, CvRect region)
494 {
495 
496  vec10d tl, tr, bl, br , sum;
497  int sR=Params.y+region.y, sC=Params.x+region.x, eR=sR+Params.height, eC=sC+Params.width;
498 
499  tl= IntegralChannels.at<vec10d>(sR,sC);
500  tr= IntegralChannels.at<vec10d>(sR,eC);
501  bl= IntegralChannels.at<vec10d>(eR,sC);
502  br= IntegralChannels.at<vec10d>(eR,eC);
503 
504 
505  sum=br-bl-tr+tl;
506 
507  for (int n=0;n<CHANNELNR;n++)
508  features.push_back(sum[n]);
509 
510 }
511 
512 
513 
514 void GetChnFtrsOverWindow(Mat IntegralChannels , vector<float> & features ,FtrVecParams2 Params, CvRect region , PedRoiVec & PedRect,CvBoost & boost_classifier)
515 {
516 
517 
518  Mat Test;
519  Test=Mat::zeros(1,NRFEATURE,CV_32FC1);
520  PedRoi ROI;
521 // vector<float> aux;
522 
523 
524  for (int n=0; n< Params[0].nFtrs ; n++)
525  {
526 
527  GetIntegralSum(IntegralChannels,features ,Params[n],region);
528 
529 
530  }
531 
532 
533 
534  for(int n=0; n<NRFEATURE; n++)
535  {
536  Test.at<float>(0,n)=features[features.size()-NRFEATURE+n];
537 // aux.push_back(features[features.size()-NRFEATURE+n]);
538 
539  }
540 // CvMat TestMat = Test;
541 // CvMat * weak_responses = cvCreateMat(1,Test.cols,CV_32F);
542 
543  float x = boost_classifier.predict(Test,Mat(),Range::all(),false,true);
544 // float x = boost_classifier.predict(&TestMat,0,weak_responses,CV_WHOLE_SEQ,0,1);
545 
546 // Mat WR(weak_responses,false);
547 
548 // cout<<x<<endl;
549 
550  if (x<=THRESHOLD)
551  {
552  ROI.x=region.x;
553  ROI.y=region.y;
554  ROI.Scale=IntegralChannels.size();
555  PedRect.push_back(ROI);
556 
557  // UNCOMMENT BELLOW TO ENABLE BOOTSTRAPPING --> BE SURE TO CHANGE THE FILE WHERE YOU WANT DATA TO BE SAVED //
558 
559 // fstream outfile;
560 //
561 // outfile.open ("/home/pedrobatista/workingcopy/lar3/perception/pedestrians/PedestrianDetect/train_15000_boot3.csv", fstream::in | fstream::out | fstream::app);
562 //
563 // outfile<<"2,";
564 //
565 // for (int i=0;i<NRFEATURE;i++)
566 // {
567 // if (i != NRFEATURE-1)
568 // {
569 // outfile<<Test.at<float>(0,i)<<",";
570 // }
571 // else
572 // {
573 // outfile<<Test.at<float>(0,i);
574 // }
575 // }
576 //
577 // outfile<<endl;
578  }
579 
580 
581 }
582 
583 void GetChnFtrsOverWindow(Mat IntegralChannels , vector<float> & features,vector<DVector> & WindowFtrs ,FtrVecParams2 Params, CvRect region)
584 {
585  vector<float> aux;
586 
587  for (int n=0; n< Params[0].nFtrs ; n++)
588  {
589 
590  GetIntegralSum(IntegralChannels,features ,Params[n],region);
591 
592  }
593 
594  for(int n=0; n<NRFEATURE; n++)
595  {
596  aux.push_back(features[features.size()-NRFEATURE+n]);
597  }
598 
599  WindowFtrs.push_back(aux);
600 
601 
602 
603 
604 }
605 
606 void PostProcess(Mat Img, vector<Rect> PedRect, FtrVecParams2 randparams, CvBoost & boost_classifier, PedRoiVec & PedRect_Post)
607 {
608  // MAKE POST PROCESSING
609 
610 
611 
612 }
613 
#define CHANNELNR
Definition: peddetect.h:47
vector< PedRoi > PedRoiVec
Definition: peddetect.h:87
Mat GradientMagnitude(Mat src)
Definition: pedfuncs.cpp:30
vector< Mat > MatVector
Definition: peddetect.h:89
#define NRFEATURE
Definition: peddetect.h:49
void GetChnFtrsOverWindow(Mat IntegralChannels, vector< float > &features, FtrVecParams2 Params, CvRect region, PedRoiVec &PedRect, CvBoost &boost_classifier)
Definition: pedfuncs.cpp:514
#define NRFEATURE2
Definition: peddetect.h:50
int nFtrs
Definition: peddetect.h:69
void GetIntegralSum(Mat IntegralChannels, vector< float > &features, FtrParams2 Params, CvRect region)
Definition: pedfuncs.cpp:493
Size Scale
Definition: peddetect.h:80
#define STEP
Definition: peddetect.h:48
#define MINAREA
Definition: peddetect.h:54
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:78
#define PI
Definition: peddetect.h:46
void PostProcess(Mat Img, vector< Rect > PedRect, FtrVecParams2 randparams, CvBoost &boost_classifier, PedRoiVec &PedRect_Post)
Definition: pedfuncs.cpp:606
int height
Definition: peddetect.h:71
Vec< float, CHANNELNR > vec10d
Definition: peddetect.h:90
void GetRandParams(int seed, int NrFtrs, FtrVecParams2 &randparams, Rect region)
Definition: pedfuncs.cpp:216
#define MAXAREAHEIGHT
Definition: peddetect.h:56
void GetFileList(string folder_path, PVector &vect)
Definition: pedfuncs.cpp:153
#define THRESHOLD
Definition: peddetect.h:57
void GetChnFtrsOverImage(Mat IntegralChannels, CvRect &region, vector< float > &features, FtrVecParams2 Params, PedRoiVec &PedRect, CvBoost &boost_classifier)
Definition: pedfuncs.cpp:388
int width
Definition: peddetect.h:70
vector< path > PVector
Definition: peddetect.h:88
vector< FtrParams2 > FtrVecParams2
Definition: peddetect.h:92
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:55
int y
Definition: peddetect.h:79


pedestrian_detect
Author(s): Pedro Batista
autogenerated on Mon Mar 2 2015 01:32:33