Resulting image keeps having high contrast
Show older comments
hello i wrote a simple code using a simple algorithm to understand steganography better , using LSB i hide the pixel values of one image into another, the key in this case represents the amount of steps (pixels) the loop will take to hide consecutive pixels from the secret cat image into the tree image.
I know it's not very secure but that's not the point.
My code to hide image:
% Function that accepts dir for secret image and dir for image to hide it in
% it writes an image that contains the secret image.
% it return an array containing the dimensions of the secret image
% because we need them in the revealing function
function [dim] = hideMyimg(secret,image,Key)
img = imread(image);
egg = imread(secret);
% Setting the least significant bits to zero
img = img - mod(img,4);
%checking if the secret fits into the hiding place
img = check(img,egg);
dim = [size(egg,1),size(egg,2)];
% using modulus to lower the amount of steps, to simplify code
key = mod(Key,1000);
%Seperating color channels
r1 = img(:,:,1);
g1 = img(:,:,2);
b1 = img(:,:,3);
r2 = egg(:,:,1);
g2 = egg(:,:,2);
b2 = egg(:,:,3);
%Driver code
r = hide(r1,r2,key);
g = hide(g1,g2,key);
b = hide(b1,b2,key);
%Combining the color channels
output(:,:,1) = r;
output(:,:,2) = g;
output(:,:,3) = b;
imwrite(output,'output.png')
end
%Function to hid matrix inside another
function [hidden] = hide(image,egg,key)
[row,col] = size(egg);
% turning the matrices into vectors to make it easy to loop over
imgvec = image(:);
% Move the most significant bits of the secret image to the least significant bits
tempi = egg./64;
eggvec = tempi(:);
%max value of pixels to hide
max = row*col;
% counter to make sure the loop stops once all the pixels are hidden
i = 1;
% temp value we need to re-loop over the matrix from a new starting point
temp = 2;
%counter
n = 1;
while(i<max)
imgvec(n) = imgvec(n) + eggvec(i);
n = n + key;
if n>max
n = temp;
temp = temp + 1;
end
i = i + 1;
end
hidden = reshape(imgvec,[size(image,1),size(image,2)]);
end
% Function to check if image 1 is bigger than image 2
% And if so resize image 1 to be bigger than image 2
function [image] = check(img1,img2)
row1 = size(img1,1);
col1 = size(img1,2);
row2 = size(img2,1);
col2 = size(img2,2);
if row2*col2>row1*col1
val1 = ceil(row2/row1);
val2 = ceil(col2/col1);
if val1 > val2
image = imresize(img1,val1);
else
image = imresize(img1,val2);
end
else
image = img1;
disp('yes')
end
end

Code to reveal the hidden image:
function [] = getMyimg(key,dim)
image = imread('output.png');
%seperating color channels
r = image(:,:,1);
g = image(:,:,2);
b = image(:,:,3);
row = dim(1);
col = dim(2);
egg(:,:,1) = getimg(r,key,row,col);
egg(:,:,2) = getimg(g,key,row,col);
egg(:,:,3) = getimg(b,key,row,col);
% move least significant bits to the most significant
egg = egg * 64;
imwrite(egg,'secret.png');
end
function [egg] = getimg(image,key,row,col)
imgvec = image(:);
%initializing vector with the right dimensions for the secret image
eggvec = zeros(row*col,1);
max = row*col;
i = 1;
n = 1;
temp = 2;
while i < max
%get the least significant bits
eggvec(i) = mod(imgvec(n),4);
n = n + key;
if n>max
n = temp;
temp = temp + 1;
end
i = i + 1;
end
egg = reshape(eggvec,[row,col]);
end
The resulting image:

why is it coming out like this?
Answers (0)
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!