我在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')产生错误。
任何帮助和建议将不胜感激。
I am working in MATLAB.
I have a an array of M x N and I fill it with 1 or 0 to represent a binary pattern. I have 24 of these "bit planes", so my array is M x N x 24.
I want to convert this array into a 24 bit M x N pixel bitmap.
Attempts like:
test = image(1:256,1:256,1:24); imwrite(test,'C:\test.bmp','bmp')Produce errors.
Any help and suggestions would be appreciated.
最满意答案
假设A是输入M x N x 24大小的数组。 我还假设每个3D“切片”中的24 bits具有red-channel的前三分之一元素,接下来是green-channel的三分之一,而blue-channel元素则占三分之一。 因此,考虑到这些假设, 在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')输出 -
Let's assume A is the input M x N x 24 sized array. I am also assuming that those 24 bits in each of its 3D "slices" have the first one-third elements for the red-channel, next one-third for the green-channel and rest one-third as blue-channel elements. So, with these assumptions in mind, one efficient approach using the fast matrix multiplication in MATLAB could be this -
%// 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')Output -
更多推荐
发布评论