Question

I have an RGB image in MATLAB, and I want to loop through each pixel and check if it's skin coloured (ish) [I will probably do it in HSV space at a later point to negate the effects of lighting etc.]

Does anyone know the best way to do this? MATLAB's weird syntax is confusing me a little!

At the moment I have:

for x = 1:size(I,1)
    for y = 1:size(I,2)
            %Get the value from this x,y coordinate and check the colour.
    end
end

But I think that's horribly wrong.

How do you access the different parts of the matrix correctly?

Was it helpful?

Solution

Ed,

You do not need to loop:

>> a  = magic(5)

a =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9


>> vi = (a > 5) & (a < 10)

vi =

     0     0     0     1     0
     0     0     1     0     0
     0     1     0     0     0
     0     0     0     0     0
     0     0     0     0     1

You can see how this statement could be expanded for RGB and such. I would make a function


function out = isFlesh(in)

%% put flesh checking code here


I suspect you are going to want a range of these (isCaucasian, isAsian, etc...) The problem is going to be that there is a huge range of flesh colors, so you are going to want to check for proximity to neighbors of a similar color range.

This part of the problem is easy, but the larger problem is fraught with peril.

-Doug

OTHER TIPS

One thing to keep in mind is that RGB images are 3-D matrices. The first "page" (i.e. A(:,:,1)) is red, the second is green, and the third is blue. Sometimes I find it easier to operate on RGB matrices by reshaping them into N-by-3 matrices where all the red pixels are in column 1, the green in column 2, and the blue in column 3. This is how to do it:

A = (a 3-D RGB image);
matSize = size(A);
A = reshape(A,prod(matSize(1:2)),3);
...
% Modify or extract data from reshaped matrix
...
A = reshape(A,matSize);  % Return A to original dimensions

Not sure if this will help you specifically with what you want to do , but I often find it useful.

Thresholding every channel is not a very robust way of doing skin color detection. A simple lookup table works much better. The lookup table would record a value for each color how likely it is to be skin. There are various skin color datasets which you can use to obtain such values. Then you can compare the color of the pixel against the value in the table and do a decision based on a threshold.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top