Change matrix values on the basis of a heatmap
9 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
% Draw polygon
pg = drawpolygon();
pos = pg.Position;
% 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];
% remove polygon outline
delete(pg)
2 Comments
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!
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!