I am getting an error while embedding text file inside the image that is Inputs must be numeric. Error in gliomatextfile (line 17) modifiedImage = bitset(image, bitPlane, bi

2 views (last 30 days)

% Read the image
image = imread('image.jpg');

% Convert the image to uint8
image = im2uint8(image);

% Read the text file
fid = fopen('textfile.txt', 'r');
textData = fread(fid, '*char');
fclose(fid);

% Convert the text data to binary
binaryData = dec2bin(textData, 8)'; % Each character represented as an 8-bit binary string

% Reshape the binary data to a column vector
binaryData = binaryData(:);

% Embed the binary data into the image using LSB steganography
bitPlane = 1; % Choose the bit plane to modify (1-8, typically use 1 or 8 for LSB)
modifiedImage = bitset(image, bitPlane, binaryData);

% Display the modified image
imshow(modifiedImage);
title('Modified Image with Embedded Text');

% Save the modified image
imwrite(modifiedImage, 'image_with_text.jpg');

Accepted Answer

Mihir
Mihir on 28 Jun 2023
The error you're encountering suggests that the variable binaryData is not numeric, which is required by the bitset function. The issue arises from the fact that binaryData is a character array obtained from the text file, and you're attempting to pass it directly to bitset, which expects a numeric input.
To resolve this issue, you need to convert the binary string data in binaryData to numeric values before using it with bitset.
The below code should work fine:
% Read the image
image = imread('image.jpg');
Error using imread>get_full_filename
File "image.jpg" does not exist.

Error in imread (line 372)
fullname = get_full_filename(filename);
% Convert the image to uint8
image = im2uint8(image);
% Read the text file
fid = fopen('textfile.txt', 'r');
textData = fread(fid, '*char');
fclose(fid);
% Convert the text data to binary
binaryData = dec2bin(textData, 8); % Each character represented as an 8-bit binary string
% Reshape the binary data to a column vector
binaryData = binaryData(:);
% Convert binary string to numeric values
numericData = double(binaryData) - '0';
% Embed the numeric data into the image using LSB steganography
bitPlane = 1; % Choose the bit plane to modify (1-8, typically use 1 or 8 for LSB)
modifiedImage = bitset(image, bitPlane, numericData);
% Display the modified image
imshow(modifiedImage);
title('Modified Image with Embedded Text');
% Save the modified image
imwrite(modifiedImage, 'image_with_text.jpg');
In this updated code, double(binaryData) - '0' converts each character in binaryData to its corresponding numeric value (0 or 1) by subtracting the character '0'. The resulting numericData is then used with bitset to embed the text data into the image.
Make sure that the textfile.txt contains only ASCII characters, as this code assumes an 8-bit binary representation per character.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!