Face detection based on MATLAB

Project Overview

We have learned the application of some image processing algorithms before, so this experiment will carry out a more comprehensive project. In this algorithm, we will use the skin color model detection algorithm to locate the face. In this article, we first use MATLAB to implement the algorithm, and wait until the next article to implement the algorithm using FPGA.

Project flow chart

Insert picture description here
The flow chart of the entire project is as shown above, we can use the above block diagram to write to complete the image positioning.

Skin color detection

First convert the image from RGB format to YCrCb format, where Y is brightness information, Cr indicates red information, and Cb indicates green information. The reason for converting to this format is to cancel the brightness-related information, and then use the blue component and blue component to determine where is the skin color.

The formula for converting RGB format to YCrCb is as follows:
Insert picture description here
after data analysis, the skin color component is in the following range:
Insert picture description here
as long as the component in this range is considered to be skin, we can see that our algorithm is relatively rough, but after corrosion The effect found that the effect is better.

Image erosion

After skin color detection, because some colors close to the skin color produce noise, the data after skin color detection needs to be morphologically filtered, that is, only the shape of the face is retained.

Basic morphological filtering includes: corrosion, dilation, open operation, and closed operation.
Open operation: corrosion first and then expansion
Close operation: expansion first and then corrosion

In our project, we used the image erosion operation. It is reasonable to use the open operation.

Image positioning

Calculate the coordinates of the boundary of the white area on the filtered image: x_min, x_max, y_min, y_max. But this method must make the image after the image erosion only have face information.

Then locate the upper boundary, use the upper boundary information to frame, and then frame the face.

The whole process is particularly simple, I believe everyone can learn. As for the specific boundary positioning and how to use the boundary to frame the frame, there is a detailed introduction in the following code. You can learn better from the code, and I wo n’t repeat it here.

MATLAB code

The above MATLAB code is as follows:

clc;
clear all;
close;
image = imread('aaa.bmp');

image_red = image(:,:,1);
image_green = image(:,:,2);
image_blue = image(:,:,3);
[ROW,COL] = size(image_red);
figure(1)
imshow(image);
title('原图');

YCbCr = rgb2ycbcr(image);

Y = YCbCr(:,:,1);
Cb = YCbCr(:,:,2);
Cr = YCbCr(:,:,3);
pic_gray = zeros(ROW,COL);

for i = 1:ROW
    for j = 1:COL
        if(Cb(i,j) > 77 && Cb(i,j) < 127 && Cr(i,j) > 133 && Cr(i,j) < 173)
            pic_gray(i,j) = 255;
        else
            pic_gray(i,j) = 0;
        end
    end
end

figure(2)
imshow(pic_gray);
title('肤色检测图');

se = strel('square',20);
erode = imerode(pic_gray,se);
figure(3)
imshow(erode);
title('形态学滤波');

x_min = 1024;
x_max = 0;
y_min = 768;
y_max = 0;


for i = 1:ROW
    for j = 1:COL
        if(erode(i,j) > 0 && x_min > j)
            x_min = j;
        end
        if(erode(i,j) > 0 && x_max < j)
            x_max = j;
        end
    end
end

for i = 1:ROW
    for j = 1:COL
        if(erode(i,j) > 0 && y_min > i)
            y_min = i;
        end
        if(erode(i,j) > 0 && y_max < i)
            y_max = i;
        end
    end
end

for i = 1:ROW
    for j = 1:COL
        if(j == x_min && i >= y_min && i <= y_max)
            image_test(i,j,1) = uint8(255);
            image_test(i,j,2) = uint8(255);
            image_test(i,j,3) = uint8(255);
        elseif(j == x_max && i >= y_min && i <= y_max)
            image_test(i,j,1) = uint8(255);
            image_test(i,j,2) = uint8(255);
            image_test(i,j,3) = uint8(255);
        elseif(i == y_max && j >= x_min && j <= x_max)
            image_test(i,j,1) = uint8(255);
            image_test(i,j,2) = uint8(255);
            image_test(i,j,3) = uint8(255);
         elseif(i == y_min && j >= x_min && j <= x_max)
            image_test(i,j,1) = uint8(255);
            image_test(i,j,2) = uint8(255);
            image_test(i,j,3) = uint8(255);
        else
            image_test(i,j,1) = image(i,j,1);
            image_test(i,j,2) = image(i,j,2);
            image_test(i,j,3) = image(i,j,3);
        end
    end
end

%subplot(2,2,4);
figure(4)
imshow(image_test);
title('头像检测');



From the above program combined with our previous description, we can learn the image positioning method very well.

Simulation results

Original image:
Insert picture description here
Skin color detection image: After
Insert picture description here
algorithm corrosion
Insert picture description here
:
Insert picture description here
After image positioning: After the above simulation results, we successfully located our face. Prove the effectiveness of the algorithm. Next we will use FPGA to realize the above process, understand the algorithm flow of MATLAB above, and change to FPGA implementation is relatively simple.

references

1. Open source hackers

to sum up

It is not easy to create, and students who think the article is helpful can follow, like, and forward support. (Txt files, picture files are in the group) Students who have any opinions on the article or need to communicate more closely can join the following groups:
Insert picture description here

Published 56 original articles · Like 38 · Visits 20,000+

Guess you like

Origin blog.csdn.net/zhangningning1996/article/details/105441515