Image 嵌入后的RGB图像重建

Image 嵌入后的RGB图像重建,image,matlab,image-processing,rgb,steganography,Image,Matlab,Image Processing,Rgb,Steganography,我目前正在对每个通道的RGB图像执行LSB操作 但我对在处理LSB位后重建图像的方法感到困惑 %Load Cover Image 24-bit RGB Cover_Image=imread('RGB_24bits_palette_sample_image.jpg'); % Extract the individual red, green, and blue color channels. redChannel = Cover_Image(:, :, 1); greenChannel = Co

我目前正在对每个通道的RGB图像执行LSB操作

但我对在处理LSB位后重建图像的方法感到困惑

%Load Cover Image 24-bit RGB
Cover_Image=imread('RGB_24bits_palette_sample_image.jpg');

% Extract the individual red, green, and blue color channels.
redChannel = Cover_Image(:, :, 1);
greenChannel = Cover_Image(:, :, 2);
blueChannel = Cover_Image(:, :, 3);

% Get LSB's of each pixel for every channel.
redLsb = rem(redChannel, 2);
greenLsb = rem(greenChannel, 2);
blueLsb = rem(blueChannel, 2);

%Resizing the LSB into vector

redLsbVector = reshape(redLsb.',1,[]);
greenLsbVector = reshape(greenLsb.',1,[]);
blueLsbVector = reshape(blueLsb.',1,[]);

%Load Hidden message 
HiddenMessage = 'Hello';

%Convert Hidden message to Binary
HiddenMessageInBinary = reshape(dec2bin(HiddenMessage, 8)', 1, []) - '0';

%Start Embedding
MissMatchCount = 0;
for i=1:length(HiddenMessageInBinary)
    if redLsbVector(i)~= HiddenMessageInBinary(i)
    MissMatchCount=MissMatchCount+1;
    %embed
    redLsbVector(i) = HiddenMessageInBinary(i);
    end
end

%Reconstruct the image 

使用bitshift命令删除最低有效位后,您可以查看隐藏消息添加到图像信息的最后一部分。

修改颜色向量的lsb后,您需要将其重塑为原始图像的形状。然后可以将这些lsb值嵌入每个相应的颜色通道中。greenLsbVector和blueLsbVector,因此greenChannel和blueChannel没有被修改,所以我们只需要为红色通道工作。但过程是一样的

redLsb = reshape(redLsbVector,size(redLsb'))';
redChannel = bitset(redChannel,1,redLsb);
bitset命令将位置1 lsb处的redChannel位设置为redLsb显示的任何值

更干净的方法

你用不必要的变量把代码弄得乱七八糟。为什么提取绿色通道及其lsb值,并将它们转换为向量,如果您永远不会使用它们中的任何一个?如果只使用一组lsb值,为什么要从红色通道中提取所有lsb值?为什么要提取它们、修改它们并在可以修改它们的地方将它们放回原处?如果你没有采取这些不必要的措施,你的问题可能不会出现

您还应该尽可能避免在Matlab中使用循环,因为它针对以下方面进行了优化

总之,您的代码可能是这样的

coverImage = imread('RGB_24bits_palette_sample_image.jpg');
message = 'Hello';

%% EMBEDDING
coverPlane = coverImage(:,:,1);
bits = uint8(reshape(dec2bin(message,8)',1,[]) - '0');
nBits = length(bits);

coverPlane(1:nBits) = bitset(coverPlane(1:nBits),1,bits);

%% EXTRACTION
extBits = bitget(coverPlane(1:nBits),1)';
extMessage = char(bin2dec(reshape(int2str(extBits),8,length(extBits)/8)')');
实际嵌入发生在一行中,不需要图像重建,因为它是在原地完成的。如果要保留原始封面,可以制作其副本并对其应用更改

在提取方面,bitget只提取修改后像素的lsb值,而不处理其余数据。因此,isequalbits,extBits'应该返回true


extMessage恢复原始字符串。嵌套命令简单地颠倒了获取字符串并将其转换为二进制序列的步骤。

关于此代码的意图,有两个问题。在嵌入上述代码后,您使用MissMatchCount做什么?您开始将消息嵌入红色平面,但是如果您有一条很长的消息不适合,您会继续嵌入绿色平面吗?还是只穿红色?