LSB based Image steganography using MATLAB
Prerequisite: Image Steganography Introduction, Implementation using Python.
Steganography is the method of hiding secret data inside any form of digital media. The main idea behind steganography is to hide the existence of data in any medium like audio, video, image, etc. When we talk about image steganography, the idea is quite simple. Images are made up of pixels which usually refer to the color of that particular pixel. In a grayscale (black and white) image, these pixel values range from 0-255, 0 being black and 255 being white.
Concept of LSB based data embedding:
LSB stands for Least Significant Bit. The idea behind LSB embedding is that if we change the last bit value of a pixel, there won’t be much visible change in the color. For example, 0 is black. Changing the value to 1 won’t make much of a difference since it is still black, just a lighter shade.
The encoding is done using the following steps:
- Convert the image to grayscale
- Resize the image if needed
- Convert the message to its binary format
- Initialize output image same as input image
- Traverse through each pixel of the image and do the following:
- Convert the pixel value to binary
- Get the next bit of the message to be embedded
- Create a variable temp
- If the message bit and the LSB of the pixel are same, set temp = 0
- If the message bit and the LSB of the pixel are different, set temp = 1
- This setting of temp can be done by taking XOR of message bit and the LSB of the pixel
- Update the pixel of output image to input image pixel value + temp
- Keep updating the output image till all the bits in the message are embedded
- Finally, write the input as well as the output image to local system.
Example:
Input : message=’geeksforgeeks’
Output : Image with the given message embedded:
Below is the implementation in MATLAB:
matlab
clear all;
clc;
input = imread( 'peppers.png' );
input=rgb2gray(input);
input=imresize(input, [512 512]);
message= 'geeksforgeeks' ;
len = length(message) * 8;
ascii_value = uint8(message);
bin_message = transpose(dec2bin(ascii_value, 8));
bin_message = bin_message(:);
N = length(bin_message);
bin_num_message=str2num(bin_message);
output = input;
height = size(input, 1);
width = size(input, 2);
embed_counter = 1;
for i = 1 : height
for j = 1 : width
if (embed_counter <= len)
LSB = mod(double(input(i, j)), 2);
temp = double(xor(LSB, bin_num_message(embed_counter)));
output(i, j) = input(i, j)+temp;
embed_counter = embed_counter+1;
end
end
end
imwrite(input, 'path_to_folder\originalImage.png' );
imwrite(output, 'path_to_folder\stegoImage.png' );
|
Comparison of images:
As we can see in the above screenshot, the input and the output image look exactly the same to the human eye. The output image has the message embedded in it.
Advantages of this method:
- This method is very fast and easy to implement in comparison to other methods of image Steganography.
- The output image has very slight difference to the input image.
- Instead of embedding the message in only the LSB, we can embed the message in last two LSBs, thus embedding even large messages.
- This method forms the basics of many other complex algorithms
- Instead of embedding the message in only the LSB, we can embed the message in last two LSBs, thus embedding even large messages.
Disadvantages of this method:
- This type of encoding the data is weak since it can be easily decoded by taking the LSBs of the image and getting the message in binary format.
- This is method is too old because it was used long ago when other encoding methods were not yet developed.
- When embedding the message in more than one LSB, the image quality may reduce depending on how many pixels are changed.
Last Updated :
12 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...