The message embedded in the least significant bit plane of the gray image encoded using Hamming code 7.4

5 vues (au cours des 30 derniers jours)
Hi guys I work on LSB stenography using hamming code.
The whole code should work so that the image changes to gray, the message is encoded with a Hamming code and inserted into the LSB bit plane, then the whole image is assembled and extracted and decoded from the encoded message from the image.
I dont know solve this error on 52 line if I move everything on 52 line up then again I get an error with bin_num_message because the program does not recognize such a variable. I will be very happy for any help or advice.
% Clear the existing workspace
clear all;
% Clear the command window
clc;
% Read the input image
input = imread('lena.bmp');
% Convert image to grayscale
input=rgb2gray(input);
% Resize the image to required size
input=imresize(input, [512 512]);
% Message to be embedded
message = 'A';
% Length of the message where each character is 8 bits
len = length(message) * 8;
% Get all the ASCII values of the characters of the message
ascii_value = uint8(message);
% Convert the decimal values to binary
bin_message = transpose(dec2bin(ascii_value, 8));
% Get all the binary digits in separate row
bin_message = bin_message(:);
% Length of the binary message
N = length(bin_message);
% Converting the char array to numeric array
bin_num_message=str2num(bin_message);
% Hamming (7,4) encoding function
function [encoded_msg]=Hamming74Encode(input_msg)
data = reshape(input_msg,4,[]).';
G = [1 0 1 0 1 1 0;0 1 1 0 0 1 1;1 1 0 1 0 0 1];
encoded_msg = mod(data*G,2);
encoded_msg = encoded_msg(:).';
end
% Hamming (7,4) decoding function
function [decoded_msg]=Hamming74Decode(input_msg)
H = [1 0 1 0 1 1 0;0 1 1 0 0 1 1;1 1 0 1 0 0 1];
decoded_msg = mod(input_msg*H',2);
end
% Encode the binary message using Hamming (7,4) encoding
encoded_bin_message = Hamming74Encode(bin_num_message);
% Initialize output as input
output = input;
% Get height and width for traversing through the image
height = size(input, 1);
width = size(input, 2);
% Counter for number of embedded bits
embed_counter = 1;
% Traverse through the image
for i = 1 : height
for j = 1 : width
% If more bits are remaining to embed
if(embed_counter <= length(encoded_bin_message))
% Finding the Least Significant Bit of the current pixel
LSB = mod(double(input(i, j)), 2);
% Find whether the bit is same or needs to change
temp = double(xor(LSB, encoded_bin_message(embed_counter)));
% Updating the output to input + temp
output(i, j) = input(i, j)+temp;
% Increment the embed counter
embed_counter = embed_counter + 1;
end
end
end
% Reading the data back from image
embedded_data = [];
for i = 1 : height
for j = 1 : width
if (embed_counter <= length(encoded_bin_message))
% Finding the Least Significant Bit of the current pixel
LSB = mod(double(output(i, j)), 2);
% Find whether the bit is same or needs to change
temp = double(xor(LSB, encoded_bin_message(embed_counter)));
% Updating the output to input + temp
output(i, j) = input(i, j) + temp;
% Increment the embed counter
embed_counter = embed_counter + 1;
end
end
end
% Reading the data back from image
embedded_data = [];
for i = 1 : height
for j = 1 : width
% Finding the Least Significant Bit of the current pixel
LSB = mod(double(output(i, j)), 2);
embedded_data = [embedded_data LSB];
end
end
% Extracting the encoded message from embedded data
encoded_data = embedded_data(1:N*7);
% Decode the encoded binary message using Hamming (7,4) decoding
decoded_bin_message = Hamming74Decode(reshape(encoded_data, 7, [])');
% Get the indices where error occurs
error_index = find(decoded_bin_message >= 4);
% If no error is there in the decoded message
if isempty(error_index)
decoded_bin_message = decoded_bin_message(:).';
else
% Correcting the error in the decoded message
for i = 1 : length(error_index)
decoded_bin_message(error_index(i)) = bitxor(decoded_bin_message(error_index(i)), 7);
end
decoded_bin_message = decoded_bin_message(:).';
end
% Convert the decoded binary message to ASCII values
decoded_num_message = bin2dec(reshape(decoded_bin_message, 8, [])');
% Convert the decimal values to characters
decoded_message = char(decoded_num_message);
% Display the decoded message
disp(decoded_message);
% Write the output image
imwrite(output, 'output.bmp');
% Display the original image
figure;
imshow(input);
title('Original Image');
% Display the output image
figure;
imshow(output);
title('Steganographed Image');

Réponses (1)

Yash
Yash le 2 Mar 2023
As the error reported by the compiler, I recommend you to move the functions Hamming74Encode and Hamming74Decode at the end of the Matlab Script File. Also instead of using str2num, I recommend you to use str2double as str2num may give some unexpected results. For further details please refer here.
I hope this helps :)

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by