Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Hide Image inside an Image

The tutorial on hiding the text messages inside the image should have given enough knowledge on bit replacing method. Also check Bit plane slicing to learn more about bit planes.
In this tutorial, we will start in the reverse order i.e. Let’s first perform decryption and then we will learn about encryption.


Download the encrypted image and run this decryption code below to see the hidden image inside the cover image.



Encrypt_Image1.png







MATLAB CODE:

%DECRYPT IMAGE
clear all

%READ THE ENCRYPTED IMAGE
A = imread('Encrypt_Image1.png');

%RED,GREEN AND BLUE COMPONENTS
Red_ch = A(:,:,1);
Green_ch = A(:,:,2);
Blue_ch = A(:,:,3);

%INTERVAL
Dt = bitget(Red_ch(1:8),1);
Dt = double(bi2de(Dt));

%NUMBER OF COLUMNS
Ncols = bitget(Red_ch(9:16),1);
Ncols = double(bi2de(Ncols));

%NUMBER OF ROWS
Nrows = bitget(Red_ch(17:24),1);
Nrows = double(bi2de(Nrows));

%FETCH THE LEAST SIGNIFICANT BIT FROM THE ENCRYPTED IMAGE
EndPoint = (Dt*Nrows*Ncols*8)+24;
Decrypt_Red = bitget(Red_ch(25:Dt:EndPoint),1);
Decrypt_Green = bitget(Green_ch(25:Dt:EndPoint),1);
Decrypt_Blue = bitget(Blue_ch(25:Dt:EndPoint),1);

Decrypt_Red = reshape(Decrypt_Red,8,[])';
Decrypt_Red = bi2de(Decrypt_Red);
Decrypt_Red = reshape(Decrypt_Red,Ncols,Nrows);

Decrypt_Green = reshape(Decrypt_Green,8,[])';
Decrypt_Green = bi2de(Decrypt_Green);
Decrypt_Green = reshape(Decrypt_Green,Ncols,Nrows);

Decrypt_Blue = reshape(Decrypt_Blue,8,[])';
Decrypt_Blue = bi2de(Decrypt_Blue);
Decrypt_Blue = reshape(Decrypt_Blue,Ncols,Nrows);

Decrypted_Image = zeros([Ncols,Nrows,3]);
Decrypted_Image(:,:,1)=Decrypt_Red;
Decrypted_Image(:,:,2)=Decrypt_Green;
Decrypted_Image(:,:,3)=Decrypt_Blue;

Decrypted_Image = uint8(Decrypted_Image);


figure,imshow(Decrypted_Image);title('Secret Image');



ENCRYPTION:

MATLAB CODE:

%ENCRYPTION
clear all

%READ THE COVER IMAGE
I = imread('peppers.png');

%READ THE IMAGE TO HIDE
J = imread('cube.jpg');

%PREALLOCATE THE OUTPUT IMAGE
Output = zeros(size(I));
%REPRESENT THE NUMBER OF ROWS AND COLUMNS OF THE SECRET IMAGE
%IN BINARY FORMAT
Jsize = de2bi([size(J,1),size(J,2)]);

%RED,GREEN AND BLUE COMPONENTS OF THE SECRET IMAGE
Red_Ch = J(:,:,1);
Green_Ch = J(:,:,2);
Blue_Ch = J(:,:,3);

%CONVERT THE RED COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
Encrypt_Red = de2bi(Red_Ch(:),8)';
Encrypt_Red = Encrypt_Red(:)';

%CONVERT THE GREEN COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
Encrypt_Green = de2bi(Green_Ch(:),8)';
Encrypt_Green = Encrypt_Green(:)';

%CONVERT THE BLUE COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
Encrypt_Blue = de2bi(Blue_Ch(:),8)';
Encrypt_Blue = Encrypt_Blue(:)';

%CALCULATE THE INTERVAL AND CONVERT IT INTO BINARY FORMAT
dt=floor((size(I,1)*size(I,2))/numel(Encrypt_Red));
Bi_dt = de2bi(dt,8);

Info_data = [Bi_dt,Jsize(1,:),Jsize(2,:)];

%RED,GREEN AND BLUE COMPONENT OF THE COVER IMAGE
Red_mat = I(:,:,1);
Green_mat = I(:,:,2);
Blue_mat = I(:,:,3);

% INTERVAL,NUMBER OF COLUMNS, NUMBER OF ROWS
Red_mat(1:24) = bitset(Red_mat(1:24),1,Info_data);
Green_mat(1:24) = bitset(Green_mat(1:24),1,Info_data);
Blue_mat(1:24) = bitset(Blue_mat(1:24),1,Info_data);

%%REPLACE THE LEAST SIGNIFICANT BIT OF THE COVER IMAGE
%WITH THE BINARY FORMAT OF THE SECRET IMAGE
EndValue = numel(Encrypt_Red)*dt+24;
Red_mat(25:dt:EndValue) = bitset(Red_mat(25:dt:EndValue),1,Encrypt_Red);
Green_mat(25:dt:EndValue) = bitset(Green_mat(25:dt:EndValue),1,Encrypt_Green);
Blue_mat(25:dt:EndValue) = bitset(Blue_mat(25:dt:EndValue),1,Encrypt_Blue);

%ENCRYPTED IMAGE
Output(:,:,1) = Red_mat;
Output(:,:,2) = Green_mat;
Output(:,:,3) = Blue_mat;
Output = uint8(Output);

figure,subplot(121),imshow(I); subplot(122),imshow(Output);
 
%DIFFERENCE BETWEEN THE ORIGINAL AND THE ENCRYPTED IMAGE
figure,imagesc(double(I)-double(Output));colormap(jet);colorbar;

%WRITE THE ENCRYPTED IMAGE IN THE PNG FORMAT
imwrite(Output,'Encrypt_Image1.png');




EXPLANATION:

Were you able to extract the encrypted image? The type of the cover image is RGB and the secret image is also RGB. The Red channel of the cover image is encrypted with the Red channel of the secret image, Green channel of cover image with the green channel of the secret image. Similarly the encryption is done on the blue channel.

NOTE: 
  • The size of the cover image after encryption should not be modified
  • Save the encrypted image with lossless compression method
  • Size of the secret image should be less than 10% of the cover image
like button Like "IMAGE PROCESSING" page

Hide the message or image inside an image

                                Steganography is a technique that hides secret information inside another document or image and it is extracted by different person.
In this post, we will see how to hide text message or image inside an image without tampering the original or cover image.  Learn about bit plane slicing to know more about the background,

EXAMPLE 1: Hide Secret Message inside an image




DECRYPTION:

It is straight-forward reverse process. The secret message is obtained by extracting the least significant bits from the encrypted matrix


MATLAB CODE:
%ENCRYPTION

clear all
%READ THE INPUT IMAGE
A = imread('cameraman.tif');

%
Output = A;

%MESSAGE TO BE HIDDEN INSIDE THE IMAGE
Str = 'Image Processing! Let us learn together. Visit angeljohnsy.blogspot.com';

%NUMBER OF CHARACTERS IN THE MESSAGE
numLetters = numel(Str);

%REPRESENT THE LENGTH OF THE MESSAGE IN BINARY FORMAT
Bi_Total = de2bi(numLetters,8);
Bi_Msg = de2bi(double(Str),8)';

Encrypt_msg = Bi_Msg(:)';

%FIND THE LENGTH OF THE MESSAGE IN THE BINARY FORMAT
num_En_msg = numel(Encrypt_msg);


%DEFINE THE INTERVAL BETWEEN EACH VALUE
dt=floor(numel(A)/num_En_msg);

%BINARY VALUE OF THE INTERVAL
Bi_dt = de2bi(dt,8);

% FIRST 8 BITS - INTERVAL
% SECOND 8 BITS - TOTAL LENGTH OF THE BINARY VALUES
Info_data = [Bi_dt,Bi_Total];
Output(1:16) = bitset(A(1:16),1,Info_data);

%REPLACE THE LEAST SIGNIFICANT BIT IN THE IMAGE
Start_pt =17;
End_pt = (num_En_msg*dt)+16;
Output(Start_pt:dt:End_pt) = bitset(A(Start_pt:dt:End_pt),1,Encrypt_msg);


figure,subplot(121),imshow(A);title('Original Image');
subplot(122),imshow(Output);title('Image with the hidden message');

%SHOW THE DIFFERENCE BETWEEN THE ORIGINAL AND THE ENCRYPTED IMAGE
figure,imagesc(double(A)-double(Output));colormap(jet);colorbar;

%WRITE THE ENCRYPTED IMAGE IN THE PNG FORMAT
imwrite(Output,'Encrypt1.png');

display('Message is hidden inside the image!');



EXPLANATION:
A message is hidden inside the image by converting the message into binary format and placing it in the Least Significant Bit of the image pixels. The encrypted image is saved as ‘PNG’ image file in order to have lossless compression. With the lossless compression technique, the pixel values will not be altered so the information embedded in the LSB will not be lost.
The maximum absolute difference in value between the original and the encrypted image is one. This is obvious due to the fact that we are dealing only with the LSB bit of the image.
We can place the bits of the secret message in a specific pattern or with regular intervals instead of replacing the pixels adjacent to each other. The image shown below indicates that the bits are placed with specific interval values.


The number of characters in the secret message should be less than 10% of the cover image. This is due to the fact that for each character we need 8 bits and to place these 8 bits we need 8 pixels.
MATLAB CODE:
%DECRYPT THE MESSAGE
clear all
clc
%READ THE ENCRYPTED IMAGE
A = imread('Encrypt1.png');

%FIRST 8 BITS REPRESENT THE INTERVAL
Dt = bitget(A(1:8),1);
Dt = double(bi2de(Dt));


%LENGTH OF THE TEXT MESSAGE
Len = bitget(A(9:16),1);
Len = double(bi2de(Len));

%FETCH THE LEAST SIGNIFICANT BIT FROM THE ENCRYPTD IMAGE
Start_pt = 17;
End_pt = (Len*Dt*8)+16;
Decrypt_msg = bitget(A(Start_pt:Dt:End_pt),1);

%CONVERT THE IMAGE INTO DECIMAL
Decrypt_msg = reshape(Decrypt_msg,8,[])';
Dec_Char   = bi2de(Decrypt_msg)';

display('Text Message inside the image:');
display(char(Dec_Char));


EXPLANATION:
Decryption is the reverse process. First the interval between the pixels is fetched, followed by the length of the message.  Based on the length of the message, the bits are extracted and the final message is obtained.




like button Like "IMAGE PROCESSING" page

Digital Image Water Marking – Part 1


Digital image watermarking is the method in which data is embedded in a multimedia file such as an image or a video, so as to verify the credibility of the content or the identity of the owner.

MATLAB CODE:
%Digital Image Watermarking

%Unmasked Image f
f = imread('angel.jpg');
figure,imshow(f);title('Unmasked Image');
 
Unmasked Image
%Watermark
w = imread('watermark_sym.jpg');
figure,imshow(w);title('Watermark');







%Visibility of the watermark
alpha=0.5;

Fw = (1-alpha)*f + alpha.*w;
figure,imshow(Fw);title('Watermarked Image-Visibilty:0.5');



Explanation:
1.      Read the image to be watermarked. In my case it ‘s ‘angel.jpg’
2.      Read the watermark. Make sure the image size and the watermark size are same.
3.      Adjust the alpha value based on your requirement. Alpha can be between 0 and 1. If alpha is less than 0.5 the watermark will be less visible and if the alpha is more than 0.5, the watermark will be clearly visible.



MATLAB CODE:

%Read the Blog logo
Logo = imread('Dwatermark2.jpg');
figure,imshow(Logo);title('Blog Logo');


OIm = f;
%Visibility control
alpha = 0.5;

[m, n,~]=size(Logo);

%choose a portion of the Image
Sz = [495 927];

%Apply watermark
OIm(Sz(1):Sz(1)+m-1,Sz(2):Sz(2)+n-1,:)=(1-alpha)*OIm(Sz(1):Sz(1)+m-1,Sz(2):Sz(2)+n-1,:) + alpha.*Logo;
figure,imshow(OIm);title('Watermark in the centre');



EXPLANATION:
1.      Read the blog logo.
2.      Note: The size of the input image and the logo are not same.
3.      Choose a portion in the input image and apply watermark.



LSB Digital Watermarking
In this method, the watermark is hidden in the input image.
Consider a pixel from the input image; say 249 and a pixel from the watermark say, 155.
Now hide the pixel value of the watermark in the two least significant bits of the input image.
1.      The binary representation of 249 is 11111001
2.      The binary representation of 155 is 10011011
3.      Make the two least significant bits of input image value 249 to zeros i.e. 11111000
4.      Find the two Most significant bits of the watermark value 155 i.e. 10
5.      Now place the two most significant bits of the watermark at the two least significant bits of the input image.
6.      11111000 + 00000010 = 11111010 i.e.  250


MATLAB CODE:
%LSB watermarked Image
Fw1 = 4*(f/4) + w/64;
figure,imshow(Fw1);title('LSB watermarked Image');




How to retrieve the watermark:
Method 1:
Subtract the input image (without watermark) from the watermarked Image.
%Retrieve the Watermark
DMark = (Fw1-f)*64;
figure,imshow(DMark);title('Watermark extracted from the image');



Method 2:
Retrieve the two least significant bits of the image
%Retrieve the Watermark
DMark2 = rem(Fw1,4)*64;
figure,imshow(DMark2);title('Watermark extracted from the image');

We can extend the above techniques for steganography or image coding. In the next upcoming post I will share about how to validate the digital watermark.





like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com