0

I'm currently preforming LSB operations on RGB image for each channel

But I'm confused on the way to reconstruct the image after manipulating the LSB bits

%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 
SeRo
  • 77
  • 4
  • 15
  • Two questions regarding the intention of this code. What do you use `MissMatchCount` for after the embedding in your code above? You start embedding your messsage in the red colour plane, but if you have a very long message that doesn't fit there, would you continue the embedding in the green plane as well? Or only stick with the red? – Reti43 Jan 16 '15 at 10:12

2 Answers2

1

After modifying the lsb of the colour vectors, you need to reshape them in the shape of the original image. You can then embed these lsb values in each respective colour channel. The greenLsbVector and blueLsbVector (and therefore the greenChannel and blueChannel) were not modified, so we only have to work for the red channel. But the process would be the same.

redLsb = reshape(redLsbVector,size(redLsb'))';
redChannel = bitset(redChannel,1,redLsb);

The bitset command sets the bit of redChannel at location 1 (lsb) to whatever value the redLsb says so.


A cleaner approach

You clutter your code with unnecessary variables. Why extract the green channel and its lsb values and turn them into a vector if you are never going to use any of them? Why extract ALL of the lsb values from the red channel if you're only going to use a bunch of them? Why extract them, modify them and put them back in when you can modify them in place? If you hadn't taken any of these unnecessary steps, your problem would have probably not arisen.

You should also avoid loops when you can in Matlab, because it is optimised for vectorization.

All in all, your code could look like this.

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)')');

The actual embedding takes place in one line and no image reconstruction is required because it's done in place. If you want to keep the original coverPlane, you can make a copy of it and apply the changes to it.

On the extraction side of things, bitget extracts only the lsb values of the modified pixels and doesn't bother with the rest of the data. It follows that isequal(bits,extBits') should return true.

The extMessage restores the original string. The nested commands simply reverse the steps of taking a string and converting it to a binary sequence.

Reti43
  • 9,656
  • 3
  • 28
  • 44
-1

You can look at the last part of this answer where the hidden message is added to the image information after removing the least significant bit using bitshift commands.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371