fork download
  1. #include "stdafx.h"
  2. #include <opencv2/opencv.hpp>
  3.  
  4. static CvMemStorage *storage = 0;
  5. static CvHaarClassifierCascade *cascade = 0;
  6. bool detect_and_draw( IplImage* image ,CvHaarClassifierCascade* cascade);
  7.  
  8.  
  9. int main( int argc, char* argv )
  10. {
  11. CvCapture* capture = 0;
  12. IplImage *frame, *frame_copy = 0;
  13.  
  14. const char* input_name;
  15. storage = cvCreateMemStorage(0);//建立一個指定大小的記憶體區塊,若為0,則建立的記憶體區塊大小依照預設值為64k
  16. capture = cvCaptureFromCAM( 0);// 初始webcam
  17. cvNamedWindow( "result", 1 );//建立窗口
  18.  
  19. // If loaded succesfully, then:
  20. if( capture )
  21. {
  22. for(;;)
  23. {
  24. // Capture the frame and load it in IplImage
  25. if( !cvGrabFrame( capture ))
  26. break;
  27. frame = cvRetrieveFrame( capture );
  28. // If the frame does not exist, quit the loop
  29. if( !frame )
  30. break;
  31. // Allocate framecopy as the same size of the frame
  32. if( !frame_copy )
  33. frame_copy = cvCreateImage( cvSize(frame->width,frame->height),IPL_DEPTH_8U, frame->nChannels );
  34.  
  35. // Check the origin of image. If top left, copy the image frame to frame_copy.
  36. if( frame->origin == IPL_ORIGIN_TL )
  37. cvCopy( frame, frame_copy, 0 );
  38. // Else flip and copy the image
  39. else
  40. cvFlip( frame, frame_copy, 0 );
  41.  
  42.  
  43. cascade = (CvHaarClassifierCascade*)cvLoad( "haar/haarcascade_mcs_mouth.xml",0,0,0 );
  44.  
  45. // Check whether the cascade has loaded successfully. Else report and error and quit
  46. if( !cascade )
  47. {
  48. fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
  49. return -1;
  50. }
  51. // Call the function to detect and draw the face
  52. detect_and_draw(frame_copy,cascade);
  53.  
  54.  
  55. // Wait for a while before proceeding to the next frame
  56. if( cvWaitKey( 1 ) >= 0 )
  57. break;
  58.  
  59.  
  60.  
  61. }
  62.  
  63. // Release the images, and capture memory
  64. cvReleaseHaarClassifierCascade(&cascade);
  65.  
  66. cvReleaseImage( &frame_copy );
  67. cvReleaseCapture( &capture );
  68. cvReleaseMemStorage(&storage);
  69.  
  70.  
  71. }
  72.  
  73. return 0;
  74.  
  75. }
  76.  
  77. bool detect_and_draw( IplImage* img,CvHaarClassifierCascade* cascade )
  78. {
  79. int scale = 1;
  80.  
  81. // Create a new image based on the input image
  82. IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
  83.  
  84. // Create two points to represent the face locations
  85. CvPoint pt1, pt2;
  86. int i;
  87.  
  88. // Clear the memory storage which was used before
  89. cvClearMemStorage( storage );
  90.  
  91. // Find whether the cascade is loaded, to find the faces. If yes, then:
  92. if( cascade )
  93. {
  94.  
  95.  
  96. CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
  97. 1.1, 30, CV_HAAR_DO_CANNY_PRUNING,
  98. cvSize(40, 40) );
  99. for( i = 0; i < (faces ? faces->total : 0); i++ )
  100. {
  101.  
  102. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  103.  
  104. pt1.x = r->x*scale;
  105. pt2.x = (r->x+r->width)*scale;
  106. pt1.y = r->y*scale;
  107. pt2.y = (r->y+r->height)*scale;
  108.  
  109. // Draw the rectangle in the input image
  110. cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
  111. }
  112. }
  113.  
  114.  
  115. cvShowImage( "result", img );
  116. if(i > 0)
  117. return 1;
  118. else
  119. return 0;
  120.  
  121. cvReleaseImage( &temp );
  122.  
  123.  
  124. }
  125.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:20: error: stdafx.h: No such file or directory
prog.cpp:2:30: error: opencv2/opencv.hpp: No such file or directory
prog.cpp:4: error: expected initializer before ‘*’ token
prog.cpp:5: error: expected initializer before ‘*’ token
prog.cpp:6: error: ‘IplImage’ was not declared in this scope
prog.cpp:6: error: ‘image’ was not declared in this scope
prog.cpp:6: error: ‘CvHaarClassifierCascade’ was not declared in this scope
prog.cpp:6: error: ‘cascade’ was not declared in this scope
prog.cpp:6: error: initializer expression list treated as compound expression
prog.cpp:9: error: second argument of ‘int main(int, char*)’ should be ‘char **’
prog.cpp: In function ‘int main(int, char*)’:
prog.cpp:11: error: ‘CvCapture’ was not declared in this scope
prog.cpp:11: error: ‘capture’ was not declared in this scope
prog.cpp:12: error: ‘IplImage’ was not declared in this scope
prog.cpp:12: error: ‘frame’ was not declared in this scope
prog.cpp:12: error: ‘frame_copy’ was not declared in this scope
prog.cpp:15: error: ‘storage’ was not declared in this scope
prog.cpp:15: error: ‘cvCreateMemStorage’ was not declared in this scope
prog.cpp:16: error: ‘cvCaptureFromCAM’ was not declared in this scope
prog.cpp:17: error: ‘cvNamedWindow’ was not declared in this scope
prog.cpp:25: error: ‘cvGrabFrame’ was not declared in this scope
prog.cpp:27: error: ‘cvRetrieveFrame’ was not declared in this scope
prog.cpp:33: error: ‘cvSize’ was not declared in this scope
prog.cpp:33: error: ‘IPL_DEPTH_8U’ was not declared in this scope
prog.cpp:33: error: ‘cvCreateImage’ was not declared in this scope
prog.cpp:36: error: ‘IPL_ORIGIN_TL’ was not declared in this scope
prog.cpp:37: error: ‘cvCopy’ was not declared in this scope
prog.cpp:40: error: ‘cvFlip’ was not declared in this scope
prog.cpp:43: error: ‘cascade’ was not declared in this scope
prog.cpp:43: error: ‘CvHaarClassifierCascade’ was not declared in this scope
prog.cpp:43: error: expected primary-expression before ‘)’ token
prog.cpp:43: error: expected `;' before ‘cvLoad’
prog.cpp:48: error: ‘stderr’ was not declared in this scope
prog.cpp:48: error: ‘fprintf’ was not declared in this scope
prog.cpp:52: error: ‘detect_and_draw’ cannot be used as a function
prog.cpp:56: error: ‘cvWaitKey’ was not declared in this scope
prog.cpp:64: error: ‘cascade’ was not declared in this scope
prog.cpp:64: error: ‘cvReleaseHaarClassifierCascade’ was not declared in this scope
prog.cpp:66: error: ‘cvReleaseImage’ was not declared in this scope
prog.cpp:67: error: ‘cvReleaseCapture’ was not declared in this scope
prog.cpp:68: error: ‘cvReleaseMemStorage’ was not declared in this scope
prog.cpp:14: warning: unused variable ‘input_name’
prog.cpp: At global scope:
prog.cpp:77: error: redefinition of ‘bool detect_and_draw’
prog.cpp:6: error: ‘bool detect_and_draw’ previously defined here
prog.cpp:77: error: ‘IplImage’ was not declared in this scope
prog.cpp:77: error: ‘img’ was not declared in this scope
prog.cpp:77: error: ‘CvHaarClassifierCascade’ was not declared in this scope
prog.cpp:77: error: ‘cascade’ was not declared in this scope
stdout
Standard output is empty