Name: Maged Sarhan Ahmed
Department: Computer and Control
University: IBB
Course: Digital Image Processing
Instructor: Eng.Mariam
Title: Hiding and Extracting Text in Images Using MATLAB
Abstract
Steganography is a technique for concealing sensitive information within another medium, such as an image,
audio file, or video. This research explores the implementation of steganography in MATLAB to embed text
into an image using the Least Significant Bit (LSB) method and extract it later. Two functions were
developed: one for embedding text into an image and another for extracting the hidden text. The results
demonstrate that the modified image retains its visual integrity while successfully concealing the text. This
technique has potential applications in secure communication, copyright protection, and data encryption.
1. Introduction
In today's digital age, securing sensitive information during transmission is of paramount importance.
Steganography, the art of hiding information within another medium, provides a powerful solution for covert
communication. Unlike cryptography, which encrypts data to make it unreadable, steganography hides the
existence of the data itself, making it less detectable.
This research focuses on image steganography , specifically embedding and extracting text from an image
using the Least Significant Bit (LSB) method. The LSB method modifies the least significant bit of each
pixel in the image to store binary data representing the hidden text. Since the LSB contributes minimally to
the overall pixel value, the changes are imperceptible to the human eye.
The objectives of this research are:
1. To implement a MATLAB-based solution for embedding text into an image.
2. To extract the hidden text from the modified image.
3. To evaluate the effectiveness of the LSB method in terms of visual integrity and data concealment.
2. Literature Review
Steganography has been widely studied in the field of digital image processing. Researchers have explored
various techniques for embedding data into images, including:
• LSB Method : Modifies the least significant bits of pixel values to store data. It is simple, efficient,
and widely used due to its minimal impact on image quality.
• Transform Domain Techniques : Embed data in frequency domains, such as Discrete Cosine
Transform (DCT) or Wavelet Transform. These methods are more robust but computationally
intensive.
• Encryption-Based Steganography : Combines steganography with encryption to enhance security.
While the LSB method is straightforward, it is vulnerable to statistical attacks. Advanced techniques, such as
randomizing the embedding locations or combining LSB with encryption, can mitigate these vulnerabilities.
3. Methodology
3.1 Embedding Text in an Image
To embed text in an image:
1. Convert the text into binary format.
2. Modify the least significant bits (LSBs) of the image's pixel values to store the binary representation
of the text.
3. Add a termination marker (8 zeros) to indicate the end of the text.
4. Save the modified image.
3.2 Extracting Text from an Image
To extract text from an image:
1. Read the modified image.
2. Extract the LSBs of the pixel values to reconstruct the binary representation of the text.
3. Detect the termination marker (8 zeros) to determine the end of the text.
4. Convert the binary data back into text.
4. Implementation
4.1 Function to Embed Text in an Image
function stegoImage = embedTextInImage(imagePath, text, outputImagePath)
% This function hides text in an image using LSB steganography.
% Step 1: Read the image
originalImage = imread(imagePath);
[rows, cols, channels] = size(originalImage);
% Step 2: Convert text to binary
binaryText = dec2bin(text, 8); % Convert each character to 8-bit binary
binaryText = reshape(binaryText', 1, []); % Flatten into a single row
binaryText = binaryText - '0'; % Convert characters ('0'/'1') to numeric (0/1)
terminationMarker = zeros(1, 8); % 8 zeros to mark the end
binaryText = [binaryText, terminationMarker];
% Step 3: Check if the image can hold the text
numBitsRequired = length(binaryText);
numPixelsAvailable = rows * cols * channels;
if numBitsRequired > numPixelsAvailable
error('The image is too small to hold the text.');
end
% Step 4: Embed the binary text into the image
index = 1;
for i = 1:rows
for j = 1:cols
for k = 1:channels
if index <= numBitsRequired
originalImage(i, j, k) = bitset(originalImage(i, j, k), 1,
binaryText(index));
index = index + 1;
else
break;
end
end
end
end
% Step 5: Save the modified image
imwrite(originalImage, outputImagePath, 'png');
disp('Text has been successfully embedded in the image.');
% Return the modified image
stegoImage = originalImage;
end
4.2 Function to Extract Text from an Image
function extractedText = extractTextFromImage(imagePath)
% This function extracts hidden text from an image using LSB steganography.
% Step 1: Read the stego image
stegoImage = imread(imagePath);
[rows, cols, channels] = size(stegoImage);
% Step 2: Initialize variables to collect LSBs
binaryData = [];
terminationMarker = zeros(1, 8); % Termination marker is 8 zeros
found = false;
% Step 3: Extract LSBs from each pixel
for i = 1:rows
for j = 1:cols
for k = 1:channels
% Get the LSB of the current pixel
lsb = bitget(stegoImage(i, j, k), 1);
binaryData = [binaryData, lsb];
% Check for termination marker every 8 bits
if mod(length(binaryData), 8) == 0
currentByte = binaryData(end-7:end);
if isequal(currentByte, terminationMarker)
found = true;
break;
end
end
end
if found, break; end
end
if found, break; end
end
% Remove the termination marker
if found
binaryData = binaryData(1:end-8);
else
error('Termination marker not found.');
end
% Step 4: Convert binary data to text
% Reshape binary data into 8-bit groups
numChars = length(binaryData) / 8;
binaryMatrix = reshape(binaryData, 8, numChars)';
% Convert each binary row to a character
extractedText = char(bin2dec(num2str(binaryMatrix)))';
disp('Text has been successfully extracted from the image.');
end
4.3 Testing the Functions
imagePath = 'peppers.png';
text = 'Hello, Maged!';
outputImagePath = 'D:\\stego_image.png';
% Embed text
stegoImage = embedTextInImage(imagePath, text, outputImagePath);
output:
% Extract text
extractedText = extractTextFromImage(outputImagePath);
% Display results
disp(['Extracted Text: ', extractedText]);
output:
5. Results
1. After embedding, the modified image (stego_image.png) appears visually identical to the original
image but contains the hidden text.
2. The extraction process successfully retrieves the hidden text: Hello, Maged!.
3. The LSB method ensures minimal distortion to the image, making it suitable for covert
communication.
6. Discussion
The LSB method is effective for embedding text into images due to its simplicity and minimal impact on
image quality. However, it has limitations:
• Vulnerability to Attacks : Statistical analysis can detect anomalies in the LSB distribution.
• Capacity Constraints : Large amounts of data cannot be embedded without degrading image quality.
Future work could focus on enhancing security by incorporating encryption or using advanced techniques
like transform-domain steganography.
7. Conclusion
This research demonstrated the implementation of steganography in MATLAB to hide and extract text from
an image using the LSB method. The results show that the modified image retains its visual integrity while
successfully concealing the text. This technique has potential applications in secure communication,
copyright protection, and data encryption. Future research could explore hybrid approaches combining
steganography with encryption to enhance security.
8. References
1. MATLAB Documentation: https://www.mathworks.com/help/matlab/
2. GPT-4, DeepSeek, and Qwen2.5-Max