Is it possible to change the 'colormap' scale in Matlab? - matlab

I have 2 matrices. Matrix A contains values between 0 and 1 and matrix B contains values between 0 and 90. I would like to display an image with a different color for the numbers in each matrix.
When I use the colormap function with:
figure; colormap(jet); imshow(A);
The image displayed has several levels of gray, when I am supposed to have several colors (because I am using jet).
When I use the colormap function with:
figure; colormap(jet); imshow(B);
The image displayed is completely white, probably because my values are higher than 64 (which is the max of jet).
How can I solve these two problems? I read a lot of tutorials in several forums but I can't find the answer...
Thank you very much for answering my problem!

Just normalize the matrix by its max value if the values are more than 1. So for your B matrix try:
imshow(B/max(B(:)))

You can specify the colormap scaling and the number of actual colors within the colormap like so:
figure; imshow( A, [0 1], 'Colormap', jet(100) );
figure; imshow( B, [0 100], 'Colormap', jet(100) );
The jet(100) indicates 100 unique colors within the colormap to be used.

You are using the wrong function for the task in hand.
imshow expects an N by M by 3 array input, of the RGB channels of an image. When you use a 2D matrix the function assumes it's a grayscale image (it's like replicating it to 3 identical matrices to create these three channels - if all the channels in RGB have the same values you get grayscale colors). You can use this function together with a colormap to get a colored matrix, but there are much more convenient alternatives.
One simple function for getting a colored representation of a matrix is imagesc or (image if you want to scale the values by yourself). This function takes the values in your matrix, and assign them a color from the colormap you choose:
A = rand(10);
figure; colormap(jet); imagesc(A);
Another option is pcolor, which works a little different but give a similar result. pcolor attach the values to the vertices of the cells (in oppose to the center, as imagesc does), and interpolate the color in each cell from its' vertices. The resulted colored matrix is always smaller in one row and one column because it takes n+1 points (values in the original matrix) to define n gaps (the cells in the colored matrix). Here is an example:
A = rand(10);
figure; colormap(jet); pcolor(A);
shading flat

Related

Color Code MatLab Histogram Based on a Condition of a 2x2 Matrix

if I have a 2x2 Matrix such as
1 2
4 7
And I want to plot a histogram based on the condition that the diagonals should be the color green, and everything else except the diagonals should be red, how do I do that in Matlab.
I have tried separating the 2x2 matrix into two different vectors representing the diagonal and the others (except diagonals), but when I do:
hist(diagonals)
hist(others)
It doesn't really work and seems to just randomly color code things into one histogram (I do want just the one histogram though.) How do I do this correctly?
Found a solution to my problem, using hold on fixes this issue
histogram(diagonals, 'FaceColor', 'g')
hold on
histogram(others, 'FaceColor', 'r')
hold on

matlab colorbar for lineplot colored by value

I have 2d line plot in matlab where each line is colored according to a value. I would like to add a colorbar showing the color that corresponds to these values.
I got a solution to plot the lines according to the value I want, however I can not figure out to get the colorbar correctly. I have been searching on this but I am stuck.
Define an RGB color matrix COL.
(N x 3 low red to dark matrix corresponding to equally spaced values 0:1).
Sort the data according to their z value.
Interpolate the COL matrix to get values for all z values, giving the TRUECOL matrix for the lines.
Set the axiscolor-ordering to the TRUECOL matrix and plot the data.
minimalistic example:
% Generate 10 lines of 10 points
x = normrnd(0,1,10,10);
% The corresponding values are
% Note that these do not have to linearly spaced in real code
z = [0,0.05,0.1,0.11,0.12,0.2,0.4,0.45,0.8,0.9];
% Define colormatrix
COL = [0.996078431372549 0.878431372549020 0.823529411764706;...
0.937254901960784 0.231372549019608 0.172549019607843;...
0.403921568627451 0 0.0509803921568627];
% Interpolate the COL matrix to get colors for the data
TRUECOL = interp1(linspace(0,1,3),COL,z,'pchip');
% Set the axis coloring qnd plot the data
set(gcf,'DefaultAxesColorOrder',TRUECOL);
plot(x);
colormap(TRUECOL);
colorbar
I then change the colormap and plot the colobar, however the colors in the colorbar to not correspond to the values z. Is there a way of telling matlab which color corresponds to which value? Looking at the colorbar editor I see that CData must have something to do with it, but I cant find a way to specify that CData should be z.
My understanding is that you want the labels on the colorbar to go from 0 to 1, not 0 to 11. To fix this, use this caxis command. To get finer gradations of colors in the colorbar, you need to more finely interpolate the colormap. Try this:
colormap(interp1(linspace(0,1,size(COL,1)), COL, linspace(0,1,100)));
caxis([0,1]);
colorbar

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:
Now I want to superimpose these parameters on top of the original slice, and I create one RGB image to achieve this:
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.
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.
(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:
bwROIlocations is the logical matrix containing the ROI pixels:
Final result:
Thanks for the help Shai.

Using different colors in Matlab

In a 3D scatter graph of MATLAB I have 15 different clusters of data that I want to highlight. I can see MATLAB has 8 specific colors. Is there any other way I could use 7 more colors just to distinguish the clusters?
Thanks
I would recommend to use this File Exchange submission - Generate maximally perceptually-distinct colors
It allows you to create a colormap with very distinguished colors and apply them with COLORMAP function. See help for this submission for more options.
colors = distinguishable_colors(n_colors);
For 3D scatter you can use this colors as an argument (C) in SCATTER3:
scatter3(X,Y,Z,[],colors)
To use this colors for different lines set them as default color order either for current figure:
set(gcf,'DefaultAxesColorOrder',colors)
or all figures:
set(0,'DefaultAxesColorOrder',colors
You can use the color property using set. You must first get a handle h to the drawing objects and set(h,'color',[0.2 0.3 0.9]). The color is rgb ranging from 0 to 1 for each channel.
From the Matlab documentation:
scatter(X,Y,S,C) displays colored circles at the locations specified
by the vectors X and Y (which must be the same size).
S determines the area of each marker (specified in points^2). S can be
a vector the same length as X and Y or a scalar. If S is a scalar,
MATLAB draws all the markers the same size. If S is empty, the default
size is used.
C determines the color of each marker. When C is a vector the same
length as X and Y, the values in C are linearly mapped to the colors
in the current colormap. When C is a 1-by-3 matrix, it specifies the
colors of the markers as RGB values. If you have 3 points in the
scatter plot and wish to have the colors be indices into the colormap,
C should be a 3-by-1 matrix. C can also be a color string (see
ColorSpec for a list of color string specifiers).
So, for example, say that your clusters are given by the columns of the matrices X and Y, with the k'th column being the k'th cluster, X being the X coordinates, and Y being the Y coordinates. We can do what you want as follows:
% make some random data in clusters:
n = 15;
m = 42;
X = 0.2*rand(m,n) + repmat(rand(1,n),m,1);
Y = 0.2*rand(m,n) + repmat(rand(1,n),m,1);
% lets change the colour map:
colormap(jet);
% now plot each, one at a time, and each with a different colour:
hold on;
for k=1:n
scatter(X(:,k),Y(:,k),40,k/n*ones(m,1));
end
If you don't like these colours, you can change the colormap, and if you don't like the color maps, you can, as the other answer points out, insert any RGB values you want.

Turn a MATLAB plot into image

I have generated a plot like
figure; hold;
axis([0 10 0 10]);
fill([ 1 1 5 5], [5 1 1 5],'b')
and now I want to have this plot as an matrix so that I can i.e. filter the blog with a gaussian. Googleing I found this thread Rasterizing Plot to Image at MATLAB Central. I tried it, but I could only get it to work for line or function plots.
Do you have any ideas?
You can use GETFRAME function. It returns movie frame structure, which is actually rasterized figure. Field cdata will contain your matrix.
F=getframe;
figure(2)
imagesc(F.cdata);
What are the desired characteristics of your target matrix ? And what sort of images do you want to rasterise ?
You see, for the only example you have given us it's almost trivial to define a matrix representing your image ...
1. figmat = ones(10,10,3) % create a 10x10 raster where each entry is a triple for RGB, setting them all to 1 colours the whole raster white
2. figmat(2:5,2:5,1:2) = 0 % sets RG components in the coloured area to 0, leaving only blue
Your matrix is a raster to start with. Now, you can use the built-in function image to visualise your matrix. Have a look at the documentation for that function. And note that my suggestion does not meet the spec for use with image() and colormap().