Matlab face detection and positioning based on skin color

%---------------------- Face detection and positioning based on skin color------------------- ----------

clc ; clear ; close all ;

%--------------------------------------------------

A = imread( 'a.jpg');

subplot(2,2,1);imshow(A);title('Original image');

A = double(A);

%-------------------- Convert color space ------------------

The red, green and blue components of the %RGB image

R = A( :, :, 1);

G = A( :, :, 2);

B = A( :, :, 3);

% Convert the red R, green G and blue B of the RGB image to the hue H, saturation S and brightness V of the HSV image

[H, S , V] = rgb2hsv( A );

% Converts the red, green and blue values ​​of an RGB image to the luminance (Y) and chrominance (Cb and Cr) values ​​of a YCbCr image

Y = 0.257*R + 0.504*G + 0.098*B + 16;

Cb = 0.148*R - 0.291*G + 0.439*B + 128;

Cr = 0.439*R - 0.368*G - 0.071*B + 128;

%-------------------Find the skin area ---------------------

[m, n] = size( R ); % Find the matrix size of a single dimension value

skin = zeros(m, n);

for i = 1 : m

for j = 1 : n

if 145<=Cr(i,j)&&Cr(i,j)<=165&&...

145<=Cb(i,j)&&Cb(i,j)<=180&&...

0.01<=H(i,j)&&H(i,j)<=0.15

skin( i, j ) = 1; % skin area

end

end

end

subplot(2,2,2);imshow(skin);title('skin area location');

%----------------Enhances skin area-------------------

%BW2=bwareaopen(BW,P), removes all connected components (objects) with less than P pixels from the binary image BW

%Eliminates even loose and finely divided areas of skin

skin = bwareaopen( skin ,round( m*n/900 ));

%SE = strel("disk",r) creates a disk-shaped structuring element, where r specifies the radius

se = shot( 'disk', 5 );

%Use the structural element SE to expand the image and enhance the bright area

skin = imdilate( skin, se );

% convert skin area to color

color( :, : , 1) = R.*skin;

color( :, : , 2) = G.*skin;

color( :, : , 3) = B.*skin;

subplot(2,2,3);imshow(uint8(color));title('Show skin area');

%------------------Locate the face image --------------------

%L=bwlabel(BW) returns the label matrix L containing the labels of the 8 connected objects found in BW.

L = bwlabel (skin , 8);

%Returns the measurement value of the attribute set of each 8-connected component (object) in the binary image BW

B = regionprops( logical(L), 'BoundingBox' );

%Convert the structure to a cell array

B1 = struct2cell(B);

%Convert the cell array to an ordinary array of the underlying data type

B2 = cell2mat(B1);

subplot(2,2,4);imshow(uint8(A));title('Locate face image');

[s1, s2 ] = size(B2);

for k = 3 : 4 : s2-1;

if (B2(1,k)/B2(1,k+1))<1.8 &&...

(B2(1,k)/B2(1,k+1))>0.4 &&...

(B2(1,k)*B2(1,k+1))>1000

hold on; The function of %hold on is to keep the original image and accept the new curve drawn afterwards, superimposing the drawing

%rectangle('Position',pos) creates a rectangle in two-dimensional coordinates.

%Specify pos as a four-element vector of the form [xywh] in data units.

% The x and y elements determine the position, and the w and h elements determine the size.

rectangle ( 'Position', [B2(1,k-2),B2(1,k-1),B2(1,k),B2(1,k+1)],'EdgeColor', 'r');

end

end

%-----------------------------Method Two------------------ -----------------

% Use the official Model provided by CascadeObjectDetector to realize the detection and positioning of the face area,

clc ; clear ; close all ;

img= imread('a.jpg');

%Create a face detection object

faceDetector = vision.CascadeObjectDetector;

% Find the face area

%step calls the System object and runs the algorithm. Depending on your System object, step can return output parameters.

box = step(faceDetector,img);

% mark the face area

face = insertObjectAnnotation(img,'rectangle',box,'Face','color' , 'r');

figure;

imshow(face);

title('face positioning');

Guess you like

Origin blog.csdn.net/starryskyzl/article/details/129441481