MATLAB__LSB信息隐藏与提取

COMMAMD


1、顺序法和随机法的嵌入、提取、lsb图像对比显示

 MATLAB__LSB信息隐藏与提取


MATLAB__LSB信息隐藏与提取

MATLAB__LSB信息隐藏与提取

 

2、 顺序法和随机法信息隐藏位置分布

MATLAB__LSB信息隐藏与提取

 MATLAB__LSB信息隐藏与提取  MATLAB__LSB信息隐藏与提取


  

CODE


lsbord.m(顺序法嵌入)
function hideimg=lsbord(imgfile,info,outfile)
%读取载体图像
img=imread(imgfile);
%转灰度图像
if ndims(img)==3
    img=rgb2gray(img);
end
%读取待嵌入信息
fo=fopen(info,'r');
[msg,len]=fread(fo,'ubit1');
[m,n]=size(img);
%检查嵌入信息长度是否合法
if m*n
    error('wrong!');
end
%顺序循环嵌入隐藏信息
hideimg=img;
for i=1:m
    for j=1:n
        if (i-1)*n+j>len
            break
        end
        if bitand(hideimg(i,j),1)==msg((i-1)*n+j)
            continue
        else
            if msg((i-1)*n+j)==1
                hideimg(i,j)=hideimg(i,j)+1;
            else
                hideimg(i,j)=hideimg(i,j)-1;
        end
    end
end
imwrite(hideimg,outfile);
end

lsbextraxt_ord.m(顺序法提取)
function msg=lsbextract_ord(hideimg,len,outfile)
%读取待提取图像
img=imread(hideimg);
[m,n]=size(img);
%判断想提取的信息长度是否合法
if len>m*n
    error('wrong!');
end
%初始化缓冲变量
msg=zeros(1,len);
%顺序循环读取隐藏信息到缓冲变量
for i=1:m
    for j=1:n
        if (i-1)*n+j>len
            break
        end
        msg(1,(i-1)*n+j)=bitand(img(i,j),1);
    end
end
%将信息从缓冲变量写入文件
fi=fopen(outfile,'w');
fwrite(fi,msg,'ubit1');
fclose(fi);
end


lsbrand.m(随机法嵌入)
function hideimg=lsbrand(imgfile,info,key,outfile)
%读取载体图像
img=imread(imgfile);
%转灰度图像
if ndims(img)==3
    img=rgb2gray(img);
end
%读取嵌入信息
fi=fopen(info,'r');
[msg,len]=fread(fi,'ubit1');
[m,n]=size(img);
%检查嵌入信息是否合法
if len>m*n
    error('wrong!');
end
%生成随机序列对
[x,y]=randxy(img,len,key);
%嵌入隐藏信息
hideimg=img;
for i=1:len
    if bitand(hideimg(x(i),y(1,i)),1)==msg(i)
        continue
    else
        if msg(i)==1
            hideimg(x(i),y(i))=hideimg(x(i),y(i))+1;
        else
            hideimg(x(i),y(i))=hideimg(x(i),y(i))-1;
        end
    end
end
imwrite(hideimg,outfile);
end

randxy.m(生成随机序列对)
function [x,y]=randxy(matrix,len,key)
%计算步长,两个步长随机交替,确保插入的随机性
[m,n]=size(matrix);
step1=floor(m*n/len)+1;
step2=step1-3;
if step2<=0
    error('wrong!');
end
%设置随机种子,生成一串随机数
rand('seed',key);
temp=rand(1,len);
%根据随机数计算随机序列对(x,y)
locate=1;
x=zeros(1,len);
y=zeros(1,len);
x(1,1)=locate;
y(1,1)=locate;
for i=2:len
    if temp(i)>0.5
        locate=locate+step1;
    else
        locate=locate+step2;
    end
    x(1,i)=floor(locate/n)+1;
    if mod(locate,n)==0
        y(1,i)=n;
    else
        y(1,i)=mod(locate,n);
    end
end
end

lsbextract_rand.m(随机法提取)
function msg=lsbextract_rand(hideimg,len,key,outfile)
%读取待提取图像
img=imread(hideimg);
[m,n]=size(img);
%判断想提取的信息长度是否合法
if len>m*n
    error('wrong!');
end
%还原随机序列对,确定提取位置
[x,y]=randxy(img,len,key);
%初始化缓冲变量
msg=zeros(1,len);
%根据序列对提取隐藏信息
for i=1:len
    msg(i)=bitand(img(x(i),y(i)),1)
%将提取的信息写入文件
fi=fopen(outfile,'w');
fwrite(fi,msg,'ubit1');
fclose(fi);
  end

compare.m(lsb图像信息隐藏位置显示)
function test=compare(original,lsb)
%读取两幅图像
img1=imread(original);
img2=imread(lsb);
%double类型转换
img1=double(img1)/255;
img2=double(img2)/255;
%差值二值化显示
test=img2-img1;
imshow(mat2gray(test));
end






你可能感兴趣的:(MATLAB__LSB信息隐藏与提取)