1
votes

I am new to MATLAB Image Processing and currently I have two images - one is a grayscale image of my object and the second is the scaled image generated from MATLAB using imagesc function. I am trying to overlay this scaled image on top of my grayscale image to get a spatial resolution for easier observation. Attached are the two images:

A) Grayscale Image:

grayscale image

B) Scaled Image: scaled image

There were a few difficulties that I encountered. Firstly, the scaled image is not saved in the same pixel dimensions, but I can get around that using the imwrite function:

im = imagesc(ScaledDiff);
imwrite(get(im,'cdata'),'scaleddiff.tif')

However, doing so will result in a loss of colorbar and the colormap. Secondly, even if I manage to shrink the scaled image to the size of the grayscale image, overlaying it is still a challenge. Ideally, I would like to set the transparency (or 'alpha') to 0 for those pixels with < 0.02 in scaled image value.

Any idea on how to do this will be greatly appreciated! Sorry if I was unclear!


UPDATE: Thanks to Rotem, I have managed to overlay the grayscale image and a particular region of my heatmap:

overlay

However, I need to display the colorbar corresponding to the heatmap values, because otherwise the information is lost and the overlay will be useless. How should I do this? Below is a snippet of my code, where ScaledIntDiff contains the values from 0 to 0.25 that is displayed on the heatmap:

Brightfield = imread('gray.jpg'); % read background image
I1 = ind2rgb(gray2ind(Brightfield), gray); % convert indices into RGB scale

scale = 1000;
ScaledIntDiff2 = round(ScaledIntDiff.*scale); 
I2 = ind2rgb(ScaledIntDiff2, jet(256)); % this is for the heatmap

threshold = 0.02;
I2R = I2(:,:,1); I2G = I2(:,:,2); I2B = I2(:,:,3);
I1R = I1(:,:,1); I1G = I1(:,:,2); I1B = I1(:,:,3);
% Replace pixels in I2 with pixels in I1 if the value of ScaledIntDiff of those pixels is below the threshold
I2R(ScaledIntDiff<threshold) = I1R([ScaledIntDiff<threshold]);
I2G(ScaledIntDiff<threshold) = I1G([ScaledIntDiff<threshold]);
I2B(ScaledIntDiff<threshold) = I1B([ScaledIntDiff<threshold]);
I2(:,:,1) = I2R; I2(:,:,2) = I2G; I2(:,:,3) = I2B;

figure
imshow(I2) 

I know that the code above is highly inefficient, so suggestions on how to improve it will be very welcomed. Thank you!

1
First recommendation: Don't use imagesc, create RGB image using ind2rgb. Example: I = imread('cameraman.tif');J = ind2rgb(I, parula(256));imshow(J).Rotem
In case you still need to resize the image, use: imresize.Rotem

1 Answers

1
votes

Check the following:

I = imread('CKbi2Ll.jpg'); %Load input.

%Convert I to true color RGB image (all pixels R=G=B are gray color).
I1 = ind2rgb(I, gray(256));

%Convert I to true color RGB image with color map parula (instead of using imagesc)
I2 = ind2rgb(I, parula(256));

%Set the transparency (or 'alpha') to 0 for those pixels with < 0.02 in scaled image value.
%Instead of setting transparency, replace pixels with values from I1.
threshold = 0.02; %Set threshold to 0.02
I2(I1 < threshold) =  I1(I1 < threshold);

%Blend I1 and I2 into J.
alpha = 0.3; %Take 0.3 from I2 and 0.7 from I1.
J = I2*alpha + I1*(1-alpha);

%Display output
imshow(J);

enter image description here


Adding a colorbar with ticks labels from 0 to 0.25:

  • Set color map to parula - it doesn't affect the displayed image, because image format is true color RGB.
  • Crate array of add 6 ticks from 0 to 250.
  • Create cell array of 6 TickLabels from 0 to 0.25.
  • Add colorbar with Ticks and TickLabels properties created earlier.

Add the following code after imshow(J);:

colormap(parula(256));
TLabels = cellstr(num2str((linspace(0, 0.25, 6))'));
T = linspace(1, 250, 6);
colorbar('Ticks', T', 'TickLabels', TLabels);

enter image description here


imshow('ClmypzU.jpg');
colormap(jet(256));
TLabels = cellstr(num2str((linspace(0, 0.25, 6))'));
T = linspace(1, 250, 6);
colorbar('Ticks', T', 'TickLabels', TLabels);

enter image description here