Can any one have a code where a .wav file embedded into a image as a watermark using LSB technique?

11 views (last 30 days)
Image to image embedding using LSB is done.Just need a code where audio file will be embedded in image and create a new watermarked image.

Answers (1)

Image Analyst
Image Analyst on 6 Mar 2022
Try this:
% Demo by Image Analyst to hide an audio signal in a uint8 gray scale image by encoding it in the least significant bit.
%============================================================================================================================================
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
markerSize = 4;
%============================================================================================================================================
% Read in image
% grayImage = imread('moon.tif'); % This image is too small to contain the sound file.
baseFileName = 'concordorthophoto.png';
grayImage = imread(baseFileName); % A uint8 image.
numPixels = numel(grayImage);
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize)
%============================================================================================================================================
% Read in demo audio file that ships with MATLAB.
[y, fs] = audioread("handel_audio.wav");
y = y(:); % If stereo, stack left channel on top of right channel.
% Get the time axis
t = linspace(0, length(y) / fs, length(y))';
subplot(2, 2, [3,4]);
plot(t, y, 'b-');
grid on;
yline(0, 'Color', 'k'); % Draw line along x axis.
title('Original Audio Signal vs. Time that is Encoded into Above Right Image', 'FontSize', fontSize)
xlabel('Time (seconds)', 'FontSize', fontSize)
ylabel('Audio Signal', 'FontSize', fontSize)
ylim([-1, 1]);
xlim([0, max(t)]);
drawnow; % Force immediate screen painting so we can see the inputs while the encoding and decoding process go on.
% Play the sound.
% playerObject = audioplayer(y, fs);
% play(playerObject)
% Convert y to 16 bits to have enough resolution so that the sound signal value won't be changed much due to round off error.
y16 = uint16(rescale(y, 0, 65535));
% Sound was converted to uint16 so we will need 16 pixels to store to store one sound value.
% Make an output image initialized to the same as the original image.
stegoImage = grayImage;
% See if the image is big enough to hide all bits of the audio signal.
numPixelsRequired = length(y) * 16;
if numPixels < numPixelsRequired
errorMessage = sprintf('Cannot fit image.\nThe image is %d elements long.\nThe sound is %d elements long.\nThe sound file needs the image to have at least %d pixels (= 16 * %d) to contain the entire sound.\n', ...
numPixels, length(y), numPixelsRequired, length(y))
uiwait(errordlg(errorMessage));
else
fprintf('The image is %d elements long.\nThe sound is %d elements long.\nThe sound file needs the image to have at least %d pixels (= 16 * %d) to contain the entire sound.\n', ...
numPixels, length(y), numPixelsRequired, length(y));
end
%============================================================================================================================================
% Now encode the audio signal in the least significant bit of the uint8 gray scale image.
for k = 1 : numel(y16)
binaryNumberString = dec2bin(y16(k), 16);
if mod(k, 10000) == 0
fprintf('Changing pixel #%d of %d.\n', k, numel(y16));
end
% fprintf('%d in uint16 is %s in binary', y16(k), binaryNumberString);
imageIndex = (k - 1) * 16 + 1;
these16GrayLevels = stegoImage(imageIndex : imageIndex + 15);
for k2 = 1 : length(binaryNumberString)
gl = these16GrayLevels(k2);
if binaryNumberString(k2) == '1'
% Image gray level needs to be odd.
% If the gray level is not already odd, make it odd.
if rem(gl, 2) == 0 % if it's even...
% It's even. Add 1 to it to make it odd.
gl = gl + 1;
stegoImage(imageIndex + k2 - 1) = gl;
end
else % binaryNumberString(k2) == '0'
% Image gray level needs to be even.
% If the gray level is not already even, make it even.
if rem(gl, 2) == 1 % If it's odd...
% It's odd. Add 1 to it to make it even, unless it's already 255 because we can't have a value of 256 for a uint8 variable.
if gl <= 254
gl = gl + 1;
else
% Value is 255 initially. Make it even by making it 254, since we can't do 256.
gl = 254;
end
stegoImage(imageIndex + k2 - 1) = gl;
end
end
end
% these16GrayLevelsNow = stegoImage(imageIndex : imageIndex + 15)
end
subplot(2, 2, 2);
imshow(stegoImage, []);
impixelinfo;
title('With embedded sound file', 'FontSize', fontSize)
drawnow;
% Maximize the figure.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
%============================================================================================================================================
% Now undo the encoding process and recover the hidden sound
% by looking at the last (least significant) bit of the image gray levels and assigning that to a new sound.
yExtracted = zeros(length(y16), 1);
counter = 1;
for k = 1 : 16 : numel(y16) * 16
% Get a vector of 16 pixel values.
these16GrayLevels = stegoImage(k : k+15);
% Get the least significant bits of those 16 pixel values.
b = bitget(these16GrayLevels, 1);
% Convert to a string, then to a decimal number.
soundValue = bin2dec(sprintf('%c', b+48));
if mod(counter, 10000) == 0
fprintf('Assigning sound sample #%d of %d.\n', counter, numel(y16));
end
% Assign that sound value to the output sound signal.
yExtracted(counter) = soundValue;
counter = counter + 1;
end
% Convert back y from uint16 back to floating point like the original y.
yRecovered = yExtracted / 65535;
%============================================================================================================================================
% Play the recovered sound.
playerObject = audioplayer(yRecovered, fs);
play(playerObject)
% Double check that they're the same.
% If they're the same, the value below should be 1, true.
theyAreTheSame = isequal(y16, yExtracted)

Categories

Find more on Vibration Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!