Unsure why heatmaps cannot be produced.

2 views (last 30 days)
Below is my code that produces a heatmap with the 10 signal values inserted, along with the 10 points selected once an image is loaded for getpts. I had written this during my trial version of Matlab using help from the internet as Matlab is very new to me. I have now purchased a Matlab license and it does not seem to be working, I have not changed a thing. I am not sure what is wrong.
clc; clear;
%% Insert signal values from excel file %%
strength = [-60 -98 -99 -90 -84 -87 -87 -95 -94 -120]';
%% Select image and plot points, then press enter to produce heatmap %%
[path,~]=imgetfile(); % Lets user select image
Im=imread(path);
Im=imshow(Im);
[xi,yi] = getpts; % Lets user select points
close()
x = round(xi); % Rounds off co-ordinates of selected points to produce whole numbers
y = round(yi);
picture = imread(path); % Gathers image & co-ordinates to produce heatmap
[height,width,depth] = size(picture);
OverlayImage=[];
F = scatteredInterpolant(y, x, strength,'linear');
for i = 1:height-1
for j = 1:width-1
OverlayImage(i,j) = F(i,j);
end
end
alpha = (~isnan(OverlayImage))*0.5;
imshow(picture);
hold on
OverlayImage = imshow(OverlayImage);
caxis auto
colormap(OverlayImage.Parent, jet);
colorbar(OverlayImage.Parent);
set(OverlayImage, 'AlphaData', alpha);

Accepted Answer

Walter Roberson
Walter Roberson on 27 May 2022
imgetfile() is part of the Image Processing Toolbox. The code would fail if you do not have that toolbox installed and licensed. You can replace that code with uigetfile()
  6 Comments
Walter Roberson
Walter Roberson on 29 May 2022
You did not say anything about getting stuck before. You said that the code runs without error but produces no output, and that you saw a warning message when you hovered over that line. Do you still see a warning message when you hover over the line?
jason.dim
jason.dim on 29 May 2022
Apologies, I had forgotten to mention it. It did not show up as an error initially, I had only noticed that it got stuck on that line after pressing pause after I had run it. But it is all good now, thank you.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 May 2022
It might be something like this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
% Insert signal values from excel file %%
strength = [-60 -98 -99 -90 -84 -87 -87 -95 -94 -120]';
% Select image and plot points, then press enter to produce heatmap %%
[fileName,~]=imgetfile(); % Lets user select image
picture = imread(fileName);
imshow(picture);
impixelinfo;
axis('on', 'image')
title('Type Enter when done')
message = sprintf('Draw %d points', length(strength));
uiwait(helpdlg(message))
[xi,yi] = getpts; % Lets user select points
hold on;
plot(xi, yi, 'r.', 'MarkerSize', 30)
% close()
x = round(xi); % Rounds off co-ordinates of selected points to produce whole numbers
y = round(yi);
% Gathers image & co-ordinates to produce heatmap
[rows, columns,depth] = size(picture);
OverlayImage=[];
F = scatteredInterpolant(y, x, strength,'linear');
% The above line creates an interpolant that fits a surface of the form v = F(x,y).
% Vectors x and y specify the (x,y) coordinates of the sample points.
% v is a vector that contains the sample values associated with the points (x,y).
% Get a grid of points at every pixel location in the RGB image.
[xGrid, yGrid] = meshgrid(1:columns, 1:rows);
xq = xGrid(:);
yq = yGrid(:);
% Evaluate the interpolant at query locations (xq,yq).
vq = F(xq, yq);
OverlayImage = reshape(vq, rows, columns);
alpha = (~isnan(OverlayImage))*0.5;
imshow(picture);
hold on
OverlayImage = imshow(OverlayImage);
caxis auto
colormap(OverlayImage.Parent, jet);
colorbar(OverlayImage.Parent);
set(OverlayImage, 'AlphaData', alpha);
but check out my attached demo for a better program.

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!