Another way is using dec2bin
.
b = dec2bin(img(1,1),8);
For example, if the red, green and blue values of the pixel img(1,1)
are 255, 223 and 83, you will get
11111111
11011101
01010011
Where b(1,:)
is the binary for red (11111111), b(2,:)
for green (11011101), etc.
However, for the intention of changing the value of the lsb, this is not the prefered, or most direct way. Consider using the bitwise and operation.
Embedding the value of bit
(can be 0 or 1) in the lsb of pixel
. pixel
here refers to one specific value, so only the red, or green, or blue component of a pixel.
bitand(pixel,254) + bit;
Here, the mask 254 (or 11111110 in binary) zeroes out the lsb of pixel.
11010101 // pixel value
and 11111110 // mask
11010100 // result
+ 1 // assuming the value of bit is 1
11010101 // you have now embedded a 1 in the lsb
While zeroing out a 1 to then embed a 1 back in it seems superfluous, it's still more direct then checking whether bit
and the lsb of pixel
are different and changing them only in that case.
Extracting the lsb value of pixel
bitand(pixel,1);
This operation zeroes out all the bits except from the lsb of pixel
, effectively giving you its value.
11010101 // pixel value; we are interested in the value of the lsb, which is 1
and 00000001 // mask
00000001 // result