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.

//SkinDetector.h


#pragma once
#include<opencv\cv.h>
using namespace std;
class SkinDetector
{
public:
SkinDetector(void);
~SkinDetector(void);

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

private:
int Y_MIN;
int Y_MAX;
int Cr_MIN;
int Cr_MAX;
int Cb_MIN;
int Cb_MAX;
};

// end of SkinDetector.h file


//SkinDetector.cpp

#include "SkinDetector.h"
#include"opencv2\opencv.hpp"

SkinDetector::SkinDetector(void)
{
//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;
}

SkinDetector::~SkinDetector(void)
{
}

//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
cv::cvtColor(input,skin,cv::COLOR_BGR2YCrCb);

//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
cv::inRange(skin,cv::Scalar(Y_MIN,Cr_MIN,Cb_MIN),cv::Scalar(Y_MAX,Cr_MAX,Cb_MAX),skin);

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

capture.open(0);

to

capture.open("filenameWithPath.extention");

Source :

#include<opencv2\opencv.hpp>
#include"SkinDetector.h"
using namespace std;
using namespace cv;

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

capture.open(0);

//set height and width of capture frame
capture.set(CV_CAP_PROP_FRAME_WIDTH,320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,480);

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
while(1){

//store image to matrix
capture.read(cameraFeed);

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

skinMat= mySkinDetector.getSkin(cameraFeed);

imshow("Skin Image",skinMat);

waitKey(30);
}
return 0;
}

Advertisements

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.

24 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 cameron@splt.co

    Cameron

  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 ?

    thx

  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s