MATLAB M x N x 24 数组到位图

MATLAB M x N x 24 array to bitmap

我正在使用 MATLAB。

我有一个 M x N 的数组,我用 1 或 0 填充它来表示二进制模式。我有 24 个 "bit planes",所以我的数组是 M x N x 24。

我想将此数组转换为 24 位 M x N 像素位图。

像这样的尝试:

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

产生错误。

如有任何帮助和建议,我们将不胜感激。

%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);

对其进行调试

让我们假设 A 是输入 M x N x 24 大小的数组。我还假设每个 3D "slices" 中的那些 24 bits 具有 red-channel 的第一个 one-third 元素,[=16= 的下一个 one-third ] 并将 one-third 作为 blue-channel 元素。因此,考虑到这些假设,使用 fast matrix multiplication in 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')

输出-