使用matlab在较小的rgb图像中隐藏较大的二进制图像的代码

时间:2014-02-25 11:37:39

标签: matlab

我写了一个matlab代码,用于在较小的彩色图像中隐藏较大的黑白图像,方法是在r分量的lsb中设置黑白图像的第一位,在g分量的lsb中设置第2位,在第3位中设置第3位lsb是彩色图像的b分量,依此类推。但是在解密过程中只有一半的图像被恢复了。我能纠正它吗?

//Encryption

% read in the cover object you want to use for embedding
file_name='ballon.png';
cover_object=imread(file_name);
cover_object=imresize(cover_object,[200 200]);
cr=cover_object(:,:,1);
cg=cover_object(:,:,2);
cb=cover_object(:,:,3);


% read the message image you want to hide in the cover image
file_name='man.png';
message=imread(file_name);
message=imresize(message,[600 600]);

% conversions needed to spread the image values on a 256 gray-scale
message=double(message);
message=round(message/256);
figure(1),imshow(message),title('message');

message=uint8(message);

% determine the size of cover image used for embedding
Mc=size(cr,1);  %Height
Nc=size(cr,2);  %Width

% determine the size of message object to embed
Mm=size(message,1);         %Height
Nm=size(message,2);         %Width


%y = uint8(wgn(Mm,Nm,1));


% set the LSB of cover_object(ii,jj) to the value of the MSB of watermark(ii,jj)
wr=cr;
wg=cg;
wb=cb;
i=1;
j=1;
for ii= 1:Mc
 for jj = 1:Nc

   wr(ii,jj)=bitset(wr(ii,jj),1,message(i,j));

   wg(ii,jj)=bitset(wg(ii,jj),1,message(i+1,j));

   wb(ii,jj)=bitset(wb(ii,jj),1,message(i+2,j));

   if j == Nm
     j=1;
     i=i+3;
   else
     j=j+1;
   end

 end

end

message_image=cat(3,wr,wg,wb);



% write to file the image
imwrite(message_image,'lsb_message.bmp','bmp');


% display encoded image
figure(2)
imshow(message_image,[])
title('encoded Image')



//Decryption



% read in encoded cover image
file_name='lsb_message.bmp';
message_image=imread(file_name);

% determine size of encoded cover image
Mw=size(message_image,1);   %Height
Nw=size(message_image,2);   %Width

enc=message_image;
er=enc(:,:,1);
eg=enc(:,:,2);
eb=enc(:,:,3);


% use lsb of encoded cover image to recover message
for ii = 1:Mw
    for jj = 1:Nw
        msg(i,j)=bitget(er(ii,jj),1);
        msg(i+1,j)=bitget(eg(ii,jj),1);
        msg(i+2,j)=bitget(eb(ii,jj),1);
        if j == Nm
         j=1;
         i=i+3;
        else
         j=j+1;
        end

    end
end




% scale the recovered message image
msg=256*double(msg);



% scale and display recovered message image
figure(1)
imshow(msg)
title('Recovered image');

0 个答案:

没有答案