当前位置:   article > 正文

从摄像头中检测脸并保存图像_摄像头人脸识别保存图片

摄像头人脸识别保存图片

改写自:http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

  1. #include "opencv2/objdetect/objdetect.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include <iostream>
  5. #include <stdio.h>
  6. using namespace std;
  7. using namespace cv;
  8. /** Function Headers */
  9. void detectAndDisplay( Mat frame );
  10. /** Global variables */
  11. String face_cascade_name = "haarcascade_frontalface_alt.xml";
  12. CascadeClassifier face_cascade;
  13. string window_name = "Capture - Face detection";
  14. /** @function main */
  15. int main( int argc, const char** argv )
  16. {
  17. CvCapture* capture;
  18. Mat frame;
  19. //-- 1. Load the cascades
  20. if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
  21. //-- 2. Read the video stream
  22. capture = cvCaptureFromCAM( -1 );
  23. if( capture )
  24. {
  25. while( true )
  26. {
  27. frame = cvQueryFrame( capture );
  28. //-- 3. Apply the classifier to the frame
  29. if( !frame.empty() )
  30. {
  31. detectAndDisplay( frame );
  32. }
  33. else
  34. { printf(" --(!) No captured frame -- Break!"); break; }
  35. int c = waitKey(10);
  36. if( (char)c == 'c' ) {
  37. break;
  38. }
  39. }
  40. }
  41. return 0;
  42. }
  43. /** @function detectAndDisplay */
  44. void detectAndDisplay( Mat frame )
  45. {
  46. static int index = 0;
  47. char image_name[256];
  48. std::vector<Rect> faces;
  49. Mat frame_gray;
  50. cvtColor( frame, frame_gray, CV_BGR2GRAY );
  51. equalizeHist( frame_gray, frame_gray );
  52. //-- Detect faces
  53. face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  54. for( size_t i = 0; i < faces.size(); i++ )
  55. {
  56. Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
  57. rectangle(frame, faces[i], CV_RGB(0, 255,0), 1);
  58. Mat faceROI = frame_gray( faces[i] );
  59. sprintf(image_name, "frame%d_%d.jpg", index++, i);
  60. if(index <= 32){
  61. imwrite(image_name, faceROI);
  62. printf("frame %d write\n", index);
  63. }
  64. }
  65. //-- Show what you got
  66. imshow( window_name, frame );
  67. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/763669
推荐阅读
相关标签
  

闽ICP备14008679号