📄 steg.m
字号:
% Steganography
%
%Hide an image in another image,using LSB method
%
%There is an image in 4447.png
%Run this m file and choose 2nd option for image 4447.png to
%see that secret image
k=menu(' ','Hide an image','Recover an image');
rn=num2str(rand(1,1));
switch(k)
case 1
%case 1 :Encryption(hide secret image in cover image)
%select a cover image
[filename, pathname] = uigetfile( {'*.jpg';'*.jpeg';'*.bmp';'*.*'}, ...
'Select cover image');
I=imread([pathname filename]);
imageinfo_cover=imfinfo([pathname filename]);
image_height=imageinfo_cover.Height;
image_width=imageinfo_cover.Width;
equ=((image_height-1)*(image_width-mod(image_width,8)))/8;
val_red=I(:,:,1); %get the red color matrix
msgbox(['Choose a secret image with product of width and height less than ' num2str(equ)])
%select secret image
[filename, pathname] = uigetfile( {'*.jpg';'*.png';'*.*'}, ...
'Select secret image');
I_sec=imread([pathname filename]);
imageinfo_sec=imfinfo([pathname filename]); %get information of secret image
i_sec_height=imageinfo_sec.Height; % secret image height
i_sec_width=imageinfo_sec.Width; % secret image width
val_red=double(val_red);
%hide the secret image height
i_sec_height_bin=de2bi(i_sec_height,16);
val_red(1,1:16)=bitset(val_red(1,1:16),1,i_sec_height_bin);
%hide the secret image width
i_sec_width_bin=de2bi(i_sec_width,16);
val_red(1,17:32)=bitset(val_red(1,17:32),1,i_sec_width_bin);
I(:,:,1)=val_red;
i_sec_length=i_sec_height*i_sec_width;
I_sec_bin=zeros(i_sec_length*3,8);
I_sec_bin=de2bi(double(I_sec)); %convert the secret image to binary
Ipix_counter=1; %set a counter for the pixels
len=mod(image_width,8);
len=image_width-len;
for count_hi=2:image_height
count_wi=1;
for count_wi=1:8:len-8
val_red(count_hi,count_wi:count_wi+7)=...
bitset(val_red(count_hi,count_wi:count_wi+7),1,I_sec_bin(Ipix_counter,:));
Ipix_counter=Ipix_counter+1;
if Ipix_counter>i_sec_length*3
break;
end
end
if Ipix_counter>i_sec_length*3
break;
end
end
I(:,:,1)=val_red;
imwrite(I,[rn(3:end) '.png'],'png');
msgbox(['The secret image ' filename ' is in ' rn(3:end) '.png'])
case 2
%case 2:Dercyption(Reocver the secret image from cover image)
%select the encrypted cover image
[filename, pathname] = uigetfile( {'*.png'}, ...
'Select an encrypted cover image');
I=imread([pathname filename]);
imageinfo_cover=imfinfo([pathname filename]);%cover image information
image_height=imageinfo_cover.Height; %cover image height
image_width=imageinfo_cover.Width; %cover image width
val_red=I(:,:,1); %get the red color matrix
%extract the secret image height and width from 1st 32pixel of cover image
i_sec_height=bi2de(bitget(double(val_red(1,1:16)),1));
i_sec_width=bi2de(bitget(double(val_red(1,17:32)),1));
i_sec_length=i_sec_height*i_sec_width;
I_sec_bi=zeros(i_sec_length*3,8);%initialize a zero matrix
Ipix_counter=1; %counter for pixels
len=mod(image_width,8);
len=image_width-len;
for count_hi=2:image_height
count_wi=1;
for count_wi=1:8:len-8
I_sec_bi(Ipix_counter,1:8)=...
bitget(val_red(count_hi,count_wi:count_wi+7),1);
Ipix_counter=Ipix_counter+1;
if Ipix_counter>i_sec_length*3
break;
end
end
if Ipix_counter>i_sec_length*3
break;
end
end
image1=reshape(bi2de(I_sec_bi),i_sec_height,i_sec_width,3);
image1=uint8(image1);
imwrite(image1,[num2str(rn(3:end)) '.png'],'png');
msgbox(['The secret image ' rn(3:end) '.png is extracted from ' filename ' ,it is in your current directory']);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -