《照片人脸检测MATLAB代码.docx》由会员分享,可在线阅读,更多相关《照片人脸检测MATLAB代码.docx(4页珍藏版)》请在三一文库上搜索。
1、.% 载入图像Img = imread(star1.jpg);if ndims(Img) = 3I=rgb2gray(Img);elseI = Img;endBW = im2bw(I, graythresh(I); %二值化figure;subplot(2, 2, 1); imshow(Img);title( 原图像 , FontWeight, Bold);subplot(2, 2, 2); imshow(Img);title( 网格标记图像, FontWeight, Bold);hold on;xt, yt = meshgrid(round(linspace(1, size(I, 1), 1
2、0), .round(linspace(1, size(I, 2), 10);mesh(yt, xt, zeros(size(xt), FaceColor, .None, LineWidth, 3, .EdgeColor, r);subplot(2, 2, 3); imshow(BW);title( 二值图像 , FontWeight, Bold);n1, n2 = size(BW);r = floor(n1/10); % 分成 10 块,行c = floor(n2/10); % 分成 10 块,列x1 = 1; x2 = r; % 对应行初始化s = r*c; % 块面积for i = 1:
3、10y1 = 1; y2 = c; % 对应列初始化for j = 1:10if (y2=9*c) | (x1=1 | x2=r*10)% 如果是在四周区域loc = find(BW(x1:x2, y1:y2)=0);p, q = size(loc);pr = p/s*100; %黑色像素所占的比例数if pr mx & (Bd(k, 3)/Bd(k, 4)1.8% 如果满足面积块大,而且宽 / 高 1.8 mx = p;j = k;endendsubplot(2, 2, 4);imshow(I); hold on;rectangle(Position, Bd(j, :), .EdgeColo
4、r, r, LineWidth, 3);title( 标记图像 , FontWeight, Bold);if ndims(Img) = 3I=rgb2gray(Img);elseI = Img;endBW = im2bw(I, graythresh(I); %二值化figure;subplot(2, 2, 1); imshow(Img);title( 原图像 , FontWeight, Bold);subplot(2, 2, 2); imshow(Img);title( 网格标记图像, FontWeight, Bold);hold on;xt, yt = meshgrid(round(lins
5、pace(1, size(I, 1), 10), .round(linspace(1, size(I, 2), 10);mesh(yt, xt, zeros(size(xt), FaceColor, .None, LineWidth, 3, .EdgeColor, r);subplot(2, 2, 3); imshow(BW);title( 二值图像 , FontWeight, Bold);n1, n2 = size(BW);r = floor(n1/10); % 分成 10 块,行c = floor(n2/10); % 分成 10 块,列x1 = 1; x2 = r; % 对应行初始化.s
6、= r*c; % 块面积for i = 1:10y1 = 1; y2 = c; % 对应列初始化for j = 1:10if (y2=9*c) | (x1=1 | x2=r*10)% 如果是在四周区域loc = find(BW(x1:x2, y1:y2)=0);p, q = size(loc);pr = p/s*100; %黑色像素所占的比例数if pr mx & (Bd(k, 3)/Bd(k, 4)1.8% 如果满足面积块大,而且宽 / 高 1.8 mx = p;j = k;endendsubplot(2, 2, 4);imshow(I); hold on;rectangle(Position, Bd(j, :), .EdgeColor, r, LineWidth, 3);title( 标记图像 , FontWeight, Bold);.