MATLAB M x N x 24阵列到位图(MATLAB M x N x 24 array to bitmap)

编程入门 行业动态 更新时间:2024-10-25 14:24:48
MATLAB M x N x 24阵列到位图(MATLAB M x N x 24 array to bitmap)

我在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 -

enter image description here

更多推荐

本文发布于:2023-07-24 01:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   阵列   MATLAB   array   bitmap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!