Drawing picture from pixels matrix - matlab

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.

Related

Creating a heatmap/colormap in Matlab, by mixing two colors

Hey I have two big matrices 400x400. That I want to create into one heatmap/colormap. Current code:
res = matrix_1*256/2 + matrix_2*256/2;
%res = res -max(max(res));
HeatMap(res)
surf(res,'EdgeColor','none');
view(0,90);
colormap(gray);
colorbar
disp('done');
where the heatmap function everyone can look up. But to give a visualization of the second one it results in:
This however does not let me know which matrix is dominant. But only that both are dominant (white) both are not dominant (dark). I would like to make a plot where I use fused data. E.g. Matrix 1 is nuance of red and Matrix 2 is nuance of green:
rgb = [matrix_1(i,ii), matrix_2(i,ii), 0]
then I want to make a 2D plot using the color that rgb represents. Is this possible ? I have looked at making my own colormap (but you guessed with no good results).
I have found solutions like this (how-to-create-an-interpolated-colormap-or-color-palette-from-two-colors) and create-a-colormap-in-matlab, but I how do I specify a specific colour for every point in a 2D plot?
Like this:
RGB = cat(3, matrix_1, matrix_2, zeros(size(matrix_1)));
imshow(RGB)
Now the chart will be black were neither are dominant, red where matrix_1 is but matrix_2 is not, green where matrix_2 is but matrix_1 is not and yellow where they both dominate.
If you wanted, you could even convert this back to an indexed image and get the colormap that colours it this way using rgb2ind and then create a surface plot using your original res for the heights (note there is no longer a need to scale this) and your new indexed image (ind) to specify the colours which are no longer enitrely goverened by height
res = (matrix_1 + matrix_2)/2;
[ind, map] = rgb2ind(RGB);
surf(res, ind, 'EdgeColor','none');
colormap(map)
colorbar

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

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

heatmap to color image matlab

I have a grayscle image. I can create a heatmap in matlab using:
I = imagesc(I);
it displays the intensity values in color. I want to get a color image using this heatmap. How can I do it?
This is the output of imagesc:
Presumably by "color image" you mean an RGB or truecolor image (an image array with three color channels). And what you're calling a "heatmap" is the colormap that Matlab applies by default to grayscale images (image arrays with only one color channel). A grayscale image plus a color map is referred to as an indexed color image (read more about that here). You can use the ind2rgb function to convert from indexed color to RGB:
IMG_gray = rand(100);
map = colormap; % Get the current colormap
IMG_rgb = ind2rgb(IMG_gray,map);
Note that by default the colormaps in Matlab use only 64 colors rather 256. To get smoother color gradations you can set the colormap manually via map = colormap(jet(256)); or map = colormap(hsv(256));.
If you then want an image file, you can use imwrite, which can take either RGB images or indexed color images with colormaps as input.
Scale the intensity values in your image as imagesc does, and use a colormap (jet is default):
N = 256;
IN = round(N * (I-min(I(:)))/(max(I(:))-min(I(:))));
cmap = jet(N); % see also hot, etc.
IRGB = ind2rgb(IN,cmap);

How define a range of a specific color for plotting in MATLAB

I have a dataset with at least 10 different classes. In each class, i have at least 20 data points. When i use 'scatter plot', my dataset plots with different colors to make different between data points based on their classes. But, I am going to plot my dataset with range of a specific color such as blue, i.e., from dark blue to light blue.
How we can define a range of a specific color for a plot in MATLAB?
Consider the following example:
%# some random xy points with random 1 to 10 classes
data = [rand(100,2) randi([1,10],[100 1])];
%# colormap from dark to light blue: 10-by-3 matrix
clr = linspace(0,1,10)';
clr(:,2:3) = 0;
clr = fliplr(clr);
%# scatter plot
scatter(data(:,1), data(:,2), 10, clr(data(:,3),:))
colormap(clr), colorbar %# fake color legend
So label=1 is mapped to [0,0,0] (dark blue or just black) up to label=10 which is mapped to [0,0,1] (full blue color)
Ok my colormap is not the best, perhaps you should use one of the builtin ones. For example, replace it with:
clr = winter(10); %# or: cool(10)
in the above code