学习OpenCV——行人检测&人脸检测(总算运行出来了)

时间:2023-03-08 23:22:01
学习OpenCV——行人检测&人脸检测(总算运行出来了)

之前运行haar特征的adaboost算法人脸检测一直出错,加上今天的HOG&SVM行人检测程序,一直报错。

今天总算发现自己犯了多么白痴的错误——是因为外部依赖项lib文件没有添加完整,想一头囊死啊

做程序一定要心如止水!!! 仔细查找!!!

1.人脸识别程序:

  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <float.h>
  9. #include <limits.h>
  10. #include <time.h>
  11. #include <ctype.h>
  12. using namespace std;
  13. static CvMemStorage* storage = 0;
  14. static CvHaarClassifierCascade* cascade = 0;
  15. void detect_and_draw( IplImage* image );
  16. const char* cascade_name =
  17. "G:/OpenCV2.3.1/data/haarcascades/haarcascade_frontalface_alt.xml";
  18. /* "haarcascade_profileface.xml";*/
  19. int main()
  20. {
  21. CvCapture* capture = 0;
  22. cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
  23. if( !cascade )
  24. {
  25. fprintf( stderr, "ERROR: Could not load classifier cascade/n" );
  26. //fprintf( stderr,
  27. //"Usage: facedetect --cascade=/"<cascade_path>"/[filename|camera_index]/n" );
  28. return -1;
  29. }
  30. storage = cvCreateMemStorage(0);
  31. cvNamedWindow( "result", 1 );
  32. const char* filename = "H:/test/face05.jpg";
  33. IplImage* image = cvLoadImage(filename );
  34. if( image )
  35. {
  36. detect_and_draw( image );
  37. cvWaitKey(0);
  38. cvReleaseImage( &image );
  39. }
  40. cvDestroyWindow("result");
  41. cvWaitKey(0);
  42. return 0;
  43. }
  44. void detect_and_draw( IplImage* img )
  45. {
  46. static CvScalar colors[] =
  47. {
  48. {{0,0,255}},
  49. {{0,128,255}},
  50. {{0,255,255}},
  51. {{0,255,0}},
  52. {{255,128,0}},
  53. {{255,255,0}},
  54. {{255,0,0}},
  55. {{255,0,255}}
  56. };
  57. double scale = 1.3;
  58. IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
  59. IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
  60. cvRound (img->height/scale)),
  61. 8, 1 );
  62. int i;
  63. cvCvtColor( img, gray, CV_BGR2GRAY );
  64. cvResize( gray, small_img, CV_INTER_LINEAR );
  65. cvEqualizeHist( small_img, small_img );
  66. cvClearMemStorage( storage );
  67. if( cascade )
  68. {
  69. double t = (double)cvGetTickCount();
  70. CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
  71. 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
  72. cvSize(30, 30) );
  73. t = (double)cvGetTickCount() - t;
  74. printf( "detection time = %gms/n", t/((double)cvGetTickFrequency()*1000.) );
  75. for( i = 0; i < (faces ? faces->total : 0); i++ )
  76. {
  77. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  78. CvPoint center;
  79. int radius;
  80. center.x = cvRound((r->x + r->width*0.5)*scale);
  81. center.y = cvRound((r->y + r->height*0.5)*scale);
  82. radius = cvRound((r->width + r->height)*0.25*scale);
  83. cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
  84. }
  85. }
  86. cvShowImage( "result", img );
  87. cvReleaseImage( &gray );
  88. cvReleaseImage( &small_img );
  89. }

学习OpenCV——行人检测&人脸检测(总算运行出来了)

2.行人检测程序

  1. #include <cv.h>
  2. #include <highgui.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. using namespace cv;
  11. using namespace std;
  12. void help()
  13. {
  14. printf(
  15. "\nDemonstrate the use of the HoG descriptor using\n"
  16. "  HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"
  17. "Usage:\n"
  18. "./peopledetect (<image_filename> | <image_list>.txt)\n\n");
  19. }
  20. int main(int argc, char** argv)
  21. {
  22. Mat img;
  23. FILE* f = 0;
  24. char _filename[1024];
  25. if( argc == 1 )
  26. {
  27. printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");
  28. return 0;
  29. }
  30. img = imread(argv[1]);
  31. if( img.data )
  32. {
  33. strcpy(_filename, argv[1]);
  34. }
  35. else
  36. {
  37. f = fopen(argv[1], "rt");
  38. if(!f)
  39. {
  40. fprintf( stderr, "ERROR: the specified file could not be loaded\n");
  41. return -1;
  42. }
  43. }
  44. HOGDescriptor hog;
  45. hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//得到检测器
  46. namedWindow("people detector", 1);
  47. for(;;)
  48. {
  49. char* filename = _filename;
  50. if(f)
  51. {
  52. if(!fgets(filename, (int)sizeof(_filename)-2, f))
  53. break;
  54. //while(*filename && isspace(*filename))
  55. //  ++filename;
  56. if(filename[0] == '#')
  57. continue;
  58. int l = strlen(filename);
  59. while(l > 0 && isspace(filename[l-1]))
  60. --l;
  61. filename[l] = '\0';
  62. img = imread(filename);
  63. }
  64. printf("%s:\n", filename);
  65. if(!img.data)
  66. continue;
  67. fflush(stdout);
  68. vector<Rect> found, found_filtered;
  69. double t = (double)getTickCount();
  70. // run the detector with default parameters. to get a higher hit-rate
  71. // (and more false alarms, respectively), decrease the hitThreshold and
  72. // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
  73. hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
  74. t = (double)getTickCount() - t;
  75. printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());
  76. size_t i, j;
  77. for( i = 0; i < found.size(); i++ )
  78. {
  79. Rect r = found[i];
  80. for( j = 0; j < found.size(); j++ )
  81. if( j != i && (r & found[j]) == r)
  82. break;
  83. if( j == found.size() )
  84. found_filtered.push_back(r);
  85. }
  86. for( i = 0; i < found_filtered.size(); i++ )
  87. {
  88. Rect r = found_filtered[i];
  89. // the HOG detector returns slightly larger rectangles than the real objects.
  90. // so we slightly shrink the rectangles to get a nicer output.
  91. r.x += cvRound(r.width*0.1);
  92. r.width = cvRound(r.width*0.8);
  93. r.y += cvRound(r.height*0.07);
  94. r.height = cvRound(r.height*0.8);
  95. rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
  96. }
  97. imshow("people detector", img);
  98. int c = waitKey(0) & 255;
  99. if( c == 'q' || c == 'Q' || !f)
  100. break;
  101. }
  102. if(f)
  103. fclose(f);
  104. return 0;
  105. }

注意:可能会出现tbb_debug.dll的问题,在G:\OpenCV2.3.1\build\common\tbb\ia32\vc10中找到tbb.dll改名为tbb_debug.dll 加到程序绝对目录下即可

还有其他的解决方式:http://blog.csdn.net/scut1135/article/details/7329398

学习OpenCV——行人检测&人脸检测(总算运行出来了)

from: http://blog.csdn.net/yangtrees/article/details/7453987