水平方向
function Emean=findEnergy(X)%求能量矩阵
[rows cols dim]=size(X);
Grd=[ 1 2 1;
0 0 0;
-1 -2 -1];
Emean=zeros(rows,cols);
for i=1:dim
Eh(:,:,i)=conv2(X(:,:,i),Grd,'same');
Ev(:,:,i)=conv2(X(:,:,i),Grd.','same');
E(:,:,i)=abs(Eh(:,:,i))+abs(Ev(:,:,i));
end
Emean=1/dim*sum(E,3);
function SeamVector=findSeam(X)%寻找seam向量
[rows cols]=size(X);
for j=cols:-1:1
if j==cols
[value, i]=min(X(:,cols)); %找最后一行的最小值,返回值的大小和所在列数
else
if SeamVector(j+1)==1
Vector=[Inf X(SeamVector(j+1),j) X(SeamVector(j+1)+1),j];
elseif SeamVector(j+1)==rows
Vector=[X(SeamVector(j+1)-1,j) X(SeamVector(j+1),j) Inf];
else
Vector=[X(SeamVector(j+1)-1,j) X(SeamVector(j+1),j) X(SeamVector(j+1)+1,j)];
end
[Value Index]=min(Vector);
IndexIncrement=Index-2;
i=SeamVector(j+1)+IndexIncrement;
end
SeamVector(1,j)=i;
end
function SeamImg=findSeamImg(X)
[rows cols]=size(X);
SeamImg=zeros(rows,cols);
SeamImg(:,1)=X(:,1);
X=SeamPlot(X,SeamVector)
value=1.5*max(X(:));
[A,B]=size(SeamVector);
for j=1:B
X(SeamVector(j),j)=value;
end
for j=2:cols
for i=1:rows
if i-1<1 SeamImg(i,j)= X(i,j)+min([SeamImg(i,j-1),SeamImg(i+1,j-1)]);
elseif i+1>rows SeamImg(i,j)= X(i,j)+min([SeamImg(i-1,j-1),SeamImg(i,j-1)]);
else SeamImg(i,j)= X(i,j)+min([SeamImg(i-1,j-1),SeamImg(i,j-1),SeamImg(i+1,j-1)]);
end
end
end
SeamVector=removalMap(X,lines);
[rows cols dim]=size(X);
E=findEnergy(X);
for j=1:min(lines,rows-1)
S=findSeamImg(E);
SeamVector(j,:)=findSeam(S);
X=SeamCut(X,SeamVector(j,:));
E=SeamCut(E,SeamVector(j,:));
[rows cols dim]=size(X);
end
X=SeamCut(X,SeamVector)
[rows cols dim]=size(X);
[SVrows SVcols SVdim]=size(SeamVector);
if cols~=SVcols
error('SeamVector and image dimension mismatch');
end
for k=1:SVrows
for j=1:dim
for i=1:cols
if SeamVector(k,i)==1
CutImg(:,i,j)=[X(2:rows,i,j)];
elseif SeamVector(k,i)==rows
CutImg(:,i,j)=[X(1:rows-1,i,j)];
else
CutImg(:,i,j)=[X(1:SeamVector(k,i)-1,i,j);X(SeamVector(k,i)+1:rows,i,j)];
end
end
end
X=CutImg;
clear CutImg
[rows cols dim]=size(X);
end
垂直方向
function SeamVector=findSeam(X)
[rows cols]=size(X);
for i=rows:-1:1
if i==rows
[value, j]=min(X(rows,:));
else
if SeamVector(i+1)==1
Vector=[Inf X(i,SeamVector(i+1)) X(i,SeamVector(i+1)+1)];
elseif SeamVector(i+1)==cols
Vector=[X(i,SeamVector(i+1)-1) X(i,SeamVector(i+1)) Inf];
else
Vector=[X(i,SeamVector(i+1)-1) X(i,SeamVector(i+1)) X(i,SeamVector(i+1)+1)];
end
[Value Index]=min(Vector);
IndexIncrement=Index-2;
j=SeamVector(i+1)+IndexIncrement;
end
SeamVector(i,1)=j;
end
SeamImg=findSeamImg(X)
[rows cols]=size(X);
SeamImg=zeros(rows,cols);
SeamImg(1,:)=X(1,:); %X(1,:)表示矩阵的第一行
for i=2:rows
for j=1:cols
if j-1<1
SeamImg(i,j)= X(i,j)+min([SeamImg(i-1,j),SeamImg(i-1,j+1)]);
elseif j+1>cols
SeamImg(i,j)= X(i,j)+min([SeamImg(i-1,j-1),SeamImg(i-1,j)]);
else
SeamImg(i,j)= X(i,j)+min([SeamImg(i-1,j-1),SeamImg(i-1,j),SeamImg(i-1,j+1)]);
end
end
end
SeamVector=removalMap(X,lines);
[rows cols dim]=size(X);
E=findEnergy(X);
for i=1:min(lines,cols-1)
S=findSeamImg(E);
SeamVector(:,i)=findSeam(S);
X=SeamCut(X,SeamVector(:,i));
E=SeamCut(E,SeamVector(:,i));
[rows cols dim]=size(X);
end
X=SeamCut(X,SeamVector)
[rows cols dim]=size(X);
[SVrows SVcols SVdim]=size(SeamVector);
if rows~=SVrows
error('SeamVector and image dimension mismatch');
end
for k=1:SVcols
for i=1:dim
for j=1:rows
if SeamVector(j,k)==1
CutImg(j,:,i)=[X(j,2:cols,i)];
elseif SeamVector(j,k)==cols
CutImg(j,:,i)=[X(j,1:cols-1,i)];
else
CutImg(j,:,i)=[X(j,1:SeamVector(j,k)-1,i) X(j,SeamVector(j,k)+1:cols,i)];
end
end
end
X=CutImg;
clear CutImg
[rows cols dim]=size(X);
end
X=SeamPlot(X,SeamVector)
value=1.5*max(X(:));
for i=1:size(SeamVector,1)
X(i,SeamVector(i))=value;
end
总程序
X=imread('256V256.jpg');
X=double(X)/255;
[rows cols dim]=size(X);
Y=rgb2hsv(X);
figure(1); %原图
imagesc(X)
axis equal %坐标轴的长度单位设置成相等
% 获取灰度图像
E1=findEnergy(X);
figure(2);
imshow(E1)
%寻找计算seam图像
S1=findSeamImg(E1);
figure(3)
imshow(S1,[min(S1(:)) max(S1(:))])
%寻seam
SeamVector=findSeam(S1);
% 画出梯度图像的缝
SeamedImg=SeamPlot(X,SeamVector);
figure(4);
imshow(SeamedImg,[min(SeamedImg(:)) max(SeamedImg(:))])
%移除seam
SeamCutImg=SeamCut(X,SeamVector);
figure(5);
imshow(SeamCutImg)
%寻找并移除N个seams
M=removalMap(X,N);
MSeamedImg=SeamCut(X,M);
figure(6)
imshow(MSeamedImg)
imwrite(MSeamedImg,'N.jpg')