How show two images in a figure - matlab

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

Related

Matlab: How to set color of legend in a scatter plot where each data point gets a different color?

Here is the sample code:
x = rand(100,1);
y = rand(100,1);
score = rand(100,1);
figure;
colormap(flipud(bone));
caxis([0 1])
axis([0 1 0 1])
scatter(x,y,50,score,'+','LineWidth',2);
legend('scores');
I'm using the reversed 'bone' colormap which assigns pure white to score value 0 and pure black to score value 1. However, the legend seems to be automatically assigned the score 0, and so if you run the code the legend color is pure white and not visible.
Is there any ways to fix that? Thanks.
If you only want to plot a black + without showing the color range of the data (as with color bar) you can create a dummy legend for that. Here is how you do that:
% plot some dummy data for the legend:
scatter(nan,nan,[],1,'+','LineWidth',2)
hold on
% plot your data:
scatter(x,y,50,score,'+','LineWidth',2);
hold off
% add the legend only for the first (dummy) data:
leg = legend('scores');
The result:
What you are looking for is a colorbar. This shows a bar with color gradient for all values in the colormap used.
Replace legend('scores'); line with colorbar.
P.S. legend is meant for identifying multiple plots in the same figure.

Drawing picture from pixels matrix

I want to draw an image from a matrix that contains color values. It look like this:
TEMP = zeros(100,100);
for i=1:100
for j=1:100
if (...)
TEMP(i,j)=0;
elseif (...)
TEMP(i,j)=56;
elseif (...)
TEMP(i,j)=32;
end
end
end
image(TEMP)
But it only draws using this colors:
These colors have values from 0 to 66, step=2
How can I draw RGB colors using matrix? Especially I need grey color
Change your colormap to gray to get a BNW image:
TEMP = rand(100)*255;
image(TEMP)
colormap gray
If your colours are inverted, use image(-TEMP).
A 2D array cannot define colors on its own, one need to associate a colormap with it. Your default colormap is jet (from blue to red) and you can change it to gray for instance but with this kind of display you will always use only a subset if colors.
If you want to use any RGB color, then you need an RGB array, i.e. an array of dimensions n-by-m-by-3. The sub-array A(:,:,1) will code for red, A(:,:,2) for green and A(:,:,3) for blue. With this kind of array the command::
image(A);
should give you a true RGB display.

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

Image classification - coloring features with a colormap?

I have an image with some features/regions in it (balls in the above example). I want to color each ball with a different color based on its properties. For example, that might be its diameter in pixels.
While I'm done on the feature recognition side, I'm stuck when it comes to showing results. Right now I'm doing:
my_image = imread(...);
//ball recognition and other stuff
for i = 1:number_of_balls
ball_diameter(i) = ... //I calculate the diameter of the i-th ball
ball_indices = ... //I get the linear indices of the i-th ball
//ball coloring
my_image(ball_indices) = 255; //color the red channel
my_image(ball_indices + R*C) = 0; //color the blue channel
my_image(ball_indices + 2*R*C) = 0; //color the green channel
end
figure
imshow(my_image)
colormap jet(100) //I want 100 different classes
colorbar
caxis([0, 50]) //I assume all balls have a diameter < 50
In the above code I'm tinting all balls red, which is definitely not what I'm looking for. The issue is that, even if I know ball_diameter(i), I do not know which colormap class that ball will get in. In other words, I would need something like:
for i = 1:number_of_balls
// ...
if ball_diameter *belongs to class k*
my_image(ball_indices) = jet(k, 1);
my_image(ball_indices + R*C) = jet(k,2);
my_image(ball_indices + 2*R*C) = jet(k,3);
end
end
How to, and mostly, is there any other more logical way?
You can separate the assignment of pixels to classes from their coloring for display: you can use my_image as a 2D R-by-C labeling matrix: that is each object (ball) is assigned a different index from 1 to 100 (in case you have 100 objects in the image). Now when you want to display the result you can either ask the figure to map indexes to colors for you, using colormap or explicitly create a colored image using ind2rgb.
For example
%// create the index/labeling matrix
my_image = zeros( R, C );
for ii = 1:number_of_balls
my_image( ball_indices ) = ii; %// assign index to pixels and not atual colors
end
%// color using figure
figure;
imagesc( my_image );axis image;
colormap rand(100,3); %// map indexes to colors using random mapping
%//explicitly create a color image using ind2rgb
my_color_image = ind2rgb( my_image, rand(100,3) );
figure;
imshow( my_color_image ); % display the color image
Notes:
1. IMHO it is preferable to use random color map to display categorizations of pixels, as opposed to jet with which you usually end up with very similar colors to adjacent objects, making it very difficult to visually appreciate the result.
2. IMHO it is more convenient to use label matrix, you can also save it to file as an indexed image (png format) thus visualizing, saving and loading your results simply and efficiently.
A simple way is as follows:
Make a 2D image of the same size as your original.
Set the indices of each "ball" to the diameter or other relevant value
Display with imagesc
Use caxis, colormap, colorbar etc. to adjust the categories dynamically.
For example,
a = randi(200,200); % 200 x 200 image containing values 1 to 200 at random
imagesc(a)
colorbar
The above should show a random color field with the default colormap. The colorbar goes from 1 to 200.
colormap(jet(5))
The colorbar still goes from 1 to 200, but with only 5 colors.
caxis([1 100])
The colorbar now shows the five colors scaled from 1 to 100 (with everything above 100 in the top pot).
If you want to convert your 2D image full of different diameters to a set of discrete labels indicating diameter ranges, an easy way is to use histc with the second output bin being the same size as your input image, set to which bin the diameter value fell into. The second input in this case is the edges of the bins, not the centres.
[n bin] = histc(a,0:20:201);

Plot with variable background color

I'm plotting line charts with the x-axis representing the timeline. Some of the dataranges should be marked as unreliable. For showig this it would be great to change the background color over these x-ranges to some bringt yellow or red. Is there an option of doing that?
I know that could us gscatter instead of gplot for using colors. But then only the data is colored and not the background and I don't get linecharts but point charts. Colored data would be an acceptable alternative, but I need line charts.
It is always possible to make a filled polygon plot. Try,
a = 0:0.1:1;
b = a;
figure;
plot(a,b,'r');
hold on;
x = [0 0 1 1 0];
y = [0 1 1 0 0];
h = fill(x,y,'g');
set(h,'FaceAlpha',0.1); % or alpha(h,0.1); would also work