我在 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')
产生错误。
如有任何帮助和建议,我们将不胜感激。
最佳答案
假设 A
是输入 M x N x 24
大小的数组。我还假设每个 3D“切片”中的 24 位
具有 red-channel
的前三分之一元素,接下来的 三分之一
用于 green-channel
,其余三分之一作为 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')
输出-
关于image - MATLAB M x N x 24 数组到位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126615/