intensity to RGB color convertor, matlab - 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.

Related

how to get good color separation for a few Matlab plot traces?

I need very distinct separation between several plot() traces. several means perhaps 2 or 3 or 5 time history traces or XY plots.
prism is nice with 7 distinctly separate colors but its yellow is hardly visible on the default white background.
Here's an example with default colors. They are quite nice but still somewhat muted pastels. I'd prefer brighter colors than default:
figure(1)
clf
x=1:100;
for i = 1:7
y=i*log(x);
plot(x,y, 'Linewidth', 5);
legendStr{i}=sprintf('line %i',i);
hold on
end
grid on
legend(legendStr,'location','best')
how can I use prism or some other colormap without bright yellow? is there a better approach for plotting a few separate traces in the same figure with really clear color separation?
The prism palette has 6 unique colors, not 7. cm=prism(6) returns these 6 colors. The 3rd one is the yellow, we can remove it with cm(3,:)=[].
We can now set the default color order for plots using the ColorOrder property of the axes object (see here):
cm = prism(6);
cm(3,:) = [];
set(gca,'ColorOrder',cm);
Now, when plotting to this axes object, those 5 colors will be cycled through (remember to set hold on so the axes properties are not reset).
To change the colors for all subsequent plots in the current MATLAB session, change the default color order:
set(groot,'defaultAxesColorOrder',cm)
Use colormap and rgb
rgb stands for Red Green Blue, just set how many percent(proportion) you want each of those main colors in your final mixed color
rgb = [0 0 0] ---> 0%Red + 0%Green + 0%Blue = Black
rgb = [1 0 0] ---> 100%Red + 0%Green + 0%Blue = Red
I use the scale [0, 1], here 1 means 100% and 0 means 0%, you can set any value between 0 and 1
The code is as follow
% Set the color you want using rgb
map = [0 0 0 %---> 1st color
1 0 0 %---> 2nd color
0 1 0 %---> 3rd color
0 0 1 %---> 4th color
1 0.5 1 %---> 5th color
0.2 0.5 0.75 %---> 6th color
0.5 0.3 0.8]; %---> 7th color
x=1:100;
y = zeros(100, 7);
plt = zeros(1,7);
for i = 1:7
y(:,i)=i*log(x);
% For each iteration use different map color, like map(1, :)-->[0 0 0]
plt(i) = plot(x,y(:, i), 'Color',colormap(map(i, :)), 'Linewidth', 5);
hold on
end
legend(plt,{'1st Color','2nd Color', '3rd Color', '4th Color', ...
'5th Color', '6th Color', '7th Color'});
set(gca,'FontSize',20)
xlabel('x-axis','color', 'red', 'fontSize', 25)
ylabel('y-axis', 'color','red', 'fontSize', 25)
Graph

Matlab: Explicitly specifying pie graph slice color

I'm creating a pie graph.
pie([a,b,c,d]);
Is it possible to explicitly change the color of the individual slices?
For example; if I wanted the slices for a and b to always be green and c and d to always be blue, regardless of their size, how would I do that? It seems to me that a color map shades using the size of the slice not necessarily the order in which it was given to the pie function.
The colors of the pie are determined by the axis colormap. So define a matrix with as many rows as the number of pie wedges, and use that as colormap. The first color refers to the first value (a), etc.
For example:
pie([3 2 4 1])
colormap([1 0 0; %// red
0 1 0; %// green
0 0 1; %// blue
.5 .5 .5]) %// grey

How show two images in a figure

I need to show two 3D images using one figure, I used alpha( 0.5) but i have problem with colormap
This is my code:
%%%%%%%%%%%%%%%%%%%%%%%%%%% liver 1 %%%%%%%%%%%%%%%%%%%%%%
liver_surf1=isosurface(nii.img(:,end:-1:1,:),0.05);
V=liver_surf1.vertices;
F=liver_surf1.faces;
t=trisurf(F,V(:,1),V(:,2),V(:,3),'facealpha',0.5,'FaceColor',colormap([1 0 0])); % red color
shading flat;
hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%% liver 2 %%%%%%%%%%%%%%%%%%%%%%
liver_surf=isosurface(nii.img(:,end:-1:1,end:-1:1),0.05);
V2=liver_surf.vertices;
F2=liver_surf.faces;
t2 = trisurf(F2,V2(:,1),V2(:,2),V2(:,3),'facealpha',0.5,'FaceColor',colormap([0 1 0])); % green color
and this is result :
Then problem is that both images colors change to green, if i run separately is ok but i cannot combine both images together. so after i add "shading flat" it becomes like this :
how can i have red and green color together ?
thanks
these are two different liver which should show in an image with transparency because they have intersection.
I'm surprised that code even works (what version of MATLAB do you have?), I would expect trisurf to error. Anyway, colormap is a function that sets the colormap of the image, not of the individual surfaces. You set the colormap to green, all the things will be green.
What you want is to set the colordata, C, to a single value of the same size of your z, then adjust the colormap to give you the right colors. Here's an easy way:
trisurf(F,V(:,1),V(:,2),V(:,3),zeros(size(V(:,3)))); % Cdata = 0
trisurf(F2,V2(:,1),V2(:,2),V2(:,3),,ones(size(V2(:,3)))); %Cdata = 1
colormap([1 0 0; 0 1 0]);
This basically makes two surfaces, one with all color values at zero, the other with all color values at one. The colormap call changes the colormap so it only has two values red (zero) and green (one). (Should also work if you add facealpha etc).

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

Matlab - attach scalar value to RGB colours

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...