Change matrix values on the basis of a heatmap

9 views (last 30 days)
Hello,
I have a question about using Matlab. I have a matrix (m x n), which contains temperature values. By using the heatmap function, I want to visualise the temperature plot. Afterwards, I want to to crop the heatmap manually by using a binary mask (createMask, drawpolygon) to extract important parts. The result is that the unimportant parts of the heatmap are black. My question is, if it is possible to set all the values of matrix zero on the basis of the black coloured parts of the heatmap. This means that the values of the unimportant (black) parts should be set zero on the basis of the cropped heatmap. I hope that my problem is understandable.
I have already tried to find some information online, but wasn't successful, yet. Hopefully, someone can help me.

Accepted Answer

Adam Danz
Adam Danz on 20 Nov 2019
"I want to to crop the heatmap manually by using a binary mask (createMask, drawpolygon) to extract important parts."
This won't be possible (as of r2019b). If drawpolygon() is called on a heatmap axis, you'll get the error "Polygon cannot be a child of HeatmapChart." Use imagesc() instead of heatmap.
"My question is, if it is possible to set all the values of matrix zero on the basis of the black coloured parts of the heatmap."
Here's a demo how to create a heatmap using imagesc(); how to define a region of interest using drawpolygon(), and how to set the color of units outside of the polygon to black.
% Create data
fig = figure()
data = randi(100,10,15);
x = 1:size(data,2);
y = 1:size(data,1);
hm = imagesc(x,y,data);
axis equal
191120 095515-Figure 1.png
% Draw polygon
pg = drawpolygon();
pos = pg.Position;
191120 095552-Figure 1.png
% Determine which coodinates are inside the polygon
[allx,ally] = meshgrid(x,y);
[in,on] = inpolygon(allx,ally,pos(:,1),pos(:,2));
% Replace values of units outside of polygon
hm.CData(~(in|on)) = 0;
% add black to the top of whatever colormap you're using
ax = gca();
ax.Colormap(1,:) = [0 0 0];
191120 095617-Figure 1.png
% remove polygon outline
delete(pg)
  2 Comments
Tobias Riewendt
Tobias Riewendt on 22 Nov 2019
Hello Adam, thanks for your detailed description. The use of imagesc() instead of heatmap() seems to be logical after your description. I have tried yout way and it worked very well.
Adam Danz
Adam Danz on 22 Nov 2019
Glad I could help! It was an interesting task I hadn't done before so I should thank you, too!

Sign in to comment.

More Answers (1)

MV
MV on 20 Nov 2019
Edited: MV on 20 Nov 2019
if I understand your problem correct
Matrix(~binaryMask) = 0
should do the job.
It sets every Value to zero where binaryMask is zero.

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!