Matlab - attach scalar value to RGB colours - matlab

When I read the following image into Matlab I am obtaining a 3D matrix which basically contains the values of RGB colour samples which compose every pixel within the image.
Is there any Matlab function which I can use to assign a scalar value between lets say [-10, 10] to every pixel within the image based on the RGB values? Pure red should be like 10, yellow should be 5, green should be 0, blue should be like -8 and cyan should be like -10.

Have a look at RGB2IND: http://www.mathworks.com/help/techdoc/ref/rgb2ind.html
You could then replace the Nx3 index output with your own custom N element index vector.

As you can see from running
colorlist=[1 0 0;1 1 0;0 1 0; 0 0 1; 0 1 1];
valuelist=[10 5 0 -8 -10];
figure;
hold all;
for i=1:10
bar(i,i,'FaceColor',colorlist(i,:))
end;
the colorlist defined above corresponds to the colors you are interested in.
To solve your question, for each pixel in your image, you would have to determine which RGB values are exactly zero and which ones are exactly one to determine between which pair you want to interpolate. For example, assuming size(image)==[100,100,3] and image=im2double(imread(myfilename)) i.e. max(image(:))==1:
if ((image(x,y,:)==0)==[0 0 1]) && ((image(x,y,:)==1)==[1 0 0])
%# interpolate between red and yellow
result(x,y)=10 - 5*image(x,y,2); %# pure red gives 10, pure yellow gives 5
elseif ((image(x,y,:)==0)==[0 0 1]) && ((image(x,y,:)==1)==[0 1 0])
%# interpolate between yellow and green
result(x,y)=5*image(x,y,1); %# pure yellow gives 5, pure green gives 0
elseif
%# ...
end
This solution is not vectorized, but it should get you on the right track for a doable implementation. Of course, if you could avoid saving the data with a multicolor lookup-table as RGB but save the raw values instead, you could save yourself some hassle...

Related

Is it possible to obtain RGB values of a colormap?

I am new to colormaps in MATLAB, but I know their range of values go from 0 to 1 in a 64x3 matrix. If I go to the MATLAB documentation here, I'm referring to the color scales at the very bottom. Is it possible to get the range of RGB color values they seem to represent (in a 64x3 matrix) instead of values from 0 to 1? If not, is there a way to make a color palette similar to them?
Yes, if you want the current colormap, simply call colormap with no input arguments.
current = colormap();
If you want though, you can get the RGB values for any of the colormaps by using the functions that generate the colormaps directly along with an optional input which specifies the number of colors to use.
colors = gray(); % 64 grayscale values
colors = parula(100); % 100 parula colors
colors = jet(10000); % 10000 Jet colors
colors = hsv(10); % 10 colors spanning the HSV colormap
A=colormap('jet');
>> A
A =
0 0 0.5625
0 0 0.6250
0 0 0.6875
0 0 0.7500
0 0 0.8125 % etc, size 64x3
These are the RGB values. It opens an empty figure because colormap internally calls gcf, thus opening a figure.

Matlab: How to color a region

I am wishing to use Matlab to achieve the following goal:
I have a function that takes in two inputs and gives a real number.
function T = SS(x,y)
%%% some calculation %%%
T = returnval
I want the point (x,y) in the x-y plane to be coloured blue if the return value is equal to 1, green if the return value is equal to 0.5 etc.
I don't know how to approach this.
(the calculation is complicated so it is not obvious what's the relationship between x and y with respect to z. Thus can't write an equation to divide the region then colour accordingly.)
Thanks for helping.
If you can group all your T's into a vector, you can use scatter to make the plot. Then just setup your own colormap. Here's an example:
%// Sample data
[x,y]=meshgrid(0:.1:2);
x=x(:);y=y(:);
T=rand(size(x));
%// Define the colormap
MAP=[1 0 0; %// red
0 1 0; %// green
0 0 1]; %// blue
colormap(MAP) %// apply the colormap
scatter(x,y,[],T) %// make the plot
MAP is a matrix, and each row defines a colour. In this case, there are three rows, so elements with values between 0 and 1/3 will be red, elements with values between 1/3 and 2/3 will be green, and elements between 2/3 and 1 will be blue. In general, the range of T values (i.e. max(T)-min(T)) is evenly divided into each colour defined by MAP.

intensity to RGB color convertor, matlab

I have color vector =[0.....1]. I want to convert it to RGB code such that
color_vector =[0.....1] % o for blue, .5 for green and 1 for red
R=255,0,0
G=0,255,0
B=0,0,255
Is there any matlab command(which I could not found) to do it or code.
Another issue is that I want to make my own range for green color (.45-.55) all should be green color.
Basically what you describe is a colormap - but you need to index your color vector for
that.
Incidentally hsv2rgb produces a similar color mapping. But starting from red:
As the hue varies from 0 to 1, the resulting color varies from
red, through yellow, green, cyan, blue and magenta, back to red.
Do you want to linearly interpolate between the colors, for intensity values that are not exactly 0, 0.45-0.55, or 1? If so, you can use real2rgb (on the MATLAB File Exchange), as follows:
I = rand(100, 100); % Input data
cmap = [1 0 0 45; 0 1 0 10; 0 1 0 45; 0 0 1 0]; % Colormap defining the transformation
RGB = real2rgb(I, cmap); % Do the conversion
I use CC and it works well.I can interpolate the colors.

MATLAB: how to set colors in matrix

I have a matrix N*N, with three different values, for example 0, 0.5, 1.
How can i print to the screen an image, which each value represent a different color?
Important: the matrix is a loop so the values may change (i want to print the matrix every iteration).
I tried to use colormap, it worked fine if all the three values were in the matrix,
but when one or two values only remain, the colors were changed.
How I want it to work: matrix with values 0, 0.5, 1 prints to the screen a matrix which each cell contains 0 colored black, 0.5 colored green, 1 colored yellow.
Thanks a lot!
Just create your own colormap that has only three possible values:
a = [1 0.5 0;1 .5 0;0.5 0 1];
b = [1 0 1;1 1 0;0 0 1];
cmap = [0,0,0;0,1,0;1,1,0];
clims = [0 1];
imagesc(a,clims); colormap(cmap);
imagesc(b,clims); colormap(cmap);
a gives:
b gives:
I would try maybe imagsec. Or any other scaling for the colors. Start with gray scale. RGB will be alittle more complex

plot a structure in matlab

I have a structure of known (but variable length) like this-
1 0 1
0 1 1
I want to plot this structure as colored squares- color each 1 as a green square, and 0s as a red square
Something like
[green][red][green]
[red][green][green]
It would be nice to add some optional text on each square.
Also, I have another data structure of same length, with numbers going from 0.0 to 1.0 .. something like
0.99 0.09 1.0
0.09 0.87 1.0
I want to possibly change the intensity of red and green in the above pic depending on how close to 0 or 1 is the corresponding number.
Any suggestions are helpful. Thanks a lot.
You can set the colormap after displaying the matrix as a scaled image:
Z = [1 0 1; 0 1 1];
figure; imagesc(Z);
colormap([1 0 0; 0 1 0]);
axis off; axis image;
Essentially, you want to turn the 2-d structure into a 3-d one, the last dimension being x3, one for each of the RGB colors. Start with this code, and play with it until it does what you want.
map=zeros(2,2,3);
map(:,:,1)=[1 1; 0 0];
map(:,:,2)=[1 0; 1 0];
map(:,:,3)=[0 0; 0 0];
figure;image(map);
Alternatively, you could have a colormap, which would translate the pixel counts to intensity. It's been a while since I've done it, but I can at least point you in the right direction. Run the first command, and look at the colormap. You want to have a gradual changing from Green to Red. Format it how you want it, pass it back in with the last command, and see what you get out.
cmap = colormap;
%You'll want to change cmap to meet your needs
imagesc([.1 .2; .8 .9]);
colormap(cmap);