Matlab实现简单的人脸识别程序

clear all;
close all;
F = imread(‘face9.jpg’);
F = rgb2gray(F); % 将图像转化为灰度图
level = graythresh(F);
BW = im2bw(F,level); % 将图像二值化
[n1 n2] = size(BW);
H = floor(n1/10); % 将图像分割成10个快
W = floor(n2/10);
h1 = 1;
h2 = H;
s = H*W; % 求得块的面积
h_i = H;
w_i = W;
for i = 1:10
w1 = 1;w2 =W; % 对应列初始化
for j = 1:10
if (w1 <= w_i || w2 >= 9*W) || (h1 <= h_i || h2 >= n1 - h_i) % 判断是否在图片四周的区域
loc = find(BW(h1:h2,w1:w2) == 0)
[num,~] = size(loc);
pr = num*100/s; % 计算灰度为黑色的像素占的比例
if pr < 100
BW(h1:h2,w1:w2) = 0;
end
% figure,imshow(BW);
% hold on
end
w1 = w1+W; % 跳到下一个块对应的列
w2 = w2+W;
end
h1 = h1+H; % 跳到下一个块对应的行
h2 = h2+H;
end
L = bwlabel(BW,8); % 区域标记
BB = regionprops(L,’BoundingBox’); % 得到包围的矩形框
Bd = cat(1,BB.BoundingBox);
[Bd_i,~] = size(Bd);
mx = 0;
for k = 1:Bd_i
p = Bd(k, 3)*Bd(k, 4);
% 如果满足面积块大,而且宽/高<1.8,则跳到Bd的该行
if p > mx && (Bd(k,3)/Bd(k,4)) < 1.8
mx = p;
j = k;
end
end
imshow(F); hold on; % 显示图片
% 画出矩形框
rectangle(‘Position’, Bd(j, :), …
‘EdgeColor’, ‘r’, ‘LineWidth’, 3);
title(‘标记图像’, ‘FontWeight’, ‘Bold’);

你可能感兴趣的:(数字图像处理)