matlab 简单的人脸识别

代码如下

clc; clear all; close all;
% 载入图像
Img = imread('C:\Users\Pictures\5.jpg');
if ndims(Img) == 3
    I=rgb2gray(Img);
else
    I = Img;
end
BW = im2bw(I, graythresh(I)); % 二值化
figure;
subplot(2, 3, 1); imshow(Img);
title('原图像', 'FontWeight', 'Bold');
subplot(2, 3, 2); imshow(I);
title('灰度化', 'FontWeight', 'Bold');
subplot(2, 3, 3); imshow(BW);
title('二值图像', 'FontWeight', 'Bold');
subplot(2, 3, 4); imshow(BW);
title('网格标记图像', 'FontWeight', 'Bold');

hold on;
[xt, yt] = meshgrid(round(linspace(1, size(I, 1), 10)), ...
    round(linspace(1, size(I, 2), 10)));
mesh(yt, xt, zeros(size(xt)), 'FaceColor', ...
    'None', 'LineWidth', 1, ...
    'EdgeColor', 'r');

[n1, n2] = size(BW);
r = floor(n1/10); % 分成10块,行
c = floor(n2/10); % 分成10块,列
x1 = 1; x2 = r; % 对应行初始化
s = r*c; % 块面积
for i = 1:11
    y1 = 1; y2 = c; % 对应列初始化
    for j = 1:11
        if (y2<=c || y2>=9*c) || (x1==1 || x2==r*10)
            % 如果是在四周区域
                BW(x1:x2, y1:y2) = 0;      
        end
        y1 = y1+c; % 列跳跃
        y2 = y2+c; % 列跳跃
    end
    x1 = x1+r; % 行跳跃
    x2 = x2+r; % 行跳跃
end
subplot(2, 3, 5);imshow(BW);
title('识别人脸', 'FontWeight', 'Bold');

[L, num] = bwlabel(BW, 8); % 区域标记
stats = regionprops(L, 'BoundingBox'); % 得到包围矩形框
Bd = cat(1, stats.BoundingBox);
[s1, s2] = size(Bd);
mx = 0;
for k = 1:s1
    p = Bd(k, 3)*Bd(k, 4); % 宽*高
    if p>mx && (Bd(k, 3)/Bd(k, 4))<1.8
        % 如果满足面积块大,而且宽/高<1.8
        mx = p;
        j = k;
    end
end

subplot(2, 3, 6);imshow(Img); hold on;
rectangle('Position', Bd(j, :), ...
    'EdgeColor', 'w', 'LineWidth', 1);
title('标记图像', 'FontWeight', 'Bold');

最后结果
人脸识别结果

  • 5
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我扶奶奶过哈登

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值