信息隐藏实验LSB隐写随机LSB隐写RS隐写分析.docx
《信息隐藏实验LSB隐写随机LSB隐写RS隐写分析.docx》由会员分享,可在线阅读,更多相关《信息隐藏实验LSB隐写随机LSB隐写RS隐写分析.docx(17页珍藏版)》请在冰豆网上搜索。
信息隐藏实验LSB隐写随机LSB隐写RS隐写分析
信息隐藏实验二
LSB隐写分析
姓名:
周伟康
学号:
班级:
一:
实验要求
1、针对自己实现的隐写算法(嵌入、提取),计算隐蔽载体的PSNR值,通过PSNR值来评估隐写对图像质量的影响,并与主观感受做对比。
2、实现一种隐写分析方法,对隐蔽载体进行检测(卡方、RS……)
二:
实验步骤
1、编写随机选点函数,完善顺序和随机两种LSB信息嵌入和提取。
%随机间隔选点函数
%[row,col]=randinterval(test,60,1983);
function[row,col]=randinterval(matrix,count,key)
[m,n]=size(matrix);
interval1=floor(m*n/count)+1;
interval2=interval1-2;
ifinterval2==0
error('载体太小,不能将秘密消息隐藏其内!
');
end
rand('seed',key);
a=rand(1,count);
%initialize
row=zeros([1count]);
col=zeros([1count]);
r=1;c=1;
row(1,1)=r;
col(1,1)=c;
fori=2:
count
ifa(i)>=0.5
c=c+interval1;
else
c=c+interval2;
end
ifc>n
r=r+1;
ifr>m
error('载体太小,不能将秘密消息隐藏其内!
');
end
c=mod(c,n);
ifc==0
c=1;
end
end
row(1,i)=r;
col(1,i)=c;
end
选取8*8的矩阵测试
2、对比原始图像和隐藏信息后图像,计算隐蔽载体的均方差(MSE)进而计算峰值信噪比(PSNR),评估隐写对图像质量的影响。
%输入格式:
F=compare('D:
\课件\hide\pic\2.jpg','scover.jpg');
functionF=compare(original,hided)
W=imread(original);
E=imread(hided);
ifany(size(W)~=size(E))
error('尺寸不同,不能比较');
end
F=W-E;
%F=double(F)/255;
imshow(mat2gray(double(F)/255));
[psnr,mse]=qualify(F);
function[PSNR,MSE]=qualify(Delta)
%计算MSE和PSNR;
MSE=sum(Delta(:
).^2)/prod(size(W));
PSNR=10*log10(255^2/MSE);
end
title(['差异对比PSNR=',num2str(psnr)]);
end
测试代码:
clc;clear;
key=20112910;
originalfile='D:
\¿Î¼þ\hide\pic\b.bmp';
messagefile='D:
\¿Î¼þ\hide\pic\message.txt';
scoverfile1='D:
\¿Î¼þ\hide\pic\scover1.bmp';
scoverfile2='D:
\¿Î¼þ\hide\pic\scover2.bmp';
getfile1='D:
\¿Î¼þ\hide\pic\secret1.txt';
getfile2='D:
\¿Î¼þ\hide\pic\secret2.txt';
[ste_cover,len_total]=LSBhide(originalfile,messagefile,scoverfile1);
subplot(233);
compare(originalfile,scoverfile1);
x=LSBget(scoverfile1,len_total,getfile1);
%%ff
[ste_cover2,len_total2]=randLSBhide(originalfile,messagefile,scoverfile2,key);
subplot(236);
compare(originalfile,scoverfile2);
x=randLSBget(scoverfile2,len_total2,getfile2,key);
隐写提取结果:
(成功)
实验结果1:
实验结果2:
结果分析:
后者PSNR大说明随机LSB比顺序LSB隐写安全性更好。
3、对256×256像素,256色的灰度图像进行RS隐写分析。
RS原理:
给定一个图像块,Zigzag扫描,排列成一个向量G=(x1,x2……xn)图像的空间相关性
定义翻转函数:
F1:
2i与2i+1的相互变化关系F-1:
2i-1与2i的相互变化关系F0:
不变关系
RS分析步骤:
将待检测图像分成大小相同的图像块计算每个图像块的空间相关性对每个图像块应用非负翻转(F1,F0)
RM=空间相关性增加的图像块个数/图像块总个数SM=空间相关性减小的图像块个数/图像块总个数
对每个图像块应用非正翻转(F-1,F0)R-M=空间相关性增加的图像块个数/图像块总个数
S-M=空间相关性减小的图像块个数/图像块总个数若R-M-S-M>>RM–SM,则有LSB隐写。
%RS方法对256*256像素,256色灰度图进行LSB隐写检测。
%y=RSanalysis(originalfile);
functionfff=RSanalysis(input)
org=imread(input);
org=double(org);
org=org(:
)';
[row,len]=size(org);
rs=zeros(2,4);
fx=zeros(1,3);
m=floor(len/4);
M=randsrc(4,1,[0,1]);
%temp=zeros(4,1);
%temp=[27,28,26,25];
M=[1010];
stg=org;
fori=1:
2
forj=1:
m
temp=stg((j-1)*4+1:
j*4);
%disp(temp);
fx
(1)=discrimination(temp);
fx
(2)=discrimination(fpos(temp,M));
fx(3)=discrimination(fneg(temp,M));
iffx
(2)>fx
(1)
rs(i,1)=rs(i,1)+1;
end
iffx
(2)(1)
rs(i,2)=rs(i,2)+1;
end
iffx(3)>fx
(1)
rs(i,3)=rs(i,3)+1;
end
iffx(3)(1)
rs(i,4)=rs(i,4)+1;
end
end
ifi==1
stg=fpos(stg,ones(len,1));
end
end
fff=(rs(1,3)-rs(1,4))>(rs(1,1)-rs(1,2));
iffff
disp('含有隐藏信息');
else
disp('未检测出隐藏信息。
。
。
');
end
rs=rs/m;
dpz=rs(1,1)-rs(1,2);dpo=rs(2,1)-rs(2,2);
dnz=rs(1,3)-rs(1,4);dno=rs(2,3)-rs(2,4);
C=[2*(dpo+dpz),(dnz-dno-dpo-3*dpz),(dpz-dnz)];
z=roots(C);
p=z./(z-0.5);
fprintf(1,'隐写率为:
%f\n',p
(2));
fprintf(1,'隐写长:
%f\n',p
(2)*row*len);
end
functionv=discrimination(t)
v=abs(t
(2)-t
(1))+abs(t(3)-t
(2))+abs(t(4)-t(3));
end
functionr=fpos(t,m)
r=t;
MM=size(m,2);
forq=1:
MM
ifm(q)==1
ifmod(t(q),2)==0
r(q)=r(q)+1;
else
r(q)=r(q)-1;
end
ifr(q)<0
r(q)=255;
elseifr(q)>255
r(q)=0;
end
end
end
end
functionr=fneg(t,m)
r=t;
forq=1:
size(m)
ifm(q)==1
ifmod(t(q),2)==0
r(q)=r(q)-1;
else
r(q)=r(q)+1;
end
end
ifr(q)<0
r(q)=255;
elseifr(q)>255
r(q)=0;
end
end
end
测试代码:
y=RSanalysis(originalfile);
y=RSanalysis(scoverfile1);
y=RSanalysis(scoverfile2);
实验数据1:
(对应Bird图隐写)
实验数据2:
(对应Lena图隐写)
结果分析:
隐写检测成功!
但是隐写率和隐写消息长度估计严重有误,应该是参数计算有误。
三:
总结
对Matlab进一步学习后,初步掌握了函数的编写方法,将本次和上次实验代码修改为函数封装调用的形式,使编码结构更清晰,编写修改更方便。
代码后若无’;’,执行时会产生大量中间过程输出,耽误运行和查看。
故要养成写分号的习惯,若必要的输出用disp或加注释醒目。
对不符合大小尺寸的隐蔽体,将无法分析出正确结果。
对于几个字节的短消息隐藏载体,也无法检测到隐藏了信息。
隐写率和隐写信息长估计都没有调试成功,对算法还没有完全理解通透。
附:
顺序LSB和随机LSB隐写,提取代码:
LSBhide.m
%LSB上的顺序信息隐藏,载体选用灰度bmp图
%输入格式:
[ste_cover,len_total]=
%LSBhide('D:
\课件\hide\pic\2.jpg','D:
\课件\hide\pic\message.txt','scover.jpg');
function[ste_cover,len_total]=LSBhide(input,file,output)
%读入图像矩阵
cover=imread(input);
ste_cover=cover;
%ste_cover=double(ste_cover);
%将文本文件转换为二进制序列
f_id=fopen(file,'r');
[msg,len_total]=fread(f_id,'ubit1');
fprintf('隐藏消息长度:
%d\n',len_total);
%溢出检测
[m,n]=size(ste_cover);
iflen_total>m*n
error('嵌入消息量过大,请更换图像!
');
end
iflen_total<=0
error('请输入隐藏消息!
');
end
p=1;
forf2=1:
n
forf1=1:
m
ste_cover(f1,f2)=ste_cover(f1,f2)-mod(ste_cover(f1,f2),2)+msg(p,1);
ifp==len_total
break;
end
p=p+1;
end
ifp==len_total
break;
end
end
%生成信息隐藏后的图像
imwrite(ste_cover,output);
%显示结果
subplot(231);imshow(cover);title('原始图像');
subplot(232);imshow(output);title('LSB隐藏后图像');
LSBget.m
%result=LSBget('scover.bmp',length,'secret.txt')
functionresult=LSBget(output,len_total,goalfile)
ste_cover=imread(output);
%ste_cover=double(ste_cover);
[m,n]=size(ste_cover);
frr=fopen(goalfile,'w');
iflen_total<=0
error('请输入隐藏消息!
');
end
p=1;
result=ones(len_total,1);
forf2=1:
n
forf1=1:
m
result(p,1)=mod(ste_cover(f1,f2),2);
ifp==len_total
break;
end
p=p+1;
end
ifp==len_total
break;
end
end
fwrite(frr,result,'ubit1');
fclose(frr);
randLSBhide.m
%LSB上的随机信息隐藏,载体选用灰度bmp图
%输入格式:
[ste_cover,len_total]=
%randLSBhide('D:
\课件\hide\pic\2.jpg','D:
\课件\hide\pic\message.txt','scover.jpg');
function[ste_cover,len_total]=randLSBhide(input,file,output,key)
%读入图像矩阵
cover=imread(input);
ste_cover=cover;
%ste_cover=double(ste_cover);
%将文本文件转换为二进制序列
f_id=fopen(file,'r');
[msg,len_total]=fread(f_id,'ubit1');
fprintf('隐藏消息长度:
%d\n',len_total);
%溢出检测
[m,n]=size(ste_cover);
iflen_total>m*n
error('嵌入消息量过大,请更换图像!
');
end
iflen_total<=0
error('请输入隐藏消息!
');
end
p=1;
[row,col]=randinterval(ste_cover,len_total,key);
fori=1:
len_total
ROW=row(i);
COL=col(i);
ste_cover(ROW,COL)=ste_cover(ROW,COL)-mod(ste_cover(ROW,COL),2)+msg(p,1);
ifp==len_total
break;
end
p=p+1;
end
%生成信息隐藏后的图像
imwrite(ste_cover,output);
%显示结果
subplot(234);imshow(cover);title('原始图像');
subplot(235);imshow(output);title('随机LSB隐藏后图像');
randLSBget.m
%result=randLSBget('scover.bmp',length,'secret.txt')
functionresult=randLSBget(output,len_total,goalfile,key)
ste_cover=imread(output);
%ste_cover=double(ste_cover);
[m,n]=size(ste_cover);
frr=fopen(goalfile,'w');
iflen_total<=0
error('请输入隐藏消息!
');
end
p=1;
result=ones(len_total,1);
[row,col]=randinterval(ste_cover,len_total,key);
fori=1:
len_total
ROW=row(i);
COL=col(i);
result(p,1)=mod(ste_cover(ROW,COL),2);
ifp==len_total
break;
end
p=p+1;
end
fwrite(frr,result,'ubit1');
fclose(frr);