MATLAB Answers

How to detect skin region in YCbCr color space?

40 views (last 30 days)
I want to detect skin region in images which contain hand, foot or mouth and remove the background. The YCbCr color space is used in my work. I can convert the image into YCbCr color space and split each color channel in this color space but could go further. I have attached an image shows the original image and the detected skin region which I want (It was performed in a paper).
Any help would be deeply appriciated.
Here is some of my code:
% Read image
imgRGB = imread('ColorSpace\hand_foot12.jpg');
% Convert to YCbCr
YCbCr = rgb2ycbcr(imgRGB);
% Split each channel
Y=YCbCr( :,:,1);
Cb=YCbCr( :,:,2);
Cr=YCbCr( :,:,3);


Show 2 older comments
Mohammad Farhad Aryan
Mohammad Farhad Aryan on 31 Dec 2019
Thank you for your guide.
I used the Color Thresholder App and found a threshold which works for most of images taken from different sources on the internet but I have two images (a whiter skin and a medium darker) which it can't detect them very well.
I tried HSV color space but the code did not worked well for images with vivid blue backgrounds. Therefore, I used YCbCr color space and my code is as follows.
% -----------------------
% Read image
imgRGB = imread(['ColorSpace\hfmd_foot1.jpg']);
% -----------------------
% Convert to YCbCr
YCbCr = rgb2ycbcr(imgRGB);
% -----------------------
% Split each channel
Y=YCbCr( :,:,1);
Cb=YCbCr( :,:,2);
Cr=YCbCr( :,:,3);
skinplace = (93<Cb) & (Cb<136) & (138<Cr) & (Cr<175);
% -----------------------
% Fill small holes (disease spots)
imgOpen = imopen(imgFilled, se);
% -----------------------
% Extract the biggest blob
imgBigBlob = bwareafilt(imgOpen, 1);
% -----------------------
% Denoise using Median filter
imgFiltered = medfilt2(imgBigBlob);
% -----------------------
% Binary filtered image for output mask
Bw = imgFiltered;
% -----------------------
% Initialize output masked image based on input image.
maskedRGBImage = imgRGB;
% -----------------------
% Create RGB mask (Remove background)
maskedRGBImage(repmat(~Bw,[1 1 3])) = 0;
figure, imshow(maskedRGBImage);
Any help is appreciated.
Image Analyst
Image Analyst on 31 Dec 2019
You probably need to adjust your thresholds. If possible the thresholds might have to be determined from the image itself so they can vary rather than using fixed thresholds like the Color Thresholder app does. Fixed thresholds are best but only if you have good control over your exposure, lighting, and white balance, and from these "in the wild" images, it's clear that you don't have control over any of that. Anyway, it might still work with fixed thresholds but you'll have to use other techniques afterwards, like filling or other morphological operations. Please attach the original RGB images if you need more help.

Sign in to comment.

Accepted Answer

Gaurav Garg
Gaurav Garg on 24 Dec 2019
You can use ‘Color Thresholder’ app provided in the Image Acquisition Toolbox.
  1. After loading the image from a file/workspace, you can choose a color space for the image. Since you want to extract purple and yellow colours, I would recommend you to use YCbCr for purple colour and RGB space for the yellow colour.
  2. You would then see the image along with a set of controls for each colour component, depending on the colour space chosen. Adjust these controls to get the colour you want. And ‘Export Function’ for the resultant image.
  3. By exporting to function, you get Min and Max values for each channel for both the colours (yellow and Purple). You would also get a sliderBW matrix in each function. Use logical operator || to include both the matrices in your workspace and then set these pixels to white colour.
You can also refer to an example for purple colour from the documentation provided in the link.
Below is the code for an image file named ‘peppers.png’ and may need to be changed in some cases -
% Load your image in RGB variable
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 53.000;
channel1Max = 178.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 55.000;
channel2Max = 153.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 37.000;
channel3Max = 142.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;


Sign in to comment.

More Answers (0)

Translated by