Change matrix values on the basis of a heatmap

조회 수: 2 (최근 30일)
Tobias Riewendt
Tobias Riewendt 2019년 11월 20일
댓글: Adam Danz 2019년 11월 22일
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.

채택된 답변

Adam Danz
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
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
Tobias Riewendt
Tobias Riewendt 2019년 11월 22일
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 2019년 11월 22일
Glad I could help! It was an interesting task I hadn't done before so I should thank you, too!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

MV
MV 2019년 11월 20일
편집: MV 2019년 11월 20일
if I understand your problem correct
Matrix(~binaryMask) = 0
should do the job.
It sets every Value to zero where binaryMask is zero.
  댓글 수: 1
Tobias Riewendt
Tobias Riewendt 2019년 11월 22일
Hello Manuel, I have tried your idea, but it did not work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by