RGBa image from intel realsense to matlab

984 views Asked by At

I'm trying to send a frame from the intel realsense camera to matlab. dDisplaying the image by imshow(...) or image(...) didn't make the job since the image is an rgba image... I have sent the image as an object from C# :

matlab.Feval("getImage", 1, out result, bitmap_data);

Is there a function that can display the frame ?

1

There are 1 answers

4
andrew On

You will have to play with the exact Feval implementation, but if you were to impelment it directly in matlab there are two options

1.Simply ignore the alpha channel

%this says show all rows, all cols, for channels 1 through 3 or R,G,B
imshow(rgba_im(:,:,1:3));

2.Use the alpha channel

%this says show all rows, all cols, for channels 1 through 3 or R,G,B
%also saves the handle to the newly drawn image
hndl = imshow(rgba_im(:,:,1:3));

%isolates alpha channel
alpha = rgba_im(:,:,4);

%displays alpha channel
set(hndl , 'AlphaData', alpha);

EDIT

Now that I know your data isn't in the standard rgba format here is the code to fix it, the comments should tell you everything you need

[num_rows,num_cols_x4]=size(rgba_matrix);

%we have to transpose the array to get it to unfold properly, which is
%first by columns, then rows
at = rgba_matrix.';

%converts data from [r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3....] to 
% red_chan   = [r1 r2 r3...]
% green_chan = [g1 g2 g3...]
% blue_chan  = [b1 b2 b3...]
% alpha_chan = [a1 a2 a3...] 
% it says start at some index and grab every 4th element till the end of the
% matrix
red_chan = at(1:4:end);
grn_chan = at(2:4:end);
blu_chan = at(3:4:end);
alp_chan = at(4:4:end);

% reshape each channel from one long vector into a num_rows x (num_cols_x4/4)
red_chan = reshape(red_chan, num_cols_x4/4, num_rows).';
grn_chan = reshape(grn_chan, num_cols_x4/4, num_rows).';
blu_chan = reshape(blu_chan, num_cols_x4/4, num_rows).';
alp_chan = reshape(alp_chan, num_cols_x4/4, num_rows).';

% we concatenate the channels into a num_rows x (num_cols_x4/4) x 4 matrix
standard_rgba = cat(3,red_chan,grn_chan,blu_chan,alp_chan);

From this point you can do the processing I suggested using the standard_rgba array. There may be a more efficient way to write this code, but I wanted to make it as clear and easy to follow as possible, hope this helps