MATLAB-将图像素化并使其成为热图 [英] MATLAB - Pixelize a plot and make it into a heatmap

查看:605
本文介绍了MATLAB-将图像素化并使其成为热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有x和y坐标的矩阵以及每个数据点的温度值.当我在散点图中绘制该图时,某些数据点将使其他数据点模糊,因此,该图将无法真实表示温度在我的数据集中的变化.

I have a matrix with x and y coordinates as well as the temperature values for each of my data points. When I plot this in a scatter plot, some of the data points will obscure others and therefore, the plot will not give a true representation of how the temperature varies in my data set.

要解决此问题,我想降低图形的分辨率,并创建代表像素区域内所有数据点平均温度的像素.考虑问题的另一种方法是,我需要在当前图上放置一个网格,并对网格的每个部分内的值求平均值.

To fix this, I would like to decrease the resolution of my graph and create pixels which represent the average temperature for all data points within the area of the pixel. Another way to think about the problem that I need to put a grid over the current plot and average the values within each segment of the grid.

我找到了此线程-生成热图在MatPlotLib中使用分散数据集-展示了如何使用python达到所需的最终结果.但是,我当前的代码在MATLAB中,即使我尝试了不同的建议(如热图,contourf和imagesc),也无法获得所需的结果.

I have found this thread - Generate a heatmap in MatPlotLib using a scatter data set - which shows how to use python to achieve the end result that I want. However, my current code is in MATLAB and even though I have tried different suggestions such as heatmap, contourf and imagesc, I can't get the result I want.

推荐答案

您可以使用

You can "reduce the resolution" of your data using accumarray, where you specify which output "bin" each point should go in and specify that you wish to take a mean over all points in that bin.

一些示例数据:

% make points that overlap a lot
n = 10000
% NOTE: your points do not need to be sorted.
% I only sorted so we can visually see if the code worked,
% see the below plot
Xs = sort(rand(n, 1));
Ys = rand(n, 1);
temps = sort(rand(n, 1));

% plot
colormap("hot")
scatter(Xs, Ys, 8, temps)

(为了得到上面的条纹图案,我仅按Xstemps进行了排序,以便我们可以直观地验证降低的分辨率"是否有效)

(I only sorted by Xs and temps in order to get the stripy pattern above so that we can visually verify if the "reduced resolution" worked)

现在,假设我想通过在X和Y方向上每0.05个单位仅获得一个点(即该正方形中所有点的平均值)来降低数据的分辨率(因此,由于我的XY从0变为1,我总共将获得20 * 20积分.

Now, suppose I want to decrease the resolution of my data by getting just one point per 0.05 units in the X and Y direction, being the average of all points in that square (so since my X and Y go from 0 to 1, I'll get 20*20 points total).

% group into bins of 0.05
binsize = 0.05;

% create the bins
xbins = 0:binsize:1;
ybins = 0:binsize:1;

我使用 histc 来确定哪个bin每个X和Y都在(请注意-在这种情况下,由于垃圾箱是规则的,所以我也可以做idxx = floor((Xs - xbins(1))/binsize) + 1)

% work out which bin each X and Y is in (idxx, idxy)
[nx, idxx] = histc(Xs, xbins);
[ny, idxy] = histc(Ys, ybins);

然后,我使用 accumarray 来表示平均值每个仓中temps的数量:

Then I use accumarray to do a mean of temps within each bin:

% calculate mean in each direction
out = accumarray([idxy idxx], temps', [], @mean);

(注意-这意味着temps(i)中的点属于(输出矩阵的)像素"在行idxy(1)idxx(1)上.我做了[idxy idxx]而不是[idxx idxy],所以得到的矩阵有Y ==行和X ==列))

(Note - this means that the point in temps(i) belongs to the "pixel" (of our output matrix) at row idxy(1) column idxx(1). I did [idxy idxx] as opposed to [idxx idxy] so that the resulting matrix has Y == rows and X == columns))

您可以这样绘制:

% PLOT
imagesc(xbins, ybins, out)
set(gca, 'YDir', 'normal') % flip Y axis back to normal

或者作为这样的散点图(我将每个点都绘制在像素"的中点处,并在上面绘制原始数据点以进行比较):

Or as a scatter plot like this (I plot each point in the midpoint of the 'pixel', and drew the original data points on too for comparison):

xx = xbins(1:(end - 1)) + binsize/2;
yy = ybins(1:(end - 1)) + binsize/2;
[xx, yy] = meshgrid(xx, yy);
scatter(Xs, Ys, 2, temps);
hold on;
scatter(xx(:), yy(:), 20, out(:));

这篇关于MATLAB-将图像素化并使其成为热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆