我想用Matlab在图像中进行缝合。
如果我只使用脚本而不是通过调用函数来实现它,它就可以工作。当我将脚本作为一个函数并尝试调用它时会出现问题
我使用的代码
最终剧本:我用来反复调用该函数的脚本,所以我得到了结果
clear all;
close all;
%Image = 'mall.jpg';
Image_Array = imread('mall.jpg');
%We keep the dimension of the image array
[Row_Count,Column_Count,Colour] = size(Image_Array);
Image = double(Image_Array)/255;
Image = rgb2gray(Image);
for i = 1 : 10
[Image_Array,Row_Count,Column_Count]=Vertical_Seam(Image_Array,Row_Count,Column_Count);
imshow(Image_Array);
end
Vertical Seam:我用来做缝合工作的脚本。这是我想把它作为一个函数的文件,并在Finalscript中调用它,但我不知道如何(你会注意到我试图做一个功能,但因为它没有工作我把它放在%)
%function [Final_Image,Row_Count,Column_Count] = Vertical_Seam(Image_Array,Row_Count,Column_Count)
Image_Array = imread('mall.jpg');
%We keep the dimension of the image array
[Row_Count,Column_Count,Colour] = size(Image_Array);
Image = double(Image_Array)/255;
Image = rgb2gray(Image);
for repeat = 1 : 10
%Calculate the energy
[E_x,E_y] = gradient(Image);
Energy = abs(E_x) + abs(E_y);
%We run the image and keep the minimum sum-energy in Image_Array
Cumulative_Energy = zeros(Row_Count,Column_Count);
% i represent rows
% j represent columns
for i = 1 : Row_Count
for j = 1 : Column_Count
if i==1
Cumulative_Energy(i,j) = Energy(i,j);
elseif i>1
if j==1
temp1 = [Cumulative_Energy(i-1,j),Cumulative_Energy(i-1,j+1)];
Cumulative_Energy(i,j) = Energy(i,j) + min(temp1);
elseif j>1 && j<Column_Count
temp2 = [Cumulative_Energy(i-1,j-1),Cumulative_Energy(i-1,j),Cumulative_Energy(i-1,j+1)];
Cumulative_Energy(i,j) = Energy(i,j) + min(temp2);
elseif j==Column_Count
temp3 = [Cumulative_Energy(i-1,j-1),Cumulative_Energy(i-1,j)];
Cumulative_Energy(i,j) = Energy(i,j) + min(temp3);
end
end
end
end
%Find the minimum value from last column
temp_min = Cumulative_Energy(Row_Count,1);
point_min = 1;
for j = 2 : Column_Count
if Cumulative_Energy(Row_Count,j)<temp_min
temp_min = Cumulative_Energy(Row_Count,j);
point_min = j;
end
end
%Start from the minimum value from last column and backtrack to find the minimum seam
%We need an array to keep minimum energy in every row while
%backtracking
Keep_Seam = zeros(Row_Count,1);
Keep_Seam(Row_Count) = temp_min;
%We need also the point of the minimum energy (the number of column)
Keep_Point = zeros(Row_Count,1);
Keep_Point(Row_Count) = point_min;
%Paint the pixel
Image_Array = Image_Colouring(Image_Array,Row_Count,point_min);
for i = Row_Count : -1 : 1
if i==1
if point_min==1
Tmp1 = [Cumulative_Energy(i,point_min),Cumulative_Energy(i,point_min+1)];
Keep_Seam(i) = min(Tmp1);
if min(Tmp1)==Cumulative_Energy(i,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min==Column_Count
Tmp2 = [Cumulative_Energy(i,point_min-1),Cumulative_Energy(i,point_min)];
Keep_Seam(i) = min(Tmp2);
if min(Tmp2)==Cumulative_Energy(i,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min-1;
point_min = point_min-1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min>1 && point_min<Column_Count
Tmp3 = [Cumulative_Energy(i,point_min-1),Cumulative_Energy(i,point_min),Cumulative_Energy(i,point_min+1)];
Keep_Seam(i) = min(Tmp3);
if min(Tmp3)==Cumulative_Energy(i,point_min)
Keep_Point(i) = point_min;
elseif min(Tmp3)==Cumulative_Energy(i,point_min-1)
Keep_Point(i) = point_min-1;
point_min = point_min-1;
elseif min(Tmp3)==Cumulative_Energy(i,point_min+1)
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
end
elseif i==Row_Count || i<Row_Count
if point_min==1
Temp1 = [Cumulative_Energy(i-1,point_min),Cumulative_Energy(i-1,point_min+1)];
Keep_Seam(i) = min(Temp1);
if min(Temp1)== Cumulative_Energy(i-1,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min==Column_Count
Temp2 = [Cumulative_Energy(i-1,point_min-1),Cumulative_Energy(i-1,point_min)];
Keep_Seam(i) = min(Temp2);
if min(Temp2)==Cumulative_Energy(i-1,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min-1;
point_min = point_min-1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min>1 && point_min<Column_Count
Temp3 = [Cumulative_Energy(i-1,point_min-1),Cumulative_Energy(i-1,point_min),Cumulative_Energy(i-1,point_min+1)];
Keep_Seam(i) = min(Temp3);
if min(Temp3)==Cumulative_Energy(i-1,point_min)
Keep_Point(i) = point_min;
elseif min(Temp3)==Cumulative_Energy(i-1,point_min-1)
Keep_Point(i) = point_min-1;
point_min = point_min-1;
elseif min(Temp3)==Cumulative_Energy(i-1,point_min+1)
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
end
end
end
imshow(Image_Array);
Image_Seam = zeros(Row_Count,Column_Count-1,Colour,'uint8');
for i = 1 : Row_Count
Image_Seam(i,:,:)=Image_Array(i,[1:Keep_Point(i)-1,Keep_Point(i)+1:Column_Count],:);
end
%imshow(Image_Seam);
Image_Array = Image_Seam;
end
% end
我想连接这两个文件,以便Finalscript将Verticalseam称为函数而不是脚本