Idea: Use trained palm. XML and fist. the XML file uses the cascadeclassifier of opencv to detect the palm and fist for each frame of the image, and then clusters the palm and fist detected in multiple frames, the region that meets the grouping conditions is the final detection result.
Code:
#include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> using namespace std; using namespace cv; /** Function Headers */ void detectAndDisplay( Mat frame ); void RestoreVectors(vector<vector<Rect>>& vecs_bank, vector<Rect>& vecAll); /** Global variables */ String palm_cascade_name = "palm.xml"; String fist_cascade_name = "fist.xml"; CascadeClassifier palm_cascade; CascadeClassifier fist_cascade; string window_name = "Capture - Palm and fist detection"; /** @function main */ int main( int argc, const char** argv ) { CvCapture* capture; Mat frame; //-- 1. Load the cascades if( !palm_cascade.load( palm_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }; if( !fist_cascade.load( fist_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }; //-- 2. Read the video stream capture = cvCaptureFromCAM( -1 ); if( capture ) { while( true ) { frame = cvQueryFrame( capture ); //-- 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 == ‘q‘ || (char)c == ‘Q‘ || 27 == c) { break; } } } cvReleaseCapture(&capture); return 0; }/** @function detectAndDisplay */void detectAndDisplay( Mat frame ){ std::vector<Rect> faces; std::vector<Rect> palms; std::vector<Rect> fists; static vector<vector<Rect>> palms_bank; static vector<vector<Rect>> fists_bank; const int MAX_NUM = 3; Mat frame_gray; cvtColor( frame, frame_gray, CV_BGR2GRAY ); equalizeHist( frame_gray, frame_gray ); //-- Palm detection palm_cascade.detectMultiScale( frame_gray, palms, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); palms_bank.push_back(palms); if(palms_bank.size() > MAX_NUM) palms_bank.erase(palms_bank.begin()); vector<Rect> palmAll; RestoreVectors(palms_bank, palmAll); groupRectangles(palmAll, 2); for( size_t j = 0; j < palmAll.size(); j++ ) { rectangle(frame, palmAll[j], Scalar(0,255,0), 2); } //-- Fist detection fist_cascade.detectMultiScale( frame_gray, fists, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); fists_bank.push_back(fists); if(fists_bank.size() > MAX_NUM) fists_bank.erase(fists_bank.begin()); vector<Rect> fistAll; RestoreVectors(fists_bank, fistAll); groupRectangles(fistAll, 2); for( size_t j = 0; j < fistAll.size(); j++ ) { rectangle(frame, fistAll[j], Scalar(0,0,255), 2); } //-- Show what you got imshow( window_name, frame ); }void RestoreVectors(vector<vector<Rect>>& vecs_bank, vector<Rect>& vecAll){ for(size_t i = 0; i < vecs_bank.size(); i++){ vecAll.insert(vecAll.end(), vecs_bank[i].begin(), vecs_bank[i].end()); }}
Refer:
[1] grouprectangles instructions
[2] for palm. xml and fist. xml
[3] opencv sample code for face and eye detection
Code, the palm. xml and fist. XML files, instructions can be downloaded from here:
Http://download.csdn.net/detail/lichengyu/7751671
Grouprectangles
Groups the object candidate rectangles.
-
C ++: Void Grouprectangles (Vector <rect> &
Rectlist, Int
Groupthreshold, Double
EPS= 0.2 )
-
C ++: Void Grouprectangles (Vector <rect> &
Rectlist, Vector <int> &
Weights, Int
Groupthreshold, Double
EPS= 0.2 )
-
Python: Cv2. Grouprectangles (Rectlist, groupthreshold [, EPS] )→ Rectlist, weights
-
Parameters: |
- Rectlist-Input/output vector of rectangles. output vector contains des retained and grouped rectangles. (The Python list is not modified in place .)
- Groupthreshold-Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
- EPS-Relative difference between sides of the rectangles to merge them into a group.
|
The function is a wrapper for the generic FunctionPartition (). It clusters all the input rectangles using the rectangle equivalence criteria that combines rectangles with similar sizes and similar locations. The similarity is definedEPS. WhenEPS = 0, No clustering is done at all. If, all the rectangles are put in one cluster. Then, the small clusters containing less than or equalGroupthresholdRectangles are rejected. In each other cluster, the average rectangle is computed and put into the output rectangle list.
Original article: http://blog.csdn.net/lichengyu/article/details/38544189
Use opencv to detect palm and fist)