discreate heatmap plot over an existing image

5 views (last 30 days)
Ahmed Ashour
Ahmed Ashour on 28 Sep 2021
Answered: Dave B on 29 Sep 2021
Hi guys
I have one quick question, I am taking some measurements in a room and I want to plot heatmap on some points of the room to show how the errors of the readings
the image above is for a sample of the room readings I have, and I want to plot a heatmap only over the points I measured.
I read some tutorials but I still can not get my head around what I am missing.
% Enter data by hand if data from a ThingSpeak channel is not available.
strength = [-90 -90 -90 -90 -40 -20 -22.4 -45 -35 -41 -44 -55 -40 -75 -26]';
% Read data from a ThingSpeak channel.
% Uncomment the next line to read from ThingSpeak.
% strength = thingSpeakRead(CHANNEL_ID, 'ReadKey',READ_API_KEY,'numPoints',15,'fields',FIELD_NUMBER');
X = [10 550 550 10 50 234 393 129 237 328 448 225 344 457 477]';
Y = [10 10 410 410 293 210 202 132 130 142 141 272 268 274 200]';
strengthPercent = 2*(strength+100)/100;
picture = imread("office.png");
[height, width, depth] = size(picture);
OverlayImage=[];
F = scatteredInterpolant(Y, X, strengthPercent,'linear');
for i = 1:height-1
for j = 1:width-1
OverlayImage(i,j) = F(i,j);
end
end
alpha = (~isnan(OverlayImage))*0.6;
imshow(picture)
hold on
OverlayImage = imshow( OverlayImage );
caxis auto
colormap( OverlayImage.Parent, jet );
colorbar( OverlayImage.Parent );
set( OverlayImage, 'AlphaData', alpha );
in the above tutorial I replaced the generated points with something like this
OverlayImage = [1 0.2 3; 1 2 3; 0 0.1 0.2]
but it did not work

Accepted Answer

Dave B
Dave B on 29 Sep 2021
The difference here (I'm guessing) is that your overlay has a small number of pixels, but the overlay in the example has a number of pixels that matches the image. You could use interp2 with the nearest option to 'upsample' overlay image to match the size of the picture, but it'll be kind of a pain.
Because image can take in x/y values for the limits of the image there's an easier answer.
Two things to watch out for in the code below:
  • your code sets height and width variables, so make sure to clear width and clear height (or just clear all) before running the code below (as I used the height and width functions).
  • image works with the centers of its squares, so instead of making the overlay range from 0 to width, I make it range from 1/6 to 5/6 of width (that's where the square centers are). Of you have a different shape overlay you can adjust as appropriate.
picture = imread("peppers.png");
imshow(picture)
hold on
w=width(picture);
h=height(picture);
z = [1 .2 3; 1 2 3; 0 .1 .2];
overlay = imagesc([w/6 w-w/6],[h/6 h-h/6], z); % the x/y values are the centers of the first/last box
colormap turbo;
colorbar
set(overlay, 'AlphaData', .5);

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!