数字水印学习之混沌学习

%%混沌置乱代码

clear;

clc;

%os原始图像的路径

[filename pathname]=uigetfile('*.bmp;*.tiff;*.tif', '读入图像');

os=[pathname filename];

o=imread(os);

% figure,imshow(o);

%计算原始图像的大小

[m n]=size(o);

%l=zeros(m,n);

l=linspace(0,0,m*n);

l(1)=0.98;

for i=2:m*n

l(i)=1-2*l(i-1)*l(i-1);

end

% %对生成的混沌序列进行排序

% [lsort lindex]=sort(l);

% t=linspace(0,0,m*n);

% t(1)=0.98;

% for i=1:m*n

% t(i+1)=1-2*t(i)*t(i);

% end

% for i=1:m*n

% if (l(i)>=0)

% l(i)=1;

% else

% l(i)=0;

% end

% end

% l=~l;

% subplot(2,2,2),imshow(l),title('混沌图像');

[lsort,lindex]=sort(l);

%ozl =original zhi luan 表示置乱后的原始图像

ozl=zeros(m,n);

for i=1:m*n

ozl(i)=o(lindex(i));

end

% ozl=reshape(ozl,m,n);

%%oo =original fu yuan

ofy=zeros(m,n);

for i=1:m*n

ofy(lindex(i))=ozl(i);

end

h= waitbar(0,'程序处理中,请耐心等待。。。');

for i=1:100, % computation here %

waitbar(i/100)

end

close(h) ;

subplot(1,3,1),imshow(o),title('原始图像');

subplot(1,3,2),imshow(ozl,[]),title('加密图像');

subplot(1,3,3),imshow(ofy,[]),title('复原图像');

%%%%

clear;

clc;

%需要注意本程序实现对二进制文件的加密,对于非二进制文件不能采用此程序进行加密!!!!

[filename pathname]=uigetfile('*.bmp;*.tiff;*.tif', '读入图像');

os=[pathname filename];

%os原始图像的路径

o=imread(os);

% figure,imshow(o);

%计算原始图像的大小

[m n]=size(o);

l=zeros(m,n);

l(1)=0.98;

for i=2:m*n

l(i)=1-2*l(i-1)*l(i-1);

end

% %对生成的混沌序列进行排序

% [lsort lindex]=sort(l);

% t=linspace(0,0,m*n);

% t(1)=0.98;

% for i=1:m*n

% t(i+1)=1-2*t(i)*t(i);

% end

for i=1:m*n

if (l(i)>=0)

l(i)=1;

else

l(i)=0;

end

end

% l=~l;

ei=xor(o,l);

di=xor(ei,l);

h= waitbar(0,'程序处理中,请耐心等待。。。');

for i=1:100, % computation here %

waitbar(i/100)

end

close(h)

subplot(2,2,1),imshow(o),title('原始图像');

subplot(2,2,2),imshow(l),title('混沌图像');

% figure,imshow(l);

%ei表示加密图像encryption image

subplot(2,2,3),imshow(ei),title('加密图像');

%di表示解密图像dencryption image

subplot(2,2,4),imshow(di,[]),title('解密图像');

% figure,imshow(di);

clear;

clc;

% o=imread('image/lena.bmp');

%%%%%%%%%%%%读入混沌初始值

prompt={'混沌初始值 '};

title='初值';

lines=1;

def={'98'};

mya=inputdlg(prompt,title,lines,def,'on');

key11=['0.',mya{1}];

key=str2num(key11);

%%%%%%%%%%%%

[filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存图像');

os=[pathname filename];

m=512;

n=512;

l=zeros(m,n);

l(1)=key;

for i=2:m*n

l(i)=1-2*l(i-1)*l(i-1);

end

%二值化的时候需要注意,此处的临界值是0,而不是0.5

key=0;

for i=1:m*n

if (l(i)>=key)

l(i)=0;

else

l(i)=1;

end

% waitbar((i/(m*n)),h);

end

l=~l;

% imwrite(l,'image/wm.bmp');

%进度条

h= waitbar(0,'程序处理中,请耐心等待。。。');

for i=1:100, % computation here %

waitbar(i/100);

end

close(h) ;

imwrite(l,os);

% handles.figure;

subplot(1,1,1),imshow(l),title('混沌图像');

% test=imread(os);

% figure,imshow(test,[]);

clear;

clc;

% key=0.98

%key为解密密钥

%os原始图像的路径

[filename pathname]=uigetfile('*.bmp;*.tiff;*.tif', '读入需要解密的图像');

os=[pathname filename];

ozl=imread(os);

% figure,imshow(ozl,[]);

%%%%%%%%%%%%%设置混沌初始值

prompt={'混沌初始值 '};

title='初值';

lines=1;

def={'98'};

mya=inputdlg(prompt,title,lines,def,'on');

key11=['0.',mya{1}];

key=str2num(key11);

%%%%%%%%%%%%设置混沌初始值

%计算原始图像的大小

[m n]=size(ozl);

%l=zeros(m,n);

l=linspace(0,0,m*n);

l(1)=key;

for i=2:m*n

l(i)=1-2*l(i-1)*l(i-1);

end

% %对生成的混沌序列进行排序

% [lsort lindex]=sort(l);

% t=linspace(0,0,m*n);

% t(1)=0.98;

% for i=1:m*n

% t(i+1)=1-2*t(i)*t(i);

% end

% for i=1:m*n

% if (l(i)>=0)

% l(i)=1;

% else

% l(i)=0;

% end

% end

% l=~l;

% subplot(2,2,2),imshow(l),title('混沌图像');

[lsort,lindex]=sort(l);

%ozl =original zhi luan 表示置乱后的原始图像

% ozl=zeros(m,n);

% for i=1:m*n

% ozl(i)=o(lindex(i));

% end

% ozl=reshape(ozl,m,n);

%%oo =original fu yuan

ofy=zeros(m,n);

for i=1:m*n

ofy(lindex(i))=ozl(i);

end

h= waitbar(0,'程序处理中,请耐心等待。。。');

for i=1:100, % computation here %

waitbar(i/100)

end

close(h) ;

subplot(1,2,1),imshow(ozl,[]);

%,title('需要解密的图像');

set(gca,'Title',text('String','需要解密的图像'))

subplot(1,2,2),imshow(ofy,[]);

%,title('解密后图像');

set(gca,'Title',text('String','解密后图像'))

% [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后图像');

% os=[pathname filename];

% imwrite(ozl,os);

% subplot(1,3,3),imshow(ofy,[]),title('复原图像');

clear;

clc;

%key为加密密钥

%os原始图像的路径

[filename pathname]=uigetfile('*.bmp;*.tiff;*.tif', '读入需要加密的图像');

os=[pathname filename];

[filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后的图像');

ozls=[pathname filename];

% key=0.98;

%%%%%%%%%%%%读入混沌初始值

prompt={'混沌初始值 '};

title='初值';

lines=1;

def={'98'};

mya=inputdlg(prompt,title,lines,def,'on');

key11=['0.',mya{1}];

key=str2num(key11);

%%%%%%%%%%%%

o=imread(os);

% figure,imshow(o);

%计算原始图像的大小

[m n]=size(o);

%l=zeros(m,n);

l=linspace(0,0,m*n);

l(1)=key;

for i=2:m*n

l(i)=1-2*l(i-1)*l(i-1);

end

% %对生成的混沌序列进行排序

% [lsort lindex]=sort(l);

% t=linspace(0,0,m*n);

% t(1)=0.98;

% for i=1:m*n

% t(i+1)=1-2*t(i)*t(i);

% end

% for i=1:m*n

% if (l(i)>=0)

% l(i)=1;

% else

% l(i)=0;

% end

% end

% l=~l;

% subplot(2,2,2),imshow(l),title('混沌图像');

[lsort,lindex]=sort(l);

%ozl =original zhi luan 表示置乱后的原始图像

ozl=zeros(m,n);

for i=1:m*n

ozl(i)=o(lindex(i));

end

% ozl=reshape(ozl,m,n);

%%oo =original fu yuan

% ofy=zeros(m,n);

% for i=1:m*n

% ofy(lindex(i))=ozl(i);

% end

% h= waitbar(0,'程序处理中,请耐心等待。。。');

% for i=1:100, % computation here %

% waitbar(i/100)

% end

% close(h) ;

%首先保存需要加密的图像

ozl=uint8(ozl);

ozl=double(ozl);

imwrite(ozl,ozls);

subplot(1,2,1)

imshow(o)

% title('原始图像')

set(gca,'Title',text('String','原始图像'))

subplot(1,2,2),imshow(ozl,[]);

set(gca,'Title',text('String','加密后图像'))

% title('加密后图像');

% [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后图像');

% os=[pathname filename];

% % [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存图像');

% % os=[pathname filename];

% imwrite(ozl,os);

% subplot(1,3,3),imshow(ofy,[]),title('复原图像');

% [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后的图像');

% os=[pathname filename];

% %进度条

% % h= waitbar(0,'程序处理中,请耐心等待。。。');

% % for i=1:100, % computation here %

% % waitbar(i/100);

% % end

% % close(h) ;

% ozl=uint8(ozl);

% imwrite(ozl,os);

% figure,imshow(ozl,[]);

% test=imread(os);

% figure,imshow(test,[]);

你可能感兴趣的:(数字水印)