Displaying a 4D matrix with 2D colormap in Matlab

51 Views Asked by At

I have a 1D array of double type ,size 1089720 and I reshape it 3D (1009 x270 x4 ) matrix to be displayed as RGB and/or RGBA (I am not sure if the 4th in (1009 x270 x[1:4] )value is Alaph). Its values range between [0-255] . When I directly try to display it with imshow(), I get error telling me "Color data must be an m-by-n-by-3 or m-by-n matrix." . The 3D data I have is to be diaplayed as color image and there is some shape , a ball , in the final display. I can't get that shown , here is what I got when i try like : imshow(data, 1009,270,1:3) , colormap jet; i.e by dropping one value from third dimension but I get messed up display as shown below:-

Image (1009,270,1:3) displayed with jet colormap

The expected display was as shown here: -

Here is what the data should have displayed By expected , I mean a different person displayed it this way and didnt share me hint on it/( That is why I am here to ask)

Is there a matlab function that display such data as colored image ? I was planning to display this data with custom colormap. This color map is of shape 256x16 where 16 refers to group of RGB values across column. I checked matlab documentation. All I got is that colormap need to be organized as a three column matrix , MX3 where 3 denoting RGB values and M is the number of such RGB values. My custom color map is 256 x (16x3) or based on matlab expectation, it will be 4096x3 (4096 stretched out form of 256x16).

I try to display the same data using my custom color map of shape 4096x3 and I got the following : -

Image with custom color map of shape 4096x3

and my custome colormap looks like this colorbar of my custome colormap ... some pattern is repeating , it looks my custome color map has dimension issue.

The aim was to let matlab pick one of the 16 color in one row and match and move on across row ...

But as can be seen, the colorbar itself seems a bit messed up and not clear.

here is how I loaded my custom colormap and try to display my matrix in matlab :

fid = fopen('1.bin', 'r');
samples = 1009;
lines = 270;
data = fread(fid);


fclose(fid);

data = reshape(data,270,1009,4)/255;    // to set it in range 0-1 
data2 = data(:,:,1:3);


% load color map from text file 
Decimal_colomap_data =  readmatrix('SampleColor_Map.txt');   // shape is 256x16 ( all entries decimal)


% Convert decimal values to hexadecimal values
hex_colormap_data = dec2hex(Decimal_colomap_data);   // hexadcimal  shape 4096 


hexData = dec2hex(Decimal_colomap_data);    %% Eg : one hexa value :  9B9A04 - 


% Convert the hexadecimal values to RGB decimal values  : one hexacale 9B9A04 ,  R = 9B , G = 94 , B= 04

red = hex2dec(hexData(:, 1:2));   
green = hex2dec(hexData(:, 3:4));
blue = hex2dec(hexData(:, 5:6));

%% Organize my R g B data  ... shape will be 4096 x 3 
colorMap = [red, green, blue];  

% ColorMasp_reshaped = reshape(colorMap,[256,16,3]);

colorMap1 = uint8(colorMap);   





imshow(data2)
colormap(colorMap1);  // see the 2nd image above 

% colormap(jet);   // i tried it with this too , and go the above image 


I appreciate any help on how to use this custom colormap and display the data in color RGB form along with the corrosponding colorbar.

Thank you and my pardon for some trivial point I may have messed when asking !

0

There are 0 best solutions below