如何使用 3 LSB

问题描述

我需要在不使用 MATLAB 现有函数的情况下实现 3 LSB 水印。

我需要我的函数使用 3 LSB 获取 2 个图像,包括灰度和预制水印。

我尝试了以下方法,但是从新图像中减去原始图像的结果都是零,这意味着它们是相同的。

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,k,1);
            else 
                bitset(C(i,0);
            end
        end
    end
end

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

解决方法

代替 bitset(C(i,j),k,1),使用:C(i,j) = bitset(C(i,1)

除 OOP 编程外,MATLAB 不支持引用(或指针)。
执行bitset(C(i,1),不修改C(i,j)的内容,将相关位设置为1,并返回修改后的值。

注意:
我建议起诉 rewatermark 的高位(位 6,7,8)而不是低位 1,2,3,主要是“噪音”(但这取决于您的目标)。


这是一个代码示例:

%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,k+5); % Use the high bits 6,8 (instead of lower bits 1,3 that are mainly "noise")
            C(i,b); % Set bit k to the value of b (1 or 0)
            
            %if(bitget(rewatermark(i,k) == 1)
            %    bitset(C(i,1);
            %else 
            %    bitset(C(i,0);
            %end
        end
    end
end

subplot(1,3,1)
imshow(image);
title('Original');
subplot(1,2);
imshow(rewatermark) 
title('Watermark');
subplot(1,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');