简体   繁体   中英

RGB image pixels into its binary representation

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM