heatmap to color image matlab - 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);

Related

Combine 2 images as a single planar image

I have two separate gray scale images im1 (Fig1) and im2 (Fig2) each of size 50 by 50 that are displayed here by color coding them. When I combine them together using cat() command and then display the result of concatenated images, they get displayed side by side (Fig3). However, if I create a third image by replicating either the first of the second image and then display the concatenation of the 3 images I get a single image (Fig4). I don't understand how the merging for the RGB (3 dimensions) could be possible whereas for the conversion to grayscale the merging did not happen. How can I get a single image using the two images im1 and im2 merged or overlayed whichever is legally possible and not side by side? Si how do I overlay im1 and im2 to get a single image and display it by color coding?
imgGray = cat(2,im1,im2);
imshow(imgGray)
imgGray = cat(2,im1,im2);
imshow(imgGray)
imagesc(imgGray)
im3=im1;
imgColor = cat(3,im1,im2,im3);
imagesc(imgColor)
You can also just add them to each other (after normalizing them) and have a single colormap represent it all
imagesc(I1+I2);
or if you want to set transparency according to the color and intensity you can add
alpha color
alpha scaled
You can to do it "manually":
Use ind2rgb for converting each Grayscale image to RGB with "color coding".
Compute average of two RGB images for getting "fused image".
Here is a code sample:
% Use cameraman as first image, and resized moon for the second image (just for example).
I1 = imread('cameraman.tif'); % I1 is uint8 Grayscale
I2 = imresize(imread('moon.tif'), size(I1)); % I2 is uint8 Grayscale
% Convert images to RGB using parula color map (you may choose any color map).
J1 = ind2rgb(I1, parula(256)); % J1 is double RGB (three color planes in range [0, 1]).
J2 = ind2rgb(I2, parula(256)); % J2 is double RGB (three color planes in range [0, 1]).
% Fuse J1 and J2 "manually".
alpah = 0.5; % Using alpah=0.5, gives average of J1 and J2.
K = J1*alpah + J2*(1-alpah); %K is is double RGB.
K = im2uint8(K); % Convert K to uint8 (im2uint8 multiplies pixels by 255, and convert to uint8).
%Display result
figure;imshow(K);
Result:

How to display image with self-defined transparency from matrix in matlab?

I would like to display image directly from a matrix (not colormap). For example, I have a 100x100x3 matrix A, at position (i,j), it has color [0.5,1.0,0.8]. Using imshow(A) can directly display the image.
However, the alpha value cannot be specified. If I would like each pixel to have an additional alpha channel, for example [0.5,1.0,0.8,0.2], the imshow method complains.
Is there a way to accomplish this target?
Thank you very much.
You can use the AlphaData property of the Image returned by imshow
img = A(:,:,1:3);
alpha = A(:,:, 4);
i = imshow(img);
i.AlphaData = alpha;

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 gui - How do i generate a colour scale for HSV image?

I am working on a colour scale that is used to differentiate two standards of my image data. I have image data in RGB and i already converted them to HSV. I managed to find the minimum and maximum values of HSV of each image. Now how do i generate a colour scale in HSV space for the images?
If it is your intention to generate the HSV colour bar, you can create that yourself using the hsv colormap command.
The colour map looks like this:
(source: mathworks.com)
You can specify how many total colours N from this colour map as input into the function and the output would be a N x 3 matrix where each row is a floating point ([0,1]) RGB tuple where the first row denotes the left most colour and the last row denotes the right most colour in the above image.
As such, the colour map M generated from hsv would be:
M = hsv(N);
However, as this is essentially a lookup table / colour map, this would be visualized as an image that is 1 pixel wide (N x 1). You probably want to make this wider so perhaps make the colour bar wider, and so something like this:
N = 256; %// Example
height = 50; %// Height of colour bar (in pixels)
M = hsv(N); %// Generate colour map
M = permute(M, [3, 1, 2]); %// Reshape to 1 pixel colour bar that is horizontal
M = repmat(M, [height, 1, 1]); %// Make the colour bar 50 pixels thick
imshow(M); %// Show the image
This is what I get:

using imagesc for segmentation

I have observed that whenever I use the command imagesc(image); the resultant image has some regions that are yellow in colour and the background is red.
Is there any way to segment these regions of the images ? If there is not do they have any similar pattern that can be used for thresholding or is the colour representation meaningless?
I am using Matlab R2012a on Windows.
When you are using imagesc (short for image scale), you are visualizing a matrix by mapping the lowest value of that matrix to one end of a colormap, and the highest value of the matrix to the other end.
By default, MATLAB uses the jet() colormap which is the normal RGB-colorrange. If some parts of your image turns out to be yellow, it means that the elements of the matrix has been some spesific place between the highest and lowest value.
The example below hopefully illustrates this more clearly and shows how you can segment out the "yellow" regions of a matrix (which doesn't really have any color per se)
colorRes = 256;
%# In a jet colormap with size 256, yellow is at placement 159
yellow = 159;
yellowScale = ((yellow/256));
image = repmat(1:colorRes,40,1);
figure(1);clf;
colormap(jet(colorRes))
subplot(2,1,1)
imagesc(image)
title('Original image')
%# Segment out yellow
colorDist = 1/colorRes*5; %# Make scalar higher to include colors close to yellow
imageSegmented = zeros(size(image));
imageSegmented(abs(image/colorRes-yellowScale)< colorDist) = 1;
subplot(2,1,2)
imagesc(imageSegmented)
title('Yellow segmented out')