Hand detection using openCV


In this post, as the progress goes, the hand detection technologies using openCV is introduced. As the pictures inllustrated below, this detection method is independent  with distance and background ( just not the background full of hands ), and the main segment method is color abstract which means getting the hand color pixels filtered. As the result tested within several environments, it workd well except slightly noise varying. But the noise is easy to take off.

The tested source code file is offered underneath the illustrations, feel free to use it.


SOURCE FILE HERE:  handDetection.CPP
Related post which may interest you:
Hand gesture detection and recognition


Revised by Andol on 28 Oct 2013

As asked by Niaz:

Among these or outside of these which method you think is the possible best approach to take.

1. Skin tone detection (Adaptive skin detection: opencv)
2. Blob detection (cvBlob: opencv)
3. Haar cascade classification
4. Feature-based scale invariant hand detection (using something like openSURF, SIFT, RANSAAC etc)

My anser is HAAR CASCADE CLASSIFICATION detection is the first choice, it take a lot time to work out the classifier though. Based on my experiences, if the background is clean and simple then skin colour detection is also a good choice;

while blob detection has generally lower detection accuracy in hand recognition; lastly the feature-based detection is only a choice for rigid object matching, that is, hands have different combination of features such like finger length and the ration between fingers and palm, that is not suitable for feature detection – as these hand features change all the time.


Author: Andol Li

A HCI researcher, a digital media lecturer, an information product designer, and a python/php/java coder.

91 Comments On “ Hand detection using openCV”

  1. Hello, I need your help on hand detection in OpenCV, I am quite poor at computer programing and I want to use this technique to control a robot eye. I have successfully managed to acquire and capture the image from my web cam and now need to move on to the next step. I was told i have to use the Haar libraries and the same concept of Face detection by my professors. What should i do? Thank you

    • well, if haar is adopted to detect the hand’s gestures, the first thing you should do is to capture enough number of samples including positive and negative. Then you can train your application according to these samples you have gathered. for more details, it is possible to refer the machine learning part of openCV.

  2. hi i would like to control cursor movements by using hand so please help me to find coordinates of hand..please help me in this urgent

  3. hi thanks for this i had done still but i dont know how to proceed again (ie) to detect the gesture and perform mouse clicks help me please

    • once you segment clean hand contours, the next step would be detect the convexities by using a function in openCV called ‘CvConvexityDefect’ to get the hand’s features.

      • hi andol.i am doing work on computer control using fingure movement.i have done worke till by decteing fingure movement of pointer can you plz help me how in i perform left and right click?

        • @spiffy
          there needs two distinguishable gestures that are associated with left and right click. In that case, left and right clicks may need two other finger gestures, such like bending a finger.

  4. Hi, i got your code.This is working well but i want to find the hand location.I have used contour but still can not find hand location.can your help me?plz

  5. Hi Andol,
    After detecting the hand, Is there a way to connect the mouse pointer to coordinate(x,y)? I am using Ubuntu. What command should I use to do so?

  6. hi Andol
    i have to control a robotic car by fingers i hv saw ur code.it working for hand exraction.nw i hve to count of fingers in a particular moment.plaese help me wat i do next..

  7. Hello Andol, thanks for the advice, your helping me on my way but i have a few questions.

    Im trying to make an application that responds to hand gestures. At the moment all I want to do it figure out where (in terms of x,y) the center of the hand is in the image and how many fingers are being held up.

    So once I have the black and white image you show in this post, how do i then draw areas round them and get the .Center values from it ? I see in another one of your posts you have managed to draw a very precise contour round the hand, how did you manage that ? I know you only do OpenCV and not Emgu but if you show me the specific method calls you would use I can probably figure out the Emgu equivalents.

    Thank you for your time.

    Also, I see a few people asking how to do it in the Emgu CV version. Well…

    Capture camera = new Capture()
    Image originalImage = camera.QueryFrame() ; // Raw image

    /* All the colour segmentation is done in Hsv not Bgr so we need a Hsv version of our image */
    Image hsvImage = originalImage.convert();

    /* Now we need to define the upper (max) and lower (min) Hsv values that we want to extract from the image */
    Hsv hsv_min = new Hsv(127, 66, 57);
    Hsv hsv_max = new Hsv(183, 203, 124);

    /* Note: I am trying to detect hands in my webcam image and in a dark room, you will need to play around with the parameters in hsv_min & hsv_max to find the colours you want to extract

    Now, the process of checking if pixels are in range of these min and max values results in a greyscale image, so we'll need one of those. */

    Image greyedImage = hsvImage.InRange(hsv_min, hsv_max);

    /* BAM ! A greyscale image containing only the pixels with Hsv values between the hsv_min & hsv_max ranges. *.

    • Hi Finch, after ive got the while-black result, i use:
      “cvSmooth” to reduce the noises, “cvCandy” to extract lines, “cvFindContours” to extract the most possible hand contours, once i ve got the right contours, i use “cvBoundingRect” to locate the position of hand as a rectangle, then draw out the rectangle which is the picture you see. Or you can refer here for detail codes here http://download.andol.me/hsv.cpp. PS: thanks for your codes sharing.

    • just if we use this value for hsv
      Hsv hsv_min = new Hsv(0, 30, 80);
      Hsv hsv_max = new Hsv(20, 150, 255);

      instead of this
      Hsv hsv_min = new Hsv(127, 66, 57);
      Hsv hsv_max = new Hsv(183, 203, 124);

      it works fine…

        • cheers too….. and is there any HaarCascade training set for detecting hand, i searched that many times, but i got nothing….. if there is anything means just help me out, because detecting hand using skin color does not satisfy the result…

          hearing for your reply……….

    • Hi Bon, i am not sure if the hand detection algorithm fits fire detection by openCV, though thay have some features in common. You can have a try using hand detection algorithm with some improvements according to fire features.

  8. Sir, i know this code works for a live webcam feed, how do you make it work for an input of still image and .avi video files only

    • hi JP Talusan, if you had had a look at the source loading codes in opencv, you would find that it is able to load live webcam images, still images, and .avi video files. Here is a simple instance:
      #include “cv.h”
      #include “highgui.h”
      int main( int argc, char** argv )
      IplImage* pImg;
      if( argc == 2 &&
      (pImg = cvLoadImage( argv[1], 1)) != 0 ) //argv[1] is a parameter of image path.
      cvNamedWindow( “Image”, 1 );
      cvShowImage( “Image”, pImg );
      cvDestroyWindow( “Image” );
      cvReleaseImage( &pImg );
      return 0;
      return -1;

  9. Hi,

    First of all,great work. I found it quite useful.I am currently doing a similar thing although on a depth based camera which does not have colour..

    Right now I have the contour of the hand and the arm and its a motion tracking system. I now need to cut the hand from the arm and use the hand to control a cursor on screen. Could you give me an idea as to how i should separate the hand and how to control a cursor?

  10. Hi,
    Do you know some free (natural) hand gesture or whole human or upper body database?
    I read about the Massey Gesture Database, but the link does not work 🙁
    Help me please.

  11. Dear Andol I m working on gesture recognition i need to detect and track the hand so which kind of method is beneficial for me if u have code plz send as well.

  12. Thanx dear but still didnt find appropriate way. Is there any one knows about the handshpe date base use for for tracking? waiting for reply.

  13. Hi Andol,
    I need to detect the tips of the fingers…Can you help me with the code for this… I want to know which finger is visible. Please let me know if this is possible. I am new at opencv so any help would be great

Leave a comment
Due to technical adjustments, the comment function is shortly closed and will be re-openning soon. Thanks.

Copyrights 2006-2018 © All rights reserved
Theme Tree2, re-designed by Andol Li, powered by WordPress and Bootsrap
WWW.ANDOL.ME | 浙ICP备15040508号-1
公安备案图标 浙公网安备33010602004018号
Back to top