GECKO 1.0
Human-computer interface based on hand gesture recognition
HandDescriptor.h
00001 //------------------------------------------------------------------------------
00002 //-- HandDescriptor
00003 //------------------------------------------------------------------------------
00004 //--
00005 //-- Finds the main characteristics of the hand from a binary image containing
00006 //-- a hand silouette.
00007 //--
00008 //------------------------------------------------------------------------------
00009 //--
00010 //-- This file belongs to the "Gecko - Gesture Recognition" project
00011 //-- (https://github.com/David-Estevez/gecko)
00012 //--
00013 //------------------------------------------------------------------------------
00014 //-- Authors: David Estevez Fernandez
00015 //--          Irene Sanz Nieto
00016 //--
00017 //-- Released under the GPL license (more info on LICENSE.txt file)
00018 //------------------------------------------------------------------------------
00019 
00029 #ifndef HAND_DESCRIPTOR
00030 #define HAND_DESCRIPTOR
00031 
00032 #include <opencv2/opencv.hpp>
00033 #include "handUtils.h"
00034 #include "mouse.h"
00035 
00036 //-- Constants for the gesture recognition
00037 //-----------------------------------------------------------------------
00039 static const unsigned int GECKO_GESTURE_NONE = 0;
00041 static const unsigned int GECKO_GESTURE_OPEN_PALM = 1;
00043 static const unsigned int GECKO_GESTURE_CLOSED_FIST = 2;
00045 static const unsigned int GECKO_GESTURE_VICTORY = 3;
00047 static const unsigned int GECKO_GESTURE_GUN = 4;
00048 
00049 
00057 class HandDescriptor
00058 {
00059 
00060 public:
00061     //-- Constructor
00062     //-----------------------------------------------------------------------
00064     HandDescriptor();
00065 
00066 
00067     //-- Refresh the detected hand characteristics
00068     //-----------------------------------------------------------------------
00076     void operator ()(const cv::Mat& skinMask );
00077 
00084     void update(const cv::Mat& skinMask );
00085 
00086 
00087     //-- Get the characteristics of the hand:
00088     //-----------------------------------------------------------------------
00090     bool handFound();
00091 
00093     double getHandAngle();
00095     double getHandAnglePredicted();
00097     double getHandAngleEstimated();
00098 
00100     cv::Point getCenterHand();
00102     cv::Point getCenterHandPredicted();
00104     cv::Point getCenterHandEstimated();
00105 
00107     std::vector< std::vector<cv::Point> > getContours();
00108 
00110     cv::Rect getBoundingBox();
00111 
00113     int getGesture();
00114 
00116     int getNumFingers();
00117 
00118 
00119     //-- Plot characteristics on some image:
00120     //--------------------------------------------------------------------------
00122     void plotBoundingRectangle(const cv::Mat& src, cv::Mat& dst, bool rotated = true);
00123 
00125     void plotContours(const cv::Mat& src, cv::Mat& dst );
00126 
00128     void plotCenter( const cv::Mat& src, cv::Mat& dst, bool show_corrected = true,  bool show_actual = true, bool show_predicted = true );
00129 
00131     void angleControl( bool show_corrected = true,  bool show_actual = true, bool show_predicted = true );
00132 
00134     void plotMaxInscribedCircle( cv::Mat& src, cv::Mat& dst, bool show_center = true, cv::Scalar color = cv::Scalar(0, 255, 0), int thickness = 1 );
00135 
00137     void plotMinEnclosingCircle(cv::Mat& src, cv::Mat& dst, bool show_center = true, cv::Scalar color = cv::Scalar(255,0,255), int thickness = 1);
00138 
00140     void plotComplexHull(cv::Mat& src, cv::Mat& dst, bool show_points = false, cv::Scalar color = cv::Scalar( 0, 255, 255), int thickness = 1);
00141 
00143     void plotConvexityDefects(cv::Mat& src, cv::Mat& dst, bool draw_points = true);
00144 
00146     void plotFingertips( cv::Mat& src, cv::Mat& dst, bool draw_lines = true, cv::Scalar color = cv::Scalar( 255, 0, 0) , int thickness = 2);
00147 
00149     void plotHandInterface( cv::Mat& src, cv::Mat& dst );
00150 
00151 private:
00152     //-- Functions that extract characteristics:
00153     //--------------------------------------------------------------------------
00163     void contourExtraction(const cv::Mat& skinMask);
00164 
00166     void boundingBoxExtraction();
00167 
00169     void handPalmExtraction();
00170 
00174     void ROIExtraction( const cv::Mat& src);
00175 
00177     void defectsExtraction();
00178 
00180     void fingerExtraction();
00181 
00183     void angleExtraction();
00184 
00186     void centerExtraction();
00187 
00189     void gestureExtraction();
00190 
00191 
00192     //-- Parameters that describe the hand:
00193     //--------------------------------------------------------------------------
00195     bool _hand_found;
00196 
00197 
00199     double _hand_angle;
00200 
00202     double _hand_angle_prediction;
00203 
00205     double _hand_angle_estimation;
00206 
00207 
00209     cv::Point _hand_center;
00210 
00212     cv::Point _hand_center_prediction;
00213 
00215     cv::Point _hand_center_estimation;
00216 
00217 
00219     std::vector< std::vector<cv::Point> > _hand_contour;
00220 
00221 
00223     cv::RotatedRect _hand_rotated_bounding_box;
00224 
00226     cv::Rect _hand_bounding_box;
00227 
00228 
00230     int _hand_gesture;
00231 
00232 
00233 
00235     int _hand_num_fingers;
00236 
00238     std::vector< cv::Point > _hand_fingertips;
00239 
00241     std::vector< cv::Point > _hand_finger_line_origin;
00242 
00243 
00244     //-- Describe hand palm:
00245     //-------------------------------------------------------------------------
00247     double _max_circle_inscribed_radius;
00248 
00250     cv::Point _max_circle_incribed_center;
00251 
00253     float _min_enclosing_circle_radius;
00254 
00256     cv::Point2f _min_enclosing_circle_center;
00257 
00258 
00260     std::vector< cv::Point > _hand_hull;
00261 
00263     std::vector< ConvexityDefect > _hand_convexity_defects;
00264 
00265 
00267     cv::Mat _hand_ROI;
00268 
00269 
00270     //-- Kalman filters for smoothing:
00271     //---------------------------------------------------------------------
00273     cv::KalmanFilter kalmanFilterAngle;
00274 
00276     cv::KalmanFilter kalmanFilterCenter;
00277 
00278 
00279 };
00280 
00281 
00282 #endif // HAND_DESCRIPTOR
 All Classes Functions Variables