Created
March 6, 2010 14:48
-
-
Save baris/323720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// opencv | |
#include <highgui.h> | |
#include <cv.h> | |
const char* cascade_path = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"; | |
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad(cascade_path, 0, 0, 0 ); | |
//CvHaarClassifierCascade* cascade = cvLoadHaarClassifierCascade(cascade_path, cvSize(1,1)); | |
CvMemStorage* storage = cvCreateMemStorage( 0 ); | |
void | |
detect_and_draw( IplImage* img, double scale = 1.4 ) | |
{ | |
IplImage* gray = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 ); | |
IplImage* small_img = cvCreateImage( | |
cvSize(cvRound(img->width/scale), cvRound(img->height/scale)), | |
IPL_DEPTH_8U, | |
1 ); | |
cvCvtColor( img, gray, CV_BGR2GRAY ); | |
cvResize( gray, small_img, CV_INTER_LINEAR ); | |
cvEqualizeHist( small_img, small_img ); | |
// Detect objects | |
cvClearMemStorage( storage ); | |
CvSeq* objects = cvHaarDetectObjects( small_img, | |
cascade, | |
storage, | |
1.1, | |
2, | |
0, | |
cvSize(30, 30) ); | |
// loop through found objects and draw boxes around them | |
CvScalar color[] = {{255,0,0}, {0,255,0}}; | |
/* | |
// if ( objects->total < 2 ) { | |
// return; | |
// } | |
CvRect* face_rec1 = (CvRect*) cvGetSeqElem( objects, 0 ); | |
CvMat *face_mat1, x; | |
face_mat1 = cvGetSubRect(img, | |
&x, | |
cvRect(face_rec1->x, face_rec1->y, face_rec1->width, face_rec1->height)); | |
IplImage* face_img1 = cvCreateImage( cvGetSize(&x), IPL_DEPTH_8U, 1 ); | |
cvCopy(face_mat1, face_img1); | |
// CvRect* face_rec2 = (CvRect*) cvGetSeqElem( objects, 1 ); | |
// cvGetSubRect(img, | |
// face_rec2, | |
// cvRect(face_rec2->x, face_rec2->y, face_rec2->width, face_rec2->height)); | |
*/ | |
for( int i = 0; i < (objects ? objects->total : 0); i++ ) { | |
CvRect* rec = (CvRect*) cvGetSeqElem( objects, i ); | |
int x = rec->x * scale; int x2 = (rec->x + rec->width) * scale; | |
int y = rec->y * scale; int y2 = (rec->y + rec->height) * scale; | |
cvRectangle( img, | |
cvPoint(x, y), | |
cvPoint(x2, y2), | |
color[i % 2]); | |
} | |
cvReleaseImage( &gray ); | |
cvReleaseImage( &small_img ); | |
} | |
/* VIDEO */ | |
int | |
main( int argc, char** argv ) | |
{ | |
cvNamedWindow( "Example", CV_WINDOW_AUTOSIZE ); | |
CvCapture* capture; | |
capture = cvCreateCameraCapture( 0 ); | |
// capture = cvCreateFileCapture( argv[1] ); | |
IplImage* frame; | |
while(1) { | |
frame = cvQueryFrame( capture ); | |
if ( !frame ) continue; | |
/* edge detection | |
IplImage* out = cvCreateImage( cvGetSize(frame) , IPL_DEPTH_8U, 1 ); | |
IplImage* gray = cvCreateImage( cvGetSize(frame) , IPL_DEPTH_8U, 1 ); | |
cvCvtColor( frame, gray, CV_BGR2GRAY ); | |
cvCanny( gray, out, 10, 100, 3 ); | |
cvShowImage( "Example", out ); | |
cvReleaseImage( &out ); | |
cvReleaseImage( &gray ); | |
*/ | |
detect_and_draw( frame ); | |
cvShowImage( "Example", frame ); | |
char c = cvWaitKey( 33 ); | |
if ( c == 27 ) break; | |
} | |
cvReleaseCapture( &capture ); | |
cvDestroyWindow ( "Example" ); | |
return 0; | |
} | |
/* IMAGE | |
int | |
main( int argc, char** argv ) | |
{ | |
IplImage* img = cvLoadImage( argv[1] ); | |
cvNamedWindow( "Example", CV_WINDOW_AUTOSIZE ); | |
IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 ); | |
cvSmooth( img, out, CV_GAUSSIAN, 9, 9 ); | |
cvShowImage( "Example", out ); | |
cvWaitKey( 0 ); | |
cvReleaseImage( &img ); | |
cvDestroyWindow( "Example" ); | |
return 0; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment