Unsure why heatmaps cannot be produced.

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

 Réponse acceptée

Walter Roberson
Walter Roberson le 27 Mai 2022

1 vote

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 commentaires

Steven Lord
Steven Lord le 27 Mai 2022
If that does not resolve the problem, please clarify what "does not seem to be working" means.
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the Contact Support link on the Support section of the MathWorks website so we can investigate.
jason.dim
jason.dim le 28 Mai 2022
Thanks for your response. I have purchased both MATLAB and the Image Processing Toolbox. Previously, after running the code it would print out a heatmap over the selected image. Now when I run it, nothing shows up at all, and I don't see any error messages anywhere. However, when I put my cursor over "OverlayImage(i,j) = F(i,j);", it says "variable appears to change size after every loop iteration" and I am not quite sure what that means exactly. Your help is very much appreciated.
before
for i = 1:height-1
insert
OverlayImage = zeros(height width) ;
This will get rid of the warning about changing size.
jason.dim
jason.dim le 29 Mai 2022
Modifié(e) : jason.dim le 29 Mai 2022
Apologies Walter, it seemed as if there was an issue with the actual image I was trying to overlay a heatmap on. Thank you so much for your assistance.
Walter Roberson
Walter Roberson le 29 Mai 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 le 29 Mai 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 29 Mai 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.

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by