2015-01-24 2 views
3

Я работаю в MATLAB.MATLAB M x N x 24 массив для растрового изображения

У меня есть массив из 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') 

ошибок продукции.

Любая помощь и предложения будут оценены.

ответ

2

Давайте предположим, что A является массив размера входного M x N x 24. Я также предполагаю, что те, что 24 bits в каждом из своих трехмерных «срезов» имеют первые третьи элементы для red-channel, затем one-third для 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

+0

Divakar - Это решило мою проблему. Благодарю за помощь. – tomdertech

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

+0

Я не уверен, что это на самом деле пытаетесь достичь? Что я действительно конвертирую в BMP? – tomdertech

Смежные вопросы