Change matrix values on the basis of a heatmap
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Adam Danz
2019-11-20
"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 个评论
Adam Danz
2019-11-22
Glad I could help! It was an interesting task I hadn't done before so I should thank you, too!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!