Guest User

Untitled

a guest
Jan 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. under c/c++-->general-->additional include directories-->C:OpenCV2.3buildinclude;C:OpenCV2.3buildincludeopencv2;C:OpenCV2.3buildincludeopencv;C:OpenCV2.3opencvdatahaarcascades
  2.  
  3. under linker-->general-->additional library directories-->C:OpenCV2.3buildx86vc9lib;C:OpenCV2.3opencvdatahaarcascades;%(AdditionalLibraryDirectories)
  4.  
  5. under linker-->input-->additional dependencies--> added opencv_core230.lib;opencv_highgui230.lib;opencv_objdetect230.lib
  6.  
  7. under debugging-->command arguments -->added --cascade="C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml"testimg.jpg
  8.  
  9. #include <cv.h>
  10. #include <highgui.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include <math.h>
  16. #include <float.h>
  17. #include <limits.h>
  18. #include <time.h>
  19. #include <ctype.h>
  20.  
  21. // Create memory for calculations
  22. static CvMemStorage* storage = 0;
  23.  
  24. // Create a new Haar classifier
  25. static CvHaarClassifierCascade* cascade = 0;
  26.  
  27. // Function prototype for detecting and drawing an object from an image
  28. void detect_and_draw( IplImage* image );
  29.  
  30. // Create a string that contains the cascade name
  31. const char* cascade_name = "haarcascade_frontalface_alt.xml";
  32. /* "haarcascade_profileface.xml";*/
  33.  
  34. // Main function, defines the entry point for the program.
  35. int main( int argc, char** argv )
  36. {
  37. // Structure for getting video from camera or avi
  38. CvCapture* capture = 0;
  39.  
  40. // Images to capture the frame from video or camera or from file
  41. IplImage *frame, *frame_copy = 0;
  42. /* IplImage* img = cvLoadImage( "testimg.jpg" );
  43. cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
  44. cvShowImage("MyJPG", img);
  45. cvWaitKey(0);
  46. cvReleaseImage( &img );
  47. cvDestroyWindow( "MyJPG" );
  48. */
  49. // Used for calculations
  50. int optlen = strlen("--cascade=");
  51.  
  52. // Input file name for avi or image file.
  53. const char* input_name;
  54.  
  55. // Check for the correct usage of the command line
  56. if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
  57. {
  58. cascade_name = argv[1] + optlen;
  59. input_name = argc > 2 ? argv[2] : 0;
  60. }
  61. else
  62. {
  63. fprintf( stderr,
  64. "Usage: facedetect --cascade="<cascade_path>" [filename|camera_index]n" );
  65. return -1;
  66. /*input_name = argc > 1 ? argv[1] : 0;*/
  67. }
  68.  
  69. // Load the HaarClassifierCascade
  70. cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
  71.  
  72. // Check whether the cascade has loaded successfully. Else report and error and quit
  73. if( !cascade )
  74. {
  75. fprintf( stderr, "ERROR: Could not load classifier cascaden" );
  76. return -1;
  77. }
  78.  
  79. // Allocate the memory storage
  80. storage = cvCreateMemStorage(0);
  81.  
  82. // Find whether to detect the object from file or from camera.
  83. if( !input_name || (isdigit(input_name[0]) && input_name[1] == '') )
  84. capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
  85. else
  86. capture = cvCaptureFromAVI( input_name );
  87.  
  88. // Create a new named window with title: result
  89. cvNamedWindow( "result", 1 );
  90.  
  91. // Find if the capture is loaded successfully or not.
  92.  
  93. // If loaded succesfully, then:
  94. if( capture )
  95. {
  96. // Capture from the camera.
  97. for(;;)
  98. {
  99. // Capture the frame and load it in IplImage
  100. if( !cvGrabFrame( capture ))
  101. break;
  102. frame = cvRetrieveFrame( capture );
  103.  
  104. // If the frame does not exist, quit the loop
  105. if( !frame )
  106. break;
  107.  
  108. // Allocate framecopy as the same size of the frame
  109. if( !frame_copy )
  110. frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
  111. IPL_DEPTH_8U, frame->nChannels );
  112.  
  113. // Check the origin of image. If top left, copy the image frame to frame_copy.
  114. if( frame->origin == IPL_ORIGIN_TL )
  115. cvCopy( frame, frame_copy, 0 );
  116. // Else flip and copy the image
  117. else
  118. cvFlip( frame, frame_copy, 0 );
  119.  
  120. // Call the function to detect and draw the face
  121. detect_and_draw( frame_copy );
  122.  
  123. // Wait for a while before proceeding to the next frame
  124. if( cvWaitKey( 10 ) >= 0 )
  125. break;
  126. }
  127.  
  128. // Release the images, and capture memory
  129. cvReleaseImage( &frame_copy );
  130. cvReleaseCapture( &capture );
  131. }
  132.  
  133. // If the capture is not loaded succesfully, then:
  134. else
  135. {
  136. // Assume the image to be lena.jpg, or the input_name specified
  137. const char* filename = input_name ? input_name : (char*)"testimg.jpg";
  138.  
  139. // Load the image from that filename
  140. IplImage* image = cvLoadImage( filename, 1 );
  141.  
  142. // If Image is loaded succesfully, then:
  143. if( image )
  144. {
  145. // Detect and draw the face
  146. detect_and_draw( image );
  147.  
  148. // Wait for user input
  149. cvWaitKey(0);
  150.  
  151. // Release the image memory
  152. cvReleaseImage( &image );
  153. }
  154. else
  155. {
  156. /* assume it is a text file containing the
  157. list of the image filenames to be processed - one per line */
  158. FILE* f = fopen( filename, "rt" );
  159. if( f )
  160. {
  161. char buf[1000+1];
  162.  
  163. // Get the line from the file
  164. while( fgets( buf, 1000, f ) )
  165. {
  166.  
  167. // Remove the spaces if any, and clean up the name
  168. int len = (int)strlen(buf);
  169. while( len > 0 && isspace(buf[len-1]) )
  170. len--;
  171. buf[len] = '';
  172.  
  173. // Load the image from the filename present in the buffer
  174. image = cvLoadImage( buf, 1 );
  175.  
  176. // If the image was loaded succesfully, then:
  177. if( image )
  178. {
  179. // Detect and draw the face from the image
  180. detect_and_draw( image );
  181.  
  182. // Wait for the user input, and release the memory
  183. cvWaitKey(0);
  184. cvReleaseImage( &image );
  185. }
  186. }
  187. // Close the file
  188. fclose(f);
  189. }
  190. }
  191. }
  192.  
  193. // Destroy the window previously created with filename: "result"
  194. cvDestroyWindow("result");
  195.  
  196. // return 0 to indicate successfull execution of the program
  197. return 0;
  198. }
  199.  
  200. // Function to detect and draw any faces that is present in an image
  201. void detect_and_draw( IplImage* img )
  202. {
  203. int scale = 1;
  204.  
  205. // Create a new image based on the input image
  206. IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
  207.  
  208. // Create two points to represent the face locations
  209. CvPoint pt1, pt2;
  210. int i;
  211.  
  212. // Clear the memory storage which was used before
  213. cvClearMemStorage( storage );
  214.  
  215. // Find whether the cascade is loaded, to find the faces. If yes, then:
  216. if( cascade )
  217. {
  218. // There can be more than one face in an image. So create a growable sequence of faces.
  219. // Detect the objects and store them in the sequence
  220. CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
  221. 1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
  222. cvSize(40, 40) );
  223.  
  224. // Loop the number of faces found.
  225. for( i = 0; i < (faces ? faces->total : 0); i++ )
  226. {
  227. // Create a new rectangle for drawing the face
  228. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  229.  
  230. // Find the dimensions of the face,and scale it if necessary
  231. pt1.x = r->x*scale;
  232. pt2.x = (r->x+r->width)*scale;
  233. pt1.y = r->y*scale;
  234. pt2.y = (r->y+r->height)*scale;
  235.  
  236. // Draw the rectangle in the input image
  237. cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
  238. }
  239. }
  240.  
  241. // Show the image in the window named "result"
  242. cvShowImage( "result", img );
  243.  
  244. // Release the temp image created.
  245. cvReleaseImage( &temp );
  246. }
Add Comment
Please, Sign In to add comment