Search code examples
matlabwatermarklsb

How to implement invisible watermark image in image using 3 LSB


I need to implement 3 LSB watermarking without using the existing functions in MATLAB.

I need my function to get 2 images, both gray-level, and preform watermarking using 3 LSB.

I tried the following, but the result of the original image subtracted from the new image is all zeros which means they are the same.

function [C] = Q2(image,watermark)
% clc;
% image=imread('moon.tif');
% watermark=imread('cameraman.tif');

[X,Y] = size(image);
rewatermark = imresize(watermark,[X,Y]); % resizing watermark to fit image

% iterate the 3 LSB of the watermark and set them to a copy of the original
% image 3 LSB
C = image;
for i = 1:X
    for j = 1:Y
        for k = 1:3
            if(bitget(rewatermark(i,j),k) == 1)
                bitset(C(i,j),k,1);
            else 
                bitset(C(i,j),k,0);
            end
        end
    end
end

subplot(1,3,1)
imshow(image);
title('Original');
subplot(1,3,2);
imshow(rewatermark) 
title('Watermark');
subplot(1,3,3)
imshow(C)
title('Invisble watermarked'); 
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Q2 - Results','numbertitle','off')

Solution

  • Instead of bitset(C(i,j),k,1), use: C(i,j) = bitset(C(i,j),k,1)

    Except of OOP programing, MATLAB does not support references (or pointers).
    Executing bitset(C(i,j),k,1), does not modify the content of C(i,j), it sets the relevant bit to 1, and return the modified value.

    Note:
    I recommend suing the high bits of rewatermark (bits 6,7,8) instead of the lower bits 1,2,3 that are mainly "noise" (but it depends on your objectives).


    Here is a code sample:

    %function [C] = Q2(image,watermark)
    clc;
    image=imread('moon.tif');
    watermark=imread('cameraman.tif');
    
    [X,Y] = size(image);
    rewatermark = imresize(watermark,[X,Y]); % resizing watermark to fit image
    
    % iterate the 3 LSB of the watermark and set them to a copy of the original
    % image 3 LSB
    C = image;
    for i = 1:X
        for j = 1:Y
            for k = 1:3
                b = bitget(rewatermark(i,j), k+5); % Use the high bits 6,7,8 (instead of lower bits 1,2,3 that are mainly "noise")
                C(i, j) = bitset(C(i, j), k, b); % Set bit k to the value of b (1 or 0)
                
                %if(bitget(rewatermark(i,j),k) == 1)
                %    bitset(C(i,j),k,1);
                %else 
                %    bitset(C(i,j),k,0);
                %end
            end
        end
    end
    
    subplot(1,3,1)
    imshow(image);
    title('Original');
    subplot(1,3,2);
    imshow(rewatermark) 
    title('Watermark');
    subplot(1,3,3)
    imshow(C)
    title('Invisble watermarked'); 
    set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
    set(gcf,'name','Q2 - Results','numbertitle','off')
    
    reconstructed_rewatermark = bitshift(C, 5);
    imshow(reconstructed_rewatermark);title('reconstructed_rewatermark');