0

New to MATLAB and Image processing, I searched but couldn't find the answer

How can I view the binary representation for 24-bit RGB image ( for each channel )

So for example I would like to know that pixel number x ( any number ) in the image has

Red 00000001

Blue 01100101

Green 11010101

My intention is to manipulate such values then reconstruct the image

Any guidance/Help would be appreciated

Shai
  • 111,146
  • 38
  • 238
  • 371
SeRo
  • 77
  • 4
  • 15
  • I suspect that you don't need to convert the numbers at all. You can perform any bit operations on the uint8 without change of representation as in `c = bitxor(img(x,y,1),0x80);`. – beaker Jan 15 '15 at 17:45
  • I second what beaker said. What are you exact intentions when you get, for example, 00000001 for Red? – Reti43 Jan 15 '15 at 17:55
  • To check the LSB value and either keep or change it – SeRo Jan 15 '15 at 17:58
  • 1
    Fetch value of lsb (used for extraction): `bitand(value,1)`. Change value of lsb (used for embedding): `bitand(value,254) + bit`, where `bit` is either 1 or 0. It doesn't matter whether the bit already has the same value as the lsb of the pixel. – Reti43 Jan 15 '15 at 18:00

2 Answers2

2

You can for-loop this

bitmap = false( [size(img,1), size(img,2), 24] );
for ci=1:3
    for bit=0:7
        bitmap( :,:, (ci-1)*8+bit+1 ) = bitand( img(:,:,ci), uint8(2^bit) );
    end
end

Reconstruction is simpler

reconstruct = zeros( [size(img,1), size(img,2), 3], 'uint8' );
for ci=1:3
    reconstruct(:,:,ci) = sum( bsxfun(@power, ...
        bitmap(:,:,(ci-1)*8 + (1:8) ),...
        permute( uint8(2.^(7:-1:0)), [1 3 2] ) ), 3 );
end
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thanks Shai, The code seems to work as intended, but im having a hard time understanding the structure. Does bitmap(1,1,1:8) Represent the value of the red values for the first pixel? – SeRo Jan 15 '15 at 17:46
  • 1
    `bitmap(1,1,1:8)` are the bits of the red pixel, `bitmap(1,1,9:16)` of the green, etc. The bits are from the most significant to the least significant, so `bitmap(1,1,8)`, `bitmap(1,1,16)` and `bitmap(1,1,24)` are the least significant bits of the red, green and blue channels respectively. – Reti43 Jan 15 '15 at 17:51
  • Appreciate the help Shai. This question might be unrelated but, is this the best way to manipulate/Check the lsb for each channel (r,g,b) for every pixel in the image? – SeRo Jan 15 '15 at 17:56
  • @SeRo no. check out [this answer](http://stackoverflow.com/a/27640832/1714410) I believe you are trying to achive the same goal... – Shai Jan 15 '15 at 17:59
2

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
Reti43
  • 9,656
  • 3
  • 28
  • 44