I want hide a text in image using LSB algorithm
for example:
X = image;
S = secret message;
I write:
X2(:)=bitset(X(:),1,S(:));
but when run i have an error
"??? Error using ==> bitset
Inputs must be non-negative integers."
after convert to binary i have another error
??? Undefined function or method 'bitset' for input arguments of type 'char'.
please help me.

1 件のコメント

Michael Chan
Michael Chan 2012 年 4 月 21 日
Similar methods are used in Matlab in the following example for static images:
http://www.mathworks.com/matlabcentral/fileexchange/36288-embedding-hiding-image-within-image-with-n-bit-lsb-steganography
http://www.mathworks.com/matlabcentral/fileexchange/36275-image-description-notes-with-n-bit-lsb-steganography

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2011 年 11 月 27 日

2 投票

Well here's my demo. It lets you hide an watermark image, which may be bigger or smaller than a cover image, into a user-specified bitplane of the cover (original) image. If the watermark is bigger than the original image, it scales it down. If the watermark is smaller, it tiles it. The watermark image can be grayscale and you can specify where you want it thresholded at. It uses standard MATLAB demo images. If the images are color, it takes the red channel - I haven't made it to work with color images. At the end it adds noise and recovers the watermark from the noise-free and noisy images, so you can see how lousy this method is as far as being robust to attack.
% Demo to watermark an image by hiding another image in a certain bit
% plane. Sometimes called "LSB Watermarking" or something similar.
% User is asked which bit plane they want to hide the image in.
% By Image Analyst.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 12;
% Read in the image what will have another image hidden into it.
baseFileName='moon.tif';
% baseFileName='cameraman.tif';
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
originalImage = imread(fullFileName);
% Get the number of rows and columns in the original image.
[visibleRows visibleColumns numberOfColorChannels] = size(originalImage);
if numberOfColorChannels > 1
% If it's color, extract the red channel.
originalImage = originalImage(:,:,1);
end
% Display the original gray scale image.
subplot(3, 3, 4);
imshow(originalImage, []);
title('Original Grayscale Starting Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% read the message image you want to hide in the cover image
baseFileName='cameraman.tif';
% baseFileName='moon.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
hiddenImage = imread(fullFileName);
% Get the number of rows and columns in the hidden image.
[hiddenRows hiddenColumns numberOfColorChannels] = size(hiddenImage);
if numberOfColorChannels > 1
% If it's color, extract the red channel.
hiddenImage = hiddenImage(:,:,1);
end
% Display the image.
subplot(3, 3, 1);
imshow(hiddenImage, []);
title('Image to be Hidden', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(hiddenImage);
subplot(3, 3, 2);
bar(pixelCount);
title('Histogram of image to be hidden', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
thresholdValue = 70;
binaryImage = hiddenImage < thresholdValue;
% Display the image.
subplot(3, 3, 3);
imshow(binaryImage, []);
caption = sprintf('Hidden Image Thresholded at %d', thresholdValue);
title(caption, 'FontSize', fontSize);
% Get the bit plane to hide the image in.
prompt = 'Enter the bit plane you want to hide the image in (1 - 8) ';
dialogTitle = 'Enter Bit Plane to Replace';
numberOfLines = 1;
defaultResponse = {'1'};
bitToSet = str2double(cell2mat(inputdlg(prompt, dialogTitle, numberOfLines, defaultResponse)));
% If image to be hidden is bigger than the original image, scale it down.
if hiddenRows > visibleRows || hiddenColumns > visibleColumns
amountToShrink = min([visibleRows / hiddenRows, visibleColumns / hiddenColumns]);
binaryImage = imresize(binaryImage, amountToShrink);
% Need to update the number of rows and columns.
[hiddenRows hiddenColumns] = size(binaryImage);
end
% Tile the hiddenImage, if it's smaller, so that it will cover the original image.
if hiddenRows < visibleRows || hiddenColumns < visibleColumns
watermark = zeros(size(originalImage), 'uint8');
for column = 1:visibleColumns
for row = 1:visibleRows
watermark(row, column) = binaryImage(mod(row,hiddenRows)+1, mod(column,hiddenColumns)+1);
end
end
% Crop it to the same size as the original image.
watermark = watermark(1:visibleRows, 1:visibleColumns);
else
% Watermark is the same size as the original image.
watermark = binaryImage;
end
% Display the thresholded binary image - the watermark alone.
subplot(3, 3, 5);
imshow(watermark, []);
caption = sprintf('Hidden Image\nto be Inserted into Bit Plane %d', bitToSet);
title(caption, 'FontSize', fontSize);
% Set the bit of originalImage(a copy, actually) to the value of the watermark.
watermarkedImage=originalImage;
for column = 1:visibleColumns
for row = 1:visibleRows
watermarkedImage(row, column)=bitset(originalImage(row, column), bitToSet, watermark(row, column));
end
end
% Display the image.
subplot(3, 3, 6);
imshow(watermarkedImage, []);
caption = sprintf('Final Watermarked Image\nwithout added Noise');
title(caption, 'FontSize', fontSize);
% add noise to watermarked image
noisyWatermarkedImage = imnoise(watermarkedImage,'gaussian');
% Display the image.
subplot(3, 3, 7);
imshow(noisyWatermarkedImage, []);
caption = sprintf('Watermarked Image\nwith added Noise');
title(caption, 'FontSize', fontSize);
%====================================================================================
% Now let's pretend we are starting with the watermarked noisy corrupted image.
% We want to recover the watermark.
% Use the known bitplane of watermarked image to recover the watermark.
recoveredWatermark = zeros(size(noisyWatermarkedImage));
recoveredNoisyWatermark = zeros(size(noisyWatermarkedImage));
for column = 1:visibleColumns
for row = 1:visibleRows
recoveredWatermark(row, column) = bitget(watermarkedImage(row, column), bitToSet);
recoveredNoisyWatermark(row, column) = bitget(noisyWatermarkedImage(row, column), bitToSet);
end
end
% Scale the recovered watermark to 0=255
recoveredWatermark = uint8(255 * recoveredWatermark);
recoveredNoisyWatermark = uint8(255 * recoveredNoisyWatermark);
% Display the images.
subplot(3, 3, 8);
imshow(recoveredWatermark, []);
caption = sprintf('Watermark Recovered\nfrom Bit Plane %d of\nNoise-Free Watermarked Image', bitToSet);
title(caption, 'FontSize', fontSize);
% Display the images.
subplot(3, 3, 9);
imshow(recoveredNoisyWatermark, []);
caption = sprintf('Watermark Recovered\nfrom Bit Plane %d of\nNoisy Watermarked Image', bitToSet);
title(caption, 'FontSize', fontSize);
msgbox('Done with demo!');

16 件のコメント

Aseel H
Aseel H 2011 年 11 月 27 日
thank you very much for demo
but I need to hide text in image by simple code
such as:
text = 'abcdef';
imge = imread('x.jpg');
binary = str2bin(text);
after that I want hide (binary) in (imge) using LSB algorithm
please help me
thanks
Image Analyst
Image Analyst 2011 年 11 月 27 日
No problem. Perhaps it will help someone who wants to hide an image in another image. Use Chandra's code below if you want to hide text in an image.
rasma khan
rasma khan 2014 年 3 月 8 日
Thank u so much for the help!
DHARMRAJ PRASAD
DHARMRAJ PRASAD 2015 年 2 月 26 日
What is Chandra's code??
budi agung
budi agung 2017 年 4 月 18 日
is there a way to make the watermark still in grayscale after extract?
Walter Roberson
Walter Roberson 2017 年 4 月 18 日
The demo creates a grayscale watermark.
Image Analyst
Image Analyst 2017 年 4 月 18 日
The demo binarizes an watermark image and puts it into a single bit plane of a gray scale cover image. You could adapt it to hide a gray scale watermark image by extracting each bitplane of the gray scale watermark image (so now you have 8 binary bit plane images for an 8 bit gray scale image) and hide them all in a single bitplane of the cover image. Of course this would take up 8 times as much real estate in the cover image as hiding a single bitplane. My demo does not do that but you're welcome to adapt it.
gina lion
gina lion 2020 年 3 月 4 日
exuse me can i contact you on e-mail
?
Image Analyst
Image Analyst 2020 年 3 月 4 日
Sorry, no.
Gabriela del Pilar Rojas Rubiano
Gabriela del Pilar Rojas Rubiano 2021 年 6 月 6 日
Hi, good night
what is the reason of put in the demo the function of noise. What happen if i dont use it? it would change in something? And what does it mean the threshold at 70?
thanks
Walter Roberson
Walter Roberson 2021 年 6 月 6 日
A standard question with watermarking is how well the watermark survives if the user changes the image. For example if the user deliberately zeros out the least significant bit of the green color pane, then will the program pull back complete garbage, or will the program pull back an incomplete watermark? If the user saves the image as JPEG and retrieves it, then how well the watermark survive?
Because that is such an important question for watermarks (and even more important for steganography), it make sense to demonstrate what it would look like to recover the watermark from an image that has had noise added.
Gabriela del Pilar Rojas Rubiano
Gabriela del Pilar Rojas Rubiano 2021 年 6 月 6 日
Thank you so much. You are the best.
And you know what is the reason of use the threshold at 70?
Walter Roberson
Walter Roberson 2021 年 6 月 6 日
I do not know why the threshold of 70 was chosen, or why it was chosen to take image < threshold, which effectively inverts the foreground and background.
I do notice that graythresh() suggests that the median energy is about 88 -- so the image is a bit darker than would be expected by random chance (which would average 127.5). That gives us some justification that any threshold should be a bit lower than halfway.
It would not astonish me if Image Analyst had just picked a value that "looked good".
Gabriela del Pilar Rojas Rubiano
Gabriela del Pilar Rojas Rubiano 2021 年 6 月 6 日
I got it, thank you so much. it was very helpful :D
tuan trinh
tuan trinh 2021 年 12 月 11 日
i want to change images but i don't know how to use it
Image Analyst
Image Analyst 2021 年 12 月 11 日
@tuan trinh to change images, look to where I specify baseFileName and folder and change them to be appropriate for your particular images on your computer.

サインインしてコメントする。

その他の回答 (5 件)

Walter Roberson
Walter Roberson 2011 年 11 月 26 日

0 投票

What data types are X and S? That is, what does class() report for each of them?
Your S must be a vector of values, each 0 or 1. It cannot be '0' or '1', and it cannot be (e.g) 72 (which corresponds to the coding for 'H')

1 件のコメント

Aseel H
Aseel H 2011 年 11 月 27 日
X is a variable to identify the image
S is a variable to identify the text that i want hide it in image
please if you have code hide text in image using LSB algorithm send me
thanks

サインインしてコメントする。

pratiksha patil
pratiksha patil 2012 年 3 月 4 日

0 投票

But how can I again extract the original message file???????

4 件のコメント

Aseel H
Aseel H 2012 年 3 月 7 日
編集済み: Walter Roberson 2020 年 5 月 3 日
cover = imread('watermarked.bmp');
cl = 10; % length of message character 'abcdefghij'
idk = 1;
for row = 1 : size(cover,1)
for col = 1 : size(cover,2)
if idk <= cl * 8
bin_vector(idk) = bitget(cover(row,col), 7);
idk = idk + 1;
end
end
end
for x = 1 : cl
bin(x,:) = bin_vector((x-1)*8 + 1 : x*8);
dec(x) = bin2dec(num2str(bin(x,:)));
end
message = char(dec);
Michael Chan
Michael Chan 2012 年 4 月 21 日
Similar methods are used in Matlab in the following example for static images:
http://www.mathworks.com/matlabcentral/fileexchange/36288-embedding-hiding-image-within-image-with-n-bit-lsb-steganography
http://www.mathworks.com/matlabcentral/fileexchange/36275-image-description-notes-with-n-bit-lsb-steganography
Sanjeeb Behera
Sanjeeb Behera 2016 年 9 月 10 日
When I run this code it gives an error in below line bin_vector(idk) = bitget(cover(row,col), 7);
Error - The variable bin_vector appears to change size on every loop iteration (within script). Consider perallocating for speed
Walter Roberson
Walter Roberson 2016 年 9 月 10 日
That would not be an error, it would be a warning given by the editor. It is a matter of efficiency, not a matter of being incorrect MATLAB. If you want to avoid the warning you can use
bin = zeros(cl, 8);
before the "for x = 1 : cl" loop.

サインインしてコメントする。

sha
sha 2012 年 3 月 21 日

0 投票

This is my example of extraction..
hopefully you can try..
%determined the size of watermarked image
mw=size(image1,1); %heigth of the watermarked image
nw=size(image1,2); %width of the watermarked image
for ii = 1: mw
for jj = 1 : nw
watermark (ii,jj) = bitget(image1(ii,jj),1);
end
end
figure(2)
imshow(watermark);
title('extracted image'); % watermark image for watermarking

1 件のコメント

ghassan78
ghassan78 2014 年 1 月 27 日
ok my bro , but i think this code don't work with color watermark ... just with gray

サインインしてコメントする。

Swati Nagpal
Swati Nagpal 2018 年 7 月 21 日

0 投票

I m working on lsb to hide text behind an image I have done the encryption part but I m having problem in decryption of lsb. So j request you to help me in that or pls share code for retrieval of hidden message from the image.

1 件のコメント

Image Analyst
Image Analyst 2018 年 7 月 21 日
I believe my answer did that, didn't it? If not, tell me what about it didn't work, and attach your cover image and secret image so that the problem can be demonstrated.

サインインしてコメントする。

Ariba Tariq
Ariba Tariq 2018 年 7 月 31 日

0 投票

I am working on LSB to hide parts of one secret image into multiple cover images and then trying to recover secret from all the cover images. It would help me a lot if anybody can guide me through this or share a code of similar concept.

1 件のコメント

Image Analyst
Image Analyst 2018 年 7 月 31 日
To process a sequence of files with my code, make my code a function, then put it inside a for loop that you get from the FAQ: https://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

サインインしてコメントする。

質問済み:

2011 年 11 月 26 日

コメント済み:

2021 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by