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

时间:2015-01-15 17:13:21

标签: matlab image-processing rgb steganography

MATLAB和图像处理的新手,我搜索但无法找到答案

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

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

红色00000001

蓝色01100101

绿色11010101

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

任何指导/帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

你可以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

答案 1 :(得分:2)

另一种方法是使用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的lsb中嵌入 pixel(可以是0或1)的值。 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
相关问题