clear all;close all;clc;
src=imread('shu.png');
src=rgb2gray(src)
figure;
imshow(src,[])
[s_h s_w]=size(src);
if(s_h>s_w)
max=s_h;
B=ones(max,max-s_w)*125;
img=cat(2,src,B);
else
max=s_w;
B=ones(max-s_h,max)*125;
img=cat(1,src,B);
end
[h w]=size(img);
%置乱与复原的共同参数
n=50;
a=5;b=6;
N=h;
%置乱
imgn=zeros(max,max);
for i=1:n
for y=1:h
for x=1:w
xx=mod((x-1)+b*(y-1),N)+1;
yy=mod(a*(x-1)+(a*b+1)*(y-1),N)+1;
imgn(yy,xx)=img(y,x);
end
end
img=imgn;
end
figure;
imgs=imcrop(imgn,[0,0,s_w,s_h])
imshow(imgs,[])
%复原
img=imgn;
for i=1:n
for y=1:h
for x=1:w
xx=mod((a*b+1)*(x-1)-b*(y-1),N)+1;
yy=mod(-a*(x-1)+(y-1),N)+1 ;
imgn(yy,xx)=img(y,x);
end
end
img=imgn;
end
figure
imgn=imcrop(imgn,[0,0,s_w,s_h])
imshow(imgn,[])
** 原图**
加密图
还原图