MATLAB M x N x 24数组转换成位图

3

我正在使用MATLAB工作。

我有一个M x N的数组,我用1或0来填充它以表示二进制模式。我有24个这样的“位面”,所以我的数组是M x N x 24。

我想将这个数组转换为一个24位的M x N像素位图。

尝试过以下方法:

test = image(1:256,1:256,1:24);
imwrite(test,'C:\test.bmp','bmp')

产生错误。

任何帮助和建议都会受到赞赏。

2个回答

2
假设A是输入的大小为M x N x 24的数组。我还假设在其3D“切片”的每个24位中,前三分之一的元素用于红通道,下一个三分之一用于绿通道,并将剩余三分之一作为蓝通道元素。因此,在这些假设的基础上,使用MATLAB中的快速矩阵乘法,一种有效的方法可能是这样的 -
%// Parameters
M = 256;
N = 256;
ch = 24;

A = rand(M,N,ch)>0.5; %// random binary input array

%// Create a 3D array with the last dimension as 3 for the 3 channel data (24-bit)
Ar = reshape(A,[],ch/3,3);

%// Concatenate along dim-3 and then reshape to have 8 columns, 
%// for the 8-bit information in each of R, G and B channels
Ar1 = reshape(permute(Ar,[1 3 2]),M*N*3,[]);

%// Multiply each bit with corresponding multiplying factor, which would
%// be powers of 2, to create a [0,255] data from the binary data
img = reshape(Ar1*(2.^[7:-1:0]'),M,N,3); %//'

%// Finally convert to UINT8 format and write the image data to disk
imwrite(uint8(img), 'sample.bmp')

输出 -

在此输入图片描述


Divakar - 这解决了我的问题。谢谢你的帮助 - T - tomdertech

1
%some example data 
I=randi([0,1],256,256,24);
%value of each bit
bitvalue=permute(2.^[23:-1:0],[3,1,2])
%For each pixel, the first bit get's multiplied wih 2^23, the second with 2^22 and so on, finally summarize these values.
sum(bsxfun(@times,I,bitvalue),3);

为了理解这段代码,请尝试使用输入 I=randi([0,1],1,1,24); 进行调试。

我不确定这实际上是在尝试实现什么?我到底要将什么转换为BMP格式? - tomdertech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接