-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.cpp
88 lines (80 loc) · 2.3 KB
/
final.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//============================================================================
// Name : opencv_handdetect.cpp
// Author : andol li, andol@andol.info
// Version : 0.1
// Copyright : 2012
// Description : using haartraining results to detect the hand gesture of FIST in video stream.
//
//============================================================================
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
const double scale = 1.1;
//1.0 api version
CvMemStorage* storage = 0;
CvHaarClassifierCascade* cascade = 0;
void detectAndDraw(IplImage *input_image);
const char* cascade_name = "palm.xml";
//define the path to cascade file
int main()
{
//1.0 api version
CvCapture *capture =0;
IplImage *frame, *frame_copy = 0;
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
if( !cascade ){
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
storage = cvCreateMemStorage(0);
capture = cvCaptureFromCAM(0);
cvNamedWindow("result", 1);
if(capture){
for(;;){
if(!cvGrabFrame(capture)) break;
frame = cvRetrieveFrame( capture);
if(!frame) break;
if(!frame_copy) frame_copy = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, frame->nChannels);
if(frame->origin == IPL_ORIGIN_TL)
cvCopy(frame, frame_copy, 0);
else
cvFlip(frame, frame_copy, 0);
detectAndDraw(frame_copy);
if(cvWaitKey(10) >= 0) break;
}
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
}
return 0;
}
void detectAndDraw(IplImage *img)
{
double scale = 1.1;
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
CvPoint pt1, pt2;
int i;
cvClearMemStorage( storage );
if(cascade){
CvSeq* faces = cvHaarDetectObjects(
img,
cascade,
storage,
scale, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(24, 24) );
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
cvRectangle( img, pt1, pt2, CV_RGB(200, 0, 0), 1, 8, 0 );
}
}
cvShowImage("result", img);
cvReleaseImage( &temp );
}