Skip to content

Commit a3a87f2

Browse files
authored
Merge pull request #24 from navkiran/master
Added Image Processing Algorithms for Matlab
2 parents 8843c75 + a468e72 commit a3a87f2

File tree

11 files changed

+165
-0
lines changed

11 files changed

+165
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
clc;
2+
clear variables;
3+
len=str2double(input('Enter the length (character count) of the message you are looking for: ','s'));
4+
len = len*8;
5+
image_hide = imread('output.png');
6+
[rows,columns,channels] = size(image_hide);
7+
if channels > 1
8+
image_hide = rgb2gray(image_hide);
9+
end
10+
count=1;
11+
bitseq='';
12+
for i=1:rows
13+
for j=1:columns
14+
%For all the characters in the message
15+
if count<=len
16+
17+
%Retrieve the LSB of the intensity level of the pixel
18+
LSB=mod(image_hide(i,j),2);
19+
bitseq(count,1) = LSB;
20+
count=count+1;
21+
end
22+
end
23+
end
24+
25+
%Converting the bit sequence into the original message
26+
weights = [ 128 64 32 16 8 4 2 1 ];
27+
28+
% elements are taken column wise from bitseq, each char has 8 bits so 8
29+
% rows
30+
message_matrix = reshape(bitseq,8,len/8);
31+
original_message = char(weights*message_matrix);
32+
disp(['The original message is: ',original_message]);
Loading
Loading
Loading
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Image Steganography means hiding data in an image.
2+
3+
This is possible because images are constituted of pixels. Each pixel can be represented as bits. The lower bits don't hold a lot of detail so they can be used to hide our data (by replacing them with corresponding bits of data).
4+
5+
Since most of the information is contained in MSBs (due to their higher weights; bits are multiplied by 2^n for the nth bit and added during binary to decimal conversion), the resulting image is very similar to the human eye if we modify the LSBs only (Least Significant Bits).
6+
7+
The encoding is done using the following steps:
8+
9+
1. Convert the image to greyscale
10+
2. Resize the image if needed
11+
3. Convert the message to its binary format
12+
4. Initialize output image same as input image
13+
5. Traverse through each pixel of the image and do the following:
14+
- Convert the pixel value to binary
15+
- Get the next bit of the message to be embedded
16+
- Create a variable temp
17+
If the message bit and the LSB of the pixel are same, set temp = 0
18+
If the message bit and the LSB of the pixel are different, set temp = 1
19+
This setting of temp can be done by taking XOR of message bit and the LSB of the pixel
20+
Update the pixel of output image to input image pixel value + temp
21+
22+
Keep updating the output image till all the bits in the message are embedded
23+
Finally, write the input as well as the output image to local system.
24+
25+
The decoding/decryption is done using the following steps:
26+
27+
1. Get the output image which was encoded earlier.
28+
2. Input the length of the encoded message (character count).
29+
3. Retrieve the LSBs of each pixel
30+
4. Form a bit sequence from these LSBs
31+
5. Arrange the bit sequence into a matrix of 8 rows and total_message_bits/8 columns
32+
(each column will represent a character of 8 bits, hence 8 rows)
33+
- Convert the binary value to decimal
34+
- Get the corresponding char from ascii
35+
36+
37+
Finally, display the original message.
38+
39+
References:
40+
[Concept, TDS article](https://towardsdatascience.com/steganography-hiding-an-image-inside-another-77ca66b2acb1),
41+
[GFG article on LSB based Image Steganography](https://www.geeksforgeeks.org/lsb-based-image-steganography-using-matlab/),
42+
[GFG article on text extraction from image](https://www.geeksforgeeks.org/text-extraction-from-image-using-lsb-based-steganography/)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
clc;
2+
clear variables;
3+
% Load the image and convert to grayscale if it is RGB
4+
I=imread('input.png');
5+
[rows, columns, numberOfColorChannels] = size(I);
6+
if numberOfColorChannels > 1
7+
I = rgb2gray(I);
8+
end
9+
L=256;
10+
image_hide=I;
11+
12+
message=input('Please enter the message you want to hide: ','s');
13+
% Each character occupies a byte, so total bits can be found by multiplying
14+
% string length by 8
15+
len=strlength(message)*8;
16+
ascii_values=uint8(message);
17+
ascii2binary=dec2bin(ascii_values,8);
18+
19+
% Append all binary equivalents of ascii values into one string
20+
binary_sequence='';
21+
for i=1:strlength(message)
22+
binary_sequence=append(binary_sequence,ascii2binary(i,:));
23+
end
24+
25+
% To track how many bits of message have been hidden
26+
bitCount=1;
27+
28+
for i=1:rows
29+
for j=1:columns
30+
31+
if bitCount<=len
32+
%Obtain the LSB of the grey level of the pixel
33+
LSB=mod(I(i,j),2);
34+
35+
%Convert the bit from the message to numeric form
36+
a=str2double(binary_sequence(bitCount));
37+
38+
%Perform XOR operation between the bit and the LSB
39+
temp=double(xor(LSB,a));
40+
41+
%Change the bit of the image_hide accordingly
42+
image_hide(i,j)=I(i,j)+temp;
43+
44+
bitCount=bitCount+1;
45+
end
46+
end
47+
end
48+
49+
subplot(1,2,1);
50+
imshow(I);
51+
title('Input Image');
52+
53+
subplot(1,2,2);
54+
imshow(image_hide);
55+
title('Image with Hidden Data');
56+
57+
imwrite(image_hide,'output.png')
58+
Binary file not shown.
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Used for resizing images. It assigns to each location the intensity of its nearest neighbor in original image. Refer [Gonzalez DIP Book](https://www.pearson.com/us/higher-education/program/Gonzalez-Digital-Image-Processing-4th-Edition/PGM241219.html) chapter 1.
2+
3+
Basic algorithm is as follows:
4+
5+
*For every pixel in resized image, we find the coordinate mapping in original image and copy over the pixel intensity from there to pixel in output image.*
6+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
clc;
2+
clear variables;
3+
image=imread('lena_color.bmp');
4+
[r,c,d] = size(image);
5+
zoom = 1.5;
6+
zr=zoom*r;
7+
zc=zoom*c;
8+
9+
for i=1:1:zr
10+
for j=1:1:zc
11+
x=i/zoom;
12+
mapi=round(x);
13+
y=j/zoom;
14+
mapj=round(y);
15+
if mapi==0
16+
mapi=1;
17+
end
18+
if mapj==0
19+
mapj=1;
20+
end
21+
res(i,j)=image(mapi,mapj);
22+
end
23+
end
24+
figure
25+
imshow(image);
26+
figure
27+
imshow(res);

0 commit comments

Comments
 (0)