I did PVD steganography for hiding text in color image can any one please help me to use the same code for hiding color image inside color cover image in matlab?

I did PVD steganography for hiding text in color image can any one please help me to use the same code for hiding color image inside color cover image in matlab? The code for hiding text inside color cover image and extracting from it is given below.
clc;
close all;
clear;
%%%%%%%%%%%%%%%%%%%%%%%Reading a Secret Text File %%%%%%%%%%%%%%%%%%%%%%%%
file_id = fopen('message.txt','r');
file_content = fread(file_id);
file_length = length(file_content);
in = [];
in = [in dec2bin(file_length,20)]; %character to binary conversion
for i=1:file_length
in=[in dec2bin(file_content(i),7)];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%Reading Cover Image %%%%%%%%%%%%%%%%%%%%%%%%%%%
cover_image = imread('cover_image.bmp'); %get cover image
red = cover_image(:,:,1); %seperating rgb values
blue = cover_image(:,:,2);
green = cover_image(:,:,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Embedding Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
color = red; %red color selected for embedding
[r,c] = size(color);
final = double(color);
next=0;
capacity=0; %total no of bits that can be embedded
for x=0:1:r-1
for y=0:2:c-1
enable = 1; %enable=0 when new pixels may fall off the boundary
p = color(1+x,1+y:2+y); %block of two pixels, pi & pi+1
p = double(p);
d = p(1,2) - p(1,1); %d = difference between 2 pixel
d_abs = abs(d); %absolute difference
lb=[0 8 16 32 64 128]; %lowerbound
ub=[7 15 31 63 127 255]; %upperbound
for i=1:1:6 %test the R boundary
if((d_abs >= lb(i)) && (d_abs <= ub(i))) %selecting range
%check if any pixel in a block fall off the boundary [0,255]
even2 = mod(d,2);
m2 = ub(i) - d;
if (even2 == 0)
Pcheck=[p(1,1)-floor(m2/2) p(1,2)+ceil(m2/2)];
else
Pcheck=[p(1,1)-ceil(m2/2) p(1,2)+floor(m2/2)];
end
if(Pcheck(1)<0 || Pcheck(2)<0 || Pcheck(1)>255 || Pcheck(2)>255)
enable = 0;
break
end
n = ub(i)-lb(i)+1; %quantization width of range
t = floor(log2(n)); %maximum bit can be embedded in 2 pixels
capacity=capacity+t; %max capacity of the cover image
%check if next exceeds the length of message
if(next>length(in))
m=0;
%check if next+t exceeds the length of message
elseif(next+t>length(in))
if(1+next>=length(in))
k=zeros(1,t);
else
k=in(1+next:length(in));
end
diff =next+t-length(in);
k1=zeros(1,t);
if(diff>0)
for j=1:next+t-length(in)
k1(j)=k(j);
end
end
k=k1;
next=next+t;
k=bin2dec(char(k));
if(1+next>length(in))
m=0;
else
if(d >= 0)
dnew = k + lb(i);
else
dnew = -(k + lb(i));
end
m = dnew - d;
end
%if next is less than the length of message
else
k=in(1+next:t+next);
next=next+t;
k=bin2dec(char(k));
if(d >= 0)
dnew = k + lb(i);
else
dnew = -(k + lb(i));
end
m = dnew - d;
end
end
end
if (enable == 1)
even = mod(d,2);
if (even == 0)
P0=[p(1,1)-floor(m/2) p(1,2)+ceil(m/2)];
else
P0=[p(1,1)-ceil(m/2) p(1,2)+floor(m/2)];
end
final(1+x,1+y)=P0(1,1);
final(1+x,2+y)=P0(1,2);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%Creating Stego-Image %%%%%%%%%%%%%%%%%%%%%%%%%%
if(next>length(in))
disp('Message Embedded Successfully');
final = uint8(final);
stego_image = cat(3,final,blue,green);
imwrite(stego_image,'stego_image.bmp');
fclose('all');
else %check if the cover is samll for the given messege to be embedded
error('Cover Image is too small for the given messege to be embedded, please replace cover image with the larger one.');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%Reading Stego Image %%%%%%%%%%%%%%%%%%%%%%%%%%%
stego_image = imread('stego_image.bmp');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Extracting Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%
color = stego_image(:,:,1); %red color selected where data is embedded
[r,c]=size(color);
j=0;
msg = [];
flag = 0;
length=0;
enable = 1;
for x=0:1:r-1
for y=0:2:c-1
if (enable == 1) %enable=0 when any pixels may fall off the boundary
gp = color(1+x,1+y:2+y); %block of 2 pixels, pi & pi+1
gp = double(gp);
d = gp(1,2) - gp(1,1); %d = difference between 2 pixel
nd = abs(d); %absolute difference
lb = [0 8 16 32 64 128]; %lowerbound
ub = [7 15 31 63 127 255]; %upperbound
for i=1:1:6 %test the R boundary
if(nd>=lb(i)&&nd<=ub(i))
%check if any pixel in a block fall off the boundary [0,255]
even2 = mod(d,2);
m2 = ub(i) - d;
if (even2 == 0)
Pcheck=[gp(1,1)-floor(m2/2) gp(1,2)+ceil(m2/2)];
else
Pcheck=[gp(1,1)-ceil(m2/2) gp(1,2)+floor(m2/2)];
end
if(Pcheck(1)<0 || Pcheck(2)<0 || Pcheck(1)>255 || Pcheck(2)>255)
break
end
w = ub(i)-lb(i)+1; %quantization width of range
t=log2(w); %maximum bit can be embedded between 2 pixel
b = nd - lb(i);
k=dec2bin(b,t);
msg = [msg k];
j=j+t;
if(flag==0 && j>=20)
length=bin2dec(msg(1:20))+3; %possible 3 char error
length=length*7;
flag=1;
end
if(flag==1 && j>=length)
j=1;
for i=20:7:length-7
finaltxt(j)=bin2dec(msg(1+i:7+i));
j=j+1;
end
fid=fopen('output.txt','w');
fwrite(fid,finaltxt);
disp('Message Extracted Successfully');
fclose('all');
enable = 0;
end
end
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2 Comments

hi , can you help me please , my project it about hiding secret message in an image , I find the code valid for me, but do not know why do not implement with me, please can you help thanks

Sign in to comment.

Answers (1)

Change the line
file_id = fopen('message.txt','r');
to instead refer to the image file that you want to hide, and change
in=[in dec2bin(file_content(i),7)];
to use 8 instead of 7.
When you are decoding, expect 8 bits per byte instead of 7.
With this change, you can hide any data file that is stored as a sequence of bites, provided that the length is no more than (2^20 - 1), a limit imposed in your line
in = [in dec2bin(file_length,20)]; %character to binary conversion

9 Comments

Thank you Sir, I did as you have told but now the insertion part works well and the stego image is formed but the extraction part is not working can you please help me . There is no error in the algorithm since it worked well for text data
for dim = 1: 3
em = im2;
em = em(:);
len = length(em);
in = [];
in=[in dec2bin(len,30)];
for i=1:len %character convert to binary
in=[in dec2bin(em(i),8)];
end
a = im1(:,:,dim); %get cover image
[r,c]=size(a);
final=double(a);
next=0;
capacity=0;
%total no of bits that can be embedded
for x=0:1:r-1
for y=0:2:c-1
enable = 1; %enable=0 when new pixels may fall off the boundary
p = a(1+x,1+y:2+y); %block of two pixels, pi & pi+1
p = double(p);
d = p(1,2) - p(1,1); %d = difference between 2 pixel
d_abs = abs(d); %absolute difference
lb=[0 8 16 32 64 128]; %lowerbound
ub=[7 15 31 63 127 255]; %upperbound
for i=1:1:6 %test the R boundary
if((d_abs >= lb(i)) && (d_abs <= ub(i))) %selecting range
%check if any pixel in a block fall off the boundary [0,255]
even2 = mod(d,2);
m2 = ub(i) - d;
if (even2 == 0)
Pcheck=[p(1,1)-floor(m2/2) p(1,2)+ceil(m2/2)];
else
Pcheck=[p(1,1)-ceil(m2/2) p(1,2)+floor(m2/2)];
end
if(Pcheck(1)<0 || Pcheck(2)<0 || Pcheck(1)>255 || Pcheck(2)>255)
enable = 0;
break
end
n = ub(i)-lb(i)+1; %quantization width of range
t = floor(log2(n)); %maximum bit can be embedded in 2 pixels
capacity=capacity+t; %max capacity of the cover image
%check if next exceeds the length of message
if(next>length(in))
m=0;
%check if next+t exceeds the length of message
elseif(next+t>length(in))
if(1+next>=length(in))
k=zeros(1,t);
else
k=in(1+next:length(in));
end
diff =next+t-length(in);
k1=zeros(1,t);
if(diff>0)
for j=1:next+t-length(in)
k1(j)=k(j);
end
end
k=k1;
next=next+t;
k=bin2dec(char(k));
if(1+next>length(in))
m=0;
else
if(d >= 0)
dnew = k + lb(i);
else
dnew = -(k + lb(i));
end
m = dnew - d;
end
%if next is less than the length of message
else
k=in(1+next:t+next);
next=next+t;
k=bin2dec(char(k));
if(d >= 0)
dnew = k + lb(i);
else
dnew = -(k + lb(i));
end
m = dnew - d;
end
end
end
if (enable == 1)
even = mod(d,2);
if (even == 0)
P0=[p(1,1)-floor(m/2) p(1,2)+ceil(m/2)];
else
P0=[p(1,1)-ceil(m/2) p(1,2)+floor(m/2)];
end
final(1+x,1+y)=P0(1,1);
final(1+x,2+y)=P0(1,2);
end
end
end
stego_image(:,:,dim)=final;
end
end
[row,col,dim]=size(Image);
% %RecIm=[];
% j=0;
% length=0;
for p = 1:dim
layer = Image(:,:,p);
msg = [];
flag = 0;
length=0;
[row,col,dim]=size(Image);
j=0;
finaltxt=[];
enable = 1;
% [r1 c1]=size(YourRGBImage);
% length=r1*c1;
for x=0:1:row-1
for y=0:2:col-1
if (enable == 1) %enable=0 when any pixels may fall off the boundary
gp = layer(1+x,1+y:2+y); %block of 2 pixels, pi & pi+1
gp = double(gp);
d = gp(1,2) - gp(1,1); %d = difference between 2 pixel
nd = abs(d); %absolute difference
lb = [0 8 16 32 64 128]; %lowerbound
ub = [7 15 31 63 127 255]; %upperbound
for i=1:1:6 %test the R boundary
if(nd>=lb(i)&&nd<=ub(i))
%check if any pixel in a block fall off the boundary [0,255]
even2 = mod(d,2);
m2 = ub(i) - d;
if (even2 == 0)
Pcheck=[gp(1,1)-floor(m2/2) gp(1,2)+ceil(m2/2)];
else
Pcheck=[gp(1,1)-ceil(m2/2) gp(1,2)+floor(m2/2)];
end
if(Pcheck(1)<0 || Pcheck(2)<0 || Pcheck(1)>255 || Pcheck(2)>255)
break
end
w = ub(i)-lb(i)+1; %quantization width of range
t=log2(w); %maximum bit can be embedded between 2 pixel
b = nd - lb(i);
k=dec2bin(b,t);
msg = [msg k];
j=j+t;
% % length=length(msg);
%
%
if(flag==0 && j>=30)
length=bin2dec(msg(1:30))+3; %possible 3 char error
length=length*7;
flag=1;
end
if(flag==1 && j>=length)
w1=1;
for i=30:7:length-7
finaltxt(w1)=bin2dec(msg(1+i:7+i));
w1=w1+1;
end
RecIm(:,:,p) = reshape(finaltxt,[8 8]);
enable = 0;
end
end
end
end
end
end
end
end
Why are you embedding im2 into all three dimensions of the image? Is that part of the algorithm, to embed the same data three times?
How was im2 created?
Your output seems to be RecIm which is a 3D array whose size is not obvious -- 8 by 8 by something. Which variable holds the extracted data?
Sir im2 is the secret image of size 8*8 and is color image.I like to embed the RGB secret image into RGB cover image.So I tried to embed the R plane's pixel of the secret image into the R plane of the cover image and similarly for B and G plane pixels.Te extracted data is stored in finaltxt and given to variable RecIm.
Then shouldn't you be using em = im2(:,:,dim) ?
Note: the Answer I gave above about embedding any kind of file has to do with embedding files, not images. Images are usually encoded into image files for storage on disk -- files that have header information about the image and which might store the image in compressed form. Make sure you know what it is that you are embedding, an image or an image file.
Sir I am trying to embed an image not an image file.I used em=im2(:,:,dim) still the extraction is not working.
I am going to sleep now. It might be a number of hours before I look at this.
Sorry but it is 3am here and I am expecting people in the morning.
Ok Sir.Thank you Sir for your help.Hope you would help me latter.
Have you solved the problem of extracting pictures? I also want to know what to do
Have you solved the problem of extracting images?
Please, I am facing the same problem

Sign in to comment.

Asked:

on 4 Oct 2015

Edited:

on 31 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!