/************************************************************************
* Copyright(c) 2011 Yang Xian
* All rights reserved.
*
* File: handGestureDetection.cpp
* Brief: 手势检测
* Version: 1.0
* Author: Yang Xian
* Email: xyang2011@sinano.ac.cn
* Date: 2011/12/01
* History:
************************************************************************/
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay( Mat frame );
/** Global variables */
String face_cascade_name = "hand.xml";
CascadeClassifier CHandCascade;
string window_name = "Hand Gesture Detection";
int main(int argc, const char** argv)
{
Mat frame;
//-- 1. Load the cascades
if( !CHandCascade.load( face_cascade_name ) )
{
printf("--(!)Error loading\n");
return -1;
}
//-- 2. Read the video stream
CvCapture* capture = cvCaptureFromCAM( -1 );
// VideoCapture capture("hand.avi");
if(/*capture.isOpened()*/capture)
{
while( true )
{
frame = cvQueryFrame( capture );
// capture >> frame;
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{
detectAndDisplay( frame );
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
int c = waitKey(10);
if((char)c == 27)
{
break;
}
}
}
return 0;
}
/**
* @function detectAndDisplay
*/
void detectAndDisplay( Mat frame )
{
std::vector<Rect> hands;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
CHandCascade.detectMultiScale(frame_gray, hands, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, Size(40, 40));
for(size_t i=0; i<hands.size(); i++)
{
rectangle(frame, hands[i], Scalar(255,0,255));
}
//-- Show what you got
imshow( window_name, frame );
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页