please tell me the coding of how to embed a image into another image by using LSB....i need it

Answers (2)

Here's a demo I wrote for someone a while ago. See if it's helpful to you:
% 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 Comments

Can you provide me code for qr code authentication for medical images smoothening using histogram techniques?
@RAKESH KUCHANA That sounds like a complete master thesis project. It also doesn't sound strongly related to this post.
Have you started at all? You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
Given long enough time, and enough incentive, YES, I could.
However, in practice I would probably not do it myself, and would instead assign it as an Honours Project, and then monitor the "homework assistance" services to see whether the student was in fact doing their own work.
I'm not even sure what a QR code has to do with authentication, medical images, smoothing them, or the histogram of them. Regardless, I'm not sure what @RAKESH KUCHANA's question has to do with the original question on LSB watermarking (demo attached).
QR Code Authentication has to be provided for the medical images to embed the necessary medical information through the QR Code and the encrypted QR Code is embedded into the medical image (also known as Steganography).Both QR code image & Medical image are resized & cropped to same size(say 256 x 256 pixels) for histogram equalization. Using histogram techniques to evaluate the performance metrics of final image like MSE, PSNR etc. The final image is decrypted to the normal medical image & QR code, in which finally the necessary information can be accessed by the end user. This is the project description.
Now can you develop MATLAB source code for the above project description? Please respond.
Yes, I could develop MATLAB source code for the above project description. However, due to the laws about cryptography in my country, I would not be able to provide the result to anyone who was not physically present in my country (it wouldn't be my legal problem if they tried to leave the country with the software.)
And as I am rather busy these days, I would be much more likely to assign the task to a student as a term project, and then I would do cross-checks to ensure that the student had done the work themselves.
@RAKESH KUCHANA, I also could, as I'm sure you could also. Not sure if your "can" meant "will you, for me," or not. Or if it meant "are you capable of it (do you have the skillset)"? If "can" did intend to have me write it for you and turn it over to you, then no I will not but here are some people who would be happy to : Click here
I have some code in the file exchange that will hide text in an image. Since a QR code is essentially a URL, you could store the QR code as text rather than an image like I did with my watermarking example attached above. Encoding text into image gray levels
clc;
x = imread('brain_mri(resized).png');
d = 0.7;
y = imread('brain_stego(resized).png');
Y1 = y;
[R,C] = size(Y1);
for i=2:R-1
for j=2:C-1
tmp = Y1(i-1:i+1, j-1:j+1);
if 0<Y1(i,j) && Y1(i,j)<255
Y1(i,j) = Y1(i,j);
else
b = sort(tmp(:));
b1 = b(b~=0);
b2 = b1(b1~=255);
b3 = mean(b2);
Y1(i,j) = b3;
end
end
end
subplot(1,3,1); imshow(x);
subplot(1,3,2); imshow(y);
subplot(1,3,3); imshow(Y1);
x = uint8(x);
y = uint8(y);
Y1 = uint8(Y1);
MXN = R*C;
MAE = (sum(sum(abs(((x)-(Y1))))))/MXN; %MARKED
MSE = sum(sum(((x)-(Y1)).^2))/MXN;
PSNR = 10*log10(255^(2)/MSE);
gg = sum(sum(((double(y))-double(x))).^(2));
gg1 = sum(sum(((double(Y1))-double(x))).^(2));
IEF = gg./gg1;
val = ssim(Y1,x);
%This is the code for performance metrics of two images comparision. In the bolded text (MAE = .....), I am getting error. Please rectify my problem. I've attached the screenshot for the clear observation.
I predict that your second image is not actually grayscale (but might look like it is). You should examine size() of the two images that you read in.
I agree with Walter. Never use size like that on an image. Always have 3 output arguments just in case it's color. See Steve's blog for more on the reason for that:
Hello sir, can I get the base paper for above project?
Sorry, no. It sounds like you've quoted from some particular paper and therefore you should ask the authors if they can/will supply you with code.
For the code you have written, can I get the base paper?
The work of the demo is done by the line
watermarkedImage(row, column)=bitset(originalImage(row, column), bitToSet, watermark(row, column));
which is just a straight-forward setting of the least significant bit of each location in the output according to the bits of the watermark. There is no paper associated with setting a single bit.
There are papers that explore the perceptual effect of setting particular numbers of least signficant bits under various circumstances, and there are papers that explore how well the least significant bits survive JPEG compression, and which explore ways to reduce the errors. But this demo is not worried about any of those effects.

Sign in to comment.

Hello can anybody tell me an algorithm about embedding data into a 3d medical image i needed it ana i havn't no idea please help me with any code that is correct.Thanks

2 Comments

You should start your own thread since you're asking your own question, and not answering Manisha's question.
You already have an active question on this topic, but you have not replied to the people who have provided answers or comments.
http://www.mathworks.com/matlabcentral/answers/32237-watermarking-medical-image-3d

Sign in to comment.

Asked:

on 16 Mar 2012

Commented:

on 25 May 2021

Community Treasure Hunt

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

Start Hunting!