image - MATLAB M x N x 24 数组到位图

标签 image matlab image-conversion

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

输出-

enter image description here

关于image - MATLAB M x N x 24 数组到位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126615/

相关文章:

matlab - 按行的交叉索引

Matlab: bar 不存在公共(public)属性 CData

java - 将 BufferedImage 转换为 ImageIcon

Imagemagick 将 PDF 转换为 JPEG : FailedToExecuteCommand `"gswin32c. exe”/PDFDelegateFailed

image - 从 NSImage 转换 glTexture 的最有效方法?

javascript - 通过 Webpack 导入 Module 中的图片

matlab - 如何使用 MATLAB 在卡车中查找木材?

c# - 将透明 PNG 转换为具有非黑色背景颜色的 JPG

image - 如何在 Prolog 动态提供的 HTML 页面中包含图像?

C++如何使用CWnd *对象加载图片?