fnery
fnery

Reputation: 758

MATLAB: Show colorbar of a grayscale image in a figure containing a RGB image

Suppose we have two images:

1) RGB Image:

% initialize RGB image:
rgbIm = zeros(100, 100, 3); 
rgbIm(20:80, 20:80, 1) = 1;

2) Grayscale Image:

% initialize grayscale image:
grayIm = zeros(100);
grayIm(20:80, 20:80) = 1;

Lets show both of them:

figure, imshow(rgbIm),  colormap('jet'), colorbar;
figure, imshow(grayIm), colormap('jet'), colorbar;

As we can see, the colorbar in the 2nd figure (i.e. grayscale image) makes total sense. On the other hand I can't really understand the information given by the colormap in the 1st figure.

What I would like to achieve is to display the colorbar of the grayscale image in the figure corresponding to the RGB image.

It might seem that this does not make sense but this is just a very minimal example that I just made up to show what I would like to do in a larger project.

Any thoughts?

Thanks a lot :)

EDIT1: Allow me to explain why I would need this

Suppose I compute some physiological parameters one MRI slice in certain regions of interest, yielding something like this:

parametric image

Now I want to superimpose these parameters on top of the original slice, and I create one RGB image to achieve this:

superimposition on the original image

The colormap does not make sense, and this is why I would like to display the colormap corresponding to the parametric image in the RGB image (i.e. the superimposition image).

Any ideas?

Thank you for your attention.

Upvotes: 0

Views: 6229

Answers (2)

fnery
fnery

Reputation: 758

(Answering my own question so I can hopefully help someone in the future)

I just accomplished what I intended, with the help of the above answer and a post from Steve Eddins' blog. Here's how:

baseImage = baseImage/(max(baseImage(:))); % normalize base (anatomical) image
rgbSlice  = baseImage(:,:,[1 1 1]);        % converting to RGB (ignore colormaps)
imshow(parameterROIImage, []);             % show parametric image
colormap('jet');                           % apply colormap
hold on;
h = imshow(rgbSlice);                      % superimpose anatomical image
set(h, 'AlphaData', ~bwROIlocations);      % make pixels in the ROI transparent
colorbar;       

where parameterROIImage is:

enter image description here

bwROIlocations is the logical matrix containing the ROI pixels:

enter image description here

Final result:

enter image description here

Thanks for the help Shai.

Upvotes: 1

Shai
Shai

Reputation: 114786

I still think what you are asking does not make sense.
Let me explain:
What is colorbar? Colorbar is a representation of a function (mapping) from gray-level (scalar) to color. Using jet, in your example, you map the 0 to dark blue and 1 to red.
By asking for a colorbar for an RGB image, you are actually asking for a mapping from RGB triplet to RGB color -- this mapping is the identity mapping! you do not need a colorbar to see this mapping, each pixel represent it!

EDIT

After seeing the edited question, I revise my answer a bit:
Since both MRI signal and whatever physiological parameters you computed makes no sense in RGB space, you have two 1D mappings:
1. MRI signal at each voxel to gray level ("gray" colormap)
2. Physiological measure at each voxel to "jet" color

What I usually do in this cases is convert the 2D MRI slice into RGB gray image by simply replicate it along the third dimension

rgbSlice = twoDSlice(:,:,[1 1 1]);

Now show the images on top of each other

figure;
imshow( rgbSlice );
hold on;
h = imshow( physiologicalParams );
colormap jet;
set(h, 'AlphaData', .5 ); 
colorbar

You might need to play with the color mapping of the axes using caxis or clim to get it right.

Upvotes: 1

Related Questions