function [output]=ff_transform(str);
%str为图像文件名--------------------
data=side(str);
[x,y]=size(data);
index=1;
for i=1:x
for j=1:y
if data(i,j)==1
s(index,1)=i;
s(index,2)=j;
index=index+1;
end
end
end
%--------------------------------------获取质心
x0=0;
y0=0;
for i=1:index-1
x0=x0+s(i,1);
y0=y0+s(i,2);
end
x0=x0/(index-1);
y0=y0/(index-1);
%---------------------------------------获取顺序轮廓数据
outline_data=out_line(data,s);
%----------------------------------------重构图像
recon(outline_data);
%output=outline_data;
%---------------------------------------判断是等角度还是等距离采样
dis_angle=input('Do you want use equal dis? Y/N [Y]: ', 's');
if dis_angle=='Y';
after_data=equal_dis(outline_data);
end
if dis_angle=='N'
after_data=equal_angle(outline_data,x0,y0);
end
%r(i)为轮廓像素点到质心的距离
for i=1:max(size(after_data))
r(i)=sqrt((after_data(i,1)-x0).^2+(after_data(i,2)-y0).^2);
end
%fft:表示傅立叶变换,FFT表示变换后的结果
output=fft(r);
%function side------------------
function outside=side(str);
origin_image=imread(str);
level=graythresh(origin_image);
gray_image=im2bw(origin_image,level);
se=strel([0 1 0;1 1 1;0 1 0]);
erode_image=imerode(gray_image,se);
%imshow(erode_image);
final_image=imsubtract(gray_image,erode_image);
outside=final_image;
figure: imshow(final_image);
%final_image:为轮廓矩阵
%function out_line(data,s)----------------获取轮廓数据,顺序方式的,只适合轮廓边界是连续的情况.
function [order_data]=out_line(data,s)
[xmax,ymax]=size(data)
order_data(1,1)=s(1,1);
order_data(1,2)=s(1,2);
xx=order_data(1,1);
yy=order_data(1,2);
i=2;
s1=0;
condition=1;
j=0;
while condition~=0
s1=i;
if (xx-1)&&data(xx-1,yy)==1&&~is_vector([xx-1 yy],order_data)
order_data(i,1)=xx-1;
order_data(i,2)=yy;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if (xx-1)&&(yy-1)&&data(xx-1,yy-1)==1&&~is_vector([xx-1 yy-1],order_data)
order_data(i,1)=xx-1;
order_data(i,2)=yy-1;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if (yy-1)&&data(xx,yy-1)==1&&~is_vector([xx yy-1],order_data)
order_data(i,1)=xx;
order_data(i,2)=yy-1;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if ((xx+1)<=xmax)&&(yy-1)&&data(xx+1,yy-1)==1&&~is_vector([xx+1 yy-1],order_data)
order_data(i,1)=xx+1;
order_data(i,2)=yy-1;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if ((xx+1)<=xmax)&&data(xx+1,yy)==1&&~is_vector([xx+1 yy],order_data)
order_data(i,1)=xx+1;
order_data(i,2)=yy;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if ((xx+1)<=xmax)&&((yy+1)<=ymax)&&data(xx+1,yy+1)==1&&~is_vector([xx+1 yy+1],order_data)
order_data(i,1)=xx+1;
order_data(i,2)=yy+1;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if ((yy+1)<=ymax)&&data(xx,yy+1)==1&&~is_vector([xx yy+1],order_data)
order_data(i,1)=xx;
order_data(i,2)=yy+1;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if (xx-1)&&((yy+1)<=ymax)&&data(xx-1,yy+1)==1&&~is_vector([xx-1 yy+1],order_data)
order_data(i,1)=xx-1;
order_data(i,2)=yy+1;
xx=order_data(i,1);
yy=order_data(i,2);
i=i+1;
end
if s1==i
xx=order_data(i-2,1);
yy=order_data(i-2,2);
if j==i
condition=0;
end
j=i;
end
end
%function after_data=equal_dis(out_data)----------------按等曲线距离采样
function after_data=equal_dis(out_data)
dis=input('please input the interval of image outline data:');
after_data(1,1)=out_data(1,1);
after_data(1,2)=out_data(1,2);
length=max(size(out_data));
len=0;
j=1;
for i=1:length-1
if len<dis
len=len+sqrt((out_data(i,1)-out_data(i+1)).^2+(out_data(i,2)-out_data(i+1,2)).^2);
else
len=0;
j=j+1;
after_data(j,1)=out_data(i+1,1);
after_data(j,2)=out_data(i+1,2);
end
end
%function
%after_data=equal_angle(out_data,x0,y0)---------------------------等角度采样,在实际
%中不太可能严格相等,所以设置一个阈值,阈值选取的合理性决定了最后采样的数据点数
function after_data=equal_angle(out_data,x0,y0)
N=input('please input the number of share angle');
if N>max(size(out_data))
error('N must be smaller than the size of out_data');
end
angle=pi/N;
K=1;
for j=1:max(size(out_data))
i=0;
while i<N
if abs(tan(angle*i)-(out_data(j,1)-x0)/(out_data(j,2)-y0))<0.015;
after_data(K,1)=out_data(j,1);
after_data(K,2)=out_data(j,2);
K=K+1;
break;
end
i=i+1;
end
end
%function is_vector---------------判断一向量是否为另一向量的子向量,child为1*2,mother为n*2
function mark=is_vector(child,mother)
[x,y]=size(mother);
mark=1;
for i=1:x
mark=1;
for j=1:y
mark=(mother(i,j)==child(1,j))*mark;
end
if mark
break;
end
end
if (i==x)&&(j==y)&&~mark
mark=0;
end
%function recon(data)-------------重构图像
function recon(data)
[x,y]=max(data);
image_data=zeros(x,y);
[x1,y1]=size(data);
for i=1:x1
xaxe=data(i,1);
yaxe=data(i,2);
image_data(xaxe,yaxe)=1;
end
figure:imshow(image_data);