/OpencvTest/VideoTest.cpp

https://github.com/dakk/Misc · C++ · 159 lines · 87 code · 48 blank · 24 comment · 12 complexity · 619991330f15a55c694b0f458c0181bd MD5 · raw file

  1. /*
  2. * VideoTest.cpp
  3. *
  4. * Created on: Aug 20, 2011
  5. * Author: Davide Gessa
  6. */
  7. #include "VideoTest.h"
  8. VideoTest::VideoTest() : Test("Realtime Face Replace")
  9. {
  10. }
  11. VideoTest::~VideoTest()
  12. {
  13. }
  14. /** Replace your face with a trollface */
  15. void VideoTest::Trollize(IplImage* frame)
  16. {
  17. cvClearMemStorage( fStorage );
  18. int i;
  19. // Detect faces and store them in a sequence
  20. CvSeq* faces = cvHaarDetectObjects(frame, fCascade, fStorage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20));
  21. // Loop the number of faces found.
  22. for( i = 0; i < (faces ? faces->total : 0); i++ )
  23. {
  24. // Create a new rectangle for drawing the face
  25. CvRect* r = (CvRect*) cvGetSeqElem(faces, i);
  26. // Fix invalid cordinates
  27. r->x -= 20;
  28. r->y -= 20;
  29. if(r->x < 0)
  30. r->x = 0;
  31. if(r->y < 0)
  32. r->y = 0;
  33. r->width += 50;
  34. r->height += 50;
  35. if(r->width > frame->width)
  36. r->width = frame->width;
  37. if(r->height > frame->height)
  38. r->height = frame->height;
  39. // Create a new image to resize the original trollface
  40. IplImage *temp = cvCreateImage(cvSize(r->width, r->height), frame->depth, frame->nChannels);
  41. // Swap the real image in the temp buffer with the new dimension
  42. cvResize(fImage, temp);
  43. // Specify the region of interest where put the image
  44. cvSetImageROI(frame, *r);
  45. // Swap trollface in the specified region
  46. cvAddWeighted(temp, 1.0, frame, 1.0, 0, frame);
  47. // Reset the region
  48. cvSetImageROI(frame, cvRect(0, 0, frame->width, frame->height));
  49. cvReleaseImage( &temp );
  50. }
  51. }
  52. int VideoTest::Run()
  53. {
  54. CvCapture* capture;
  55. IplImage* frame;
  56. IplImage* frame_mod;
  57. // Initialize the capture device
  58. capture = cvCaptureFromCAM(-1);
  59. if(!capture)
  60. {
  61. ShowError("Cannot find webcam");
  62. return -1;
  63. }
  64. cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 700);
  65. cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 700);
  66. cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, 0);
  67. fStorage = cvCreateMemStorage(0);
  68. // Load default cascade for faces
  69. fCascade = (CvHaarClassifierCascade*) cvLoad("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml", 0, 0, 0 );
  70. if(!fCascade)
  71. {
  72. ShowError("Cannot load frontalface cascade");
  73. return -1;
  74. }
  75. // Load the trollface
  76. fImage = cvLoadImage("data/trollface.png", CV_LOAD_IMAGE_UNCHANGED);
  77. if(!fImage)
  78. {
  79. ShowError("Cannot load troll image");
  80. return -1;
  81. }
  82. // Create windows to display results
  83. cvNamedWindow("Capture", 1);
  84. cvNamedWindow("VideoTest", 1);
  85. for(;;)
  86. {
  87. // Retrive data from webcam and copy the frame to frame_mod
  88. if(!cvGrabFrame(capture))
  89. break;
  90. frame = cvRetrieveFrame(capture);
  91. if(!frame)
  92. break;
  93. frame_mod = cvCreateImage(cvSize(frame->width,frame->height), frame->depth, frame->nChannels);
  94. // Show image on capture screen
  95. cvFlip(frame, frame, -1);
  96. cvFlip(frame, frame_mod, 0);
  97. cvShowImage("Capture", frame_mod);
  98. // Elaborate and show the result
  99. Trollize(frame_mod);
  100. cvShowImage("VideoTest", frame_mod);
  101. cvReleaseImage(&frame_mod);
  102. // Esc key
  103. if(cvWaitKey(10) >= 0)
  104. break;
  105. }
  106. cvReleaseCapture(&capture);
  107. cvDestroyWindow("VideoTest");
  108. cvDestroyWindow("Capture");
  109. cvReleaseImage(&fImage);
  110. return 0;
  111. }