将 RGB 图像像素转换为二进制表示

RGB image pixels into its binary representation

刚接触 MATLAB 和图像处理,我搜索了但找不到答案

如何查看 24 位 RGB 图像的二进制表示(对于每个通道)

例如,我想知道图像中的像素数 x(任意数字)有

红色 00000001

蓝色 01100101

绿色 11010101

我的目的是操纵这些值然后重建图像

任何 guidance/Help 将不胜感激

你可以for循环这个

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

重构更简单

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

另一种方法是使用 dec2bin.

b = dec2bin(img(1,1),8);

例如img(1,1)像素点的红绿蓝值分别为255、223和83,则得到

11111111
11011101
01010011

其中 b(1,:) 是红色二进制 (11111111),b(2,:) 绿色 (11011101) 等


但是,为了改变lsb的值,这不是首选的或最直接的方式。考虑使用 bitwise and 操作。

嵌入 bit 的值(可以是 0 或 1)在 pixel 的 lsb 中。 pixel这里指的是一个特定的值,所以只有像素的红色、绿色或蓝色分量。

bitand(pixel,254) + bit;

此处,掩码 254(或二进制 11111110)将像素的 lsb 归零。

     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

虽然将 1 归零然后将 1 重新嵌入其中似乎是多余的,但它仍然更直接,然后检查 bitpixel 的 lsb 是否不同并仅在其中更改它们案例.

提取 pixel

的lsb值
bitand(pixel,1);

此操作将 pixel 的 lsb 以外的所有位清零,有效地为您提供了它的值。

     11010101    // pixel value; we are interested in the value of the lsb, which is 1
and  00000001    // mask
     00000001    // result