Skin Detection in C++ using OpenCV

Today I am going to share how to detect the human skin using YCbCr color space in OpenCV. It is very easy. Here I made an class that will detect the skin.


#pragma once
using namespace std;
class SkinDetector

cv::Mat getSkin(cv::Mat input);

int Y_MIN;
int Y_MAX;
int Cr_MIN;
int Cr_MAX;
int Cb_MIN;
int Cb_MAX;

// end of SkinDetector.h file


#include "SkinDetector.h"

//YCrCb threshold
// You can change the values and see what happens
Y_MIN  = 0;
Y_MAX  = 255;
Cr_MIN = 133;
Cr_MAX = 173;
Cb_MIN = 77;
Cb_MAX = 127;


//this function will return a skin masked image
cv::Mat SkinDetector::getSkin(cv::Mat input)
cv::Mat skin;
//first convert our RGB image to YCrCb

//uncomment the following line to see the image in YCrCb Color Space
//cv::imshow("YCrCb Color Space",skin);

//filter the image in YCrCb color space

return skin;
//end of SkinDetector.cpp file

Here is the main program to use this class to detect skin. Here image will be captured from the webcam and show the masked image. If you want to masked from video file then you have change the;


Source :

using namespace std;
using namespace cv;

int main()
VideoCapture capture;
//open capture object at location zero (default location for webcam);

//set height and width of capture frame

Mat cameraFeed;

SkinDetector mySkinDetector;

Mat skinMat;

//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop

//store image to matrix;

//show the current image
imshow("Original Image",cameraFeed);

skinMat= mySkinDetector.getSkin(cameraFeed);

imshow("Skin Image",skinMat);

return 0;


About Razib Chandra Deb

I completed my BSc in CSE from CSE Department of KUET. Currently I am working as a Software Engineer in Samsung R&D Institute Bangladesh(SRBD). I am interested in image processing.
This entry was posted in Image Processing and tagged , , , , , , . Bookmark the permalink.

25 Responses to Skin Detection in C++ using OpenCV

  1. Al says:

    I tried your code and it works great. The camera is able to track my hand. That is awesome.
    How did you come with the right MIN and MAX values for Y, Cr, and Cb to match the skin color? Is there a color table for the YCrCb color values? Good job and thanks for posting this!

    • There are some papers who says there MIN MAX is good. It depends on the environment also. If you find any good share with us.

      • Leonardo GZ says:

        If you dont mind i would like to know the names of those papers so I could use them as reference

    • pipitito says:

      thank you for the code and this is the famous paper where they found the values ^^
      Chai, Douglas, and King N. Ngan. “Face segmentation using skin-color map in videophone applications.” Circuits and Systems for Video Technology, IEEE Transactions on 9.4 (1999): 551-564.

  2. Ray says:

    Where shall I put those files (SkinDetector.h, and the SkinDetector.cpp)? I have put the SkinDetector.h with the other libraries, but I do not know where to locate the file SkinDetector.cpp. I get the following error:

    Source.cpp:(.text+0xad): undefined reference to `SkinDetector::SkinDetector()’
    Source.cpp:(.text+0x170): undefined reference to `SkinDetector::getSkin(cv::Mat)’
    Source.cpp:(.text+0x2cd): undefined reference to `SkinDetector::~SkinDetector()

    Thanks a lot!

    • put them in the same folder of your source and add #include”SkinDetector.h” in the file from where you are using this, or you can create a new class by right clicking on the project and Add new class named SkinDetector and put the contents on the respective .h and .cpp file. Hope this will works. if not let me know.

  3. priyanka026 says:

    hi i am priyanka i am doing project on human detection in video but i am nt getting proper result can u help me out ??

  4. eS says:

    Hi! I want to test this code with my images(*.jpg). Please tell me what i need to change(code)?

  5. Hossein Rafipoor says:

    Thank you, your code was so clear and well suited for begginers, even better than opencv samples!
    but i have a problem for loading video files, camera works well, but video files were not opened, none of the wmv, avr or mpeg formats, can you help me through this?
    thanks in advance!

  6. Ab says:

    @Hossein: OpenCV works with only .avi video format

  7. Pingback: C++ & OpenCV resources | using namespace cv;

  8. Really interesting post. I was wondering if you did freelance work? I’m working on a project that I’m hoping to use a bit of opencv in. Feel free to email me at


  9. Thanks Razib fro this great tutorial

    Currently the output image is only black and white area

    how to output an image with only skin color is selected and the others are colored in black ?


  10. Leonardo GZ says:

    It works great! Thank you!

  11. Jerome Cansado says:

    Hi awesome work. I just have 1 question; how can you convert the black and white images to actual skin images? What I mean is the display of the images is white, how can I convert it into actual skin colors (brown,black)?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s