(完整word版)(绝对可运行)加密图像中的可逆数据隐藏算法matlab代码(基于张新鹏教授的算法)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
%This is the program of Reversible Data Hiding in Encrypted Domain
clc;
clear;
%===============Read image=========================== original_p=rgb2gray(imread('LENA.tif'));
[m,n]=size(original_p);
%==============Image encryption====================== random_bits=rand(m,n*8)<=0.5; %伪随机序列
for i=1:m
for j=1:n
s=0;
for k=0:7
b(k+1)=mod(fix(double(original_p(i,j))/(2^k)),2);
eb(k+1)=xor(b(k+1),random_bits(i,8*j+k-7));
s=s+eb(k+1)*(2^k);
end
encrypted_image(i,j)=s;
end
end
figure(1);
subplot(1,2,1);
imshow(original_p);
hold on;
title('Original image','fontsize',16);
subplot(1,2,2);
encrypted_image=uint8(encrypted_image);
imshow(encrypted_image);
title('Encrypted image','fontsize',16);
%================Data embedding====================== size=8; %分块大小
N=6;%置乱周期
number1=3;
number2=N-number1;
lim_row=fix(m/size); %图像的分块数
lim_col=fix(n/size); %图像的分块数
bitts=3;
watermessage=rand(lim_row,lim_col)<0.5; %水印信息
%======pseudo_randomly=======
for i=1:lim_row
for j=1:lim_col
block_image{i,j}=encrypted_image((size*i-size+1):size*i,(size*j-size+1):size*j); %分块
for k=1:size
for t=1:size
index=arnold(k,t,size,number1);%arnold is permutateion function;
arno_block_image{i,j}(index(1)+1,index(2)+1)=block_image{i,j}(k,t);
end
end
%数据嵌入
sum0=zeros(size/2,size);
if watermessage(i,j)==0
for k=0:7
bbb{k+1}=mod(fix(double(arno_block_image{i,j}(1:size/2,:))/(2^k)),2);
if k bbb{k+1}=~bbb{k+1}; end sum0=sum0+bbb{k+1}*(2^k); end data_image{i,j}=[sum0;double(arno_block_image{i,j}(size/2+1:size,:))]; end sum1=zeros(size/2,size); if watermessage(i,j)==1 for k=0:7 bbb{k+1}=mod(fix(double(arno_block_image{i,j}(size/2+1:size,:))/(2^k)),2); if k bbb{k+1}=~bbb{k+1}; end sum1=sum1+bbb{k+1}*(2^k); end data_image{i,j}=[double(arno_block_image{i,j}(1:size/2,:));sum1]; end for k=1:size for t=1:size index=arnold(k,t,size,number2);%arnold is permutateion function; re_data_image{i,j}(index(1)+1,index(2)+1)=data_image{i,j}(k,t); end end %合成嵌入数据后的图像 data_en_image((size*i-size+1):size*i,(size*j-size+1):size*j)=re_data_image{i,j}; end end %=====Encrypt image which contains data==== for i=1:lim_row*size for j=1:lim_col*size s=0; for k=0:7 b(k+1)=mod(fix(data_en_image(i,j)/(2^k)),2); eb(k+1)=xor(b(k+1),random_bits(i,8*j+k-7)); s=s+eb(k+1)*(2^k); end de_data_image(i,j)=s; end end figure(2); hold on; subplot(1,2,1); imshow(original_p); title('Original image','fontsize',16); subplot(1,2,2); imshow(uint8(de_data_image)); title('Decrypted image contains data','fontsize',16); %直接解密图像的峰值信噪比 sumsss=0; for i=1:lim_row*size for j=1:lim_col*size sumsss=(de_data_image(i,j)-double(original_p(i,j)))^2+sumsss; end end mse=sumsss/((lim_row*size)*(lim_row*size)); psnr=10*log10(255^2/mse); %======Extract data and recover image======= for i=1:lim_row for j=1:lim_col block_de_image{i,j}=de_data_image((size*i-size+1):size*i,(size*j-size+1):size*j); %分块 for k=1:size for t=1:size index=arnold(k,t,size,number1);%arnold is permutateion function; ar_block_de_image{i,j}(index(1)+1,index(2)+1)=block_de_image{i,j}(k,t); end end sum0=zeros(size/2,size); sum1=zeros(size/2,size);