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
Related
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
just want to check if it is possible to make a custom colormap with only 3 colors? (there is no need for gradient).
Example: Data ranges from 0-100,
so 0-33 is one color,
34-67 is another color,
and 68-100 is another color.
Just use a colormap with three rows. Each row defines a color in terms of R, G, B components.
A = randi(100,16,16); %// example data
imagesc(A) %// display matrix as image
colormap([1 0 0; 0 1 0; 0 0 1]) %// apply colormap
colorbar %// show color bar
This defines uniformly spaced thresholds between colors. If you need more control you need to have more than three rows, with some of the colors repeated. For example,
colormap([1 0 0; 1 0 0; 0 1 0; 0 0 1]) %// apply colormap
will define a 50% threshold for first color, 75% for second and 100% for third.
Take this example:
% some matrix with integer values in the range [0,100]
Z = peaks;
Z(:) = round((Z(:)-min(Z(:))) ./ range(Z(:))*100);
% show as image (with scaled color mapping)
image(Z, 'CDataMapping','scaled')
caxis([0 100]) % set axes CLim property
colormap(eye(3)) % set figure Colormap property
colorbar % show colorbar
Note that the colors are scaled to the range [0 100], that range is mapped to the current figure's colormap (which we set to only three colors).
Follow this example: How to create a custom colormap programmatically? but instead of R = linspace(0,t(1),50)' you would use R = ones(50,1)*t(1)
or even simpler:
if colour 1 is t1 = [r1, g1, b1] etc then
map(1:34, :) = repmat(t1, 33, 1)
map(35:68, :) = repmat(t2, (67-34), 1)
etc...
OR
map(1:34, :) = bsxfun(#times, t, ones(33,3))
etc...
Check my answer here
You can use that code and decide to interpolate between values or not, its just 2 lines of the code.
The result image shown in the original post for a GYR cutom colormap.
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.
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);
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...