||
CutAndResizeImg.m
利用MATLAB自动对图片进行切分和缩放,利用垂直方向灰度投影方法,能够对水平方向截取,重置方向类似。
%%%%%%%%%%%%%%%%%%%%%%根据灰度投影对一幅含背景的大图片进行截取(切割)人脸部分%%%%%%%%%%%%%%%%%%%%%%%%%
clc
close all
img=rgb2gray(imread('2.jpg'));
figure,imshow(img)
figure,imhist(img)
projectColumn=[];
projectColumn=sum(img,1);
maxX=max(projectColumn);
projectColumn=projectColumn./maxX;
%%%%%%%%%%%%遍历左边界,即人脸边界
leftX=0;
for i=1:size(projectColumn,2)/2
if projectColumn(i)<0.6
leftX=i;
break;
end
end
%%%%%%%%%%%%遍历右边界,即人脸边界
rightX=0;
for i=size(projectColumn,2):-1:size(projectColumn,2)/2
if projectColumn(i)<0.5
rightX=i;
break;
end
end
%%%%%%%%%%%%若符合逻辑则切割部分人脸
if leftX<rightX
imgNew=img(:,leftX:rightX);
figure,imshow(uint8(imgNew));
imgNew=imresize(imgNew,[200,200]);%%%%%%%%放大到指定尺寸
figure,imshow(uint8(imgNew));
end
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-24 04:36
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社