在matlab中,如何使用imagesc在2D热图中“平滑”像素 [英] In matlab, how to 'smooth' pixels in 2D heatmap using imagesc

查看:5471
本文介绍了在matlab中,如何使用imagesc在2D热图中“平滑”像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MATLAB,我有一个矩阵(数据),我正在使用 imagesc(data)进行绘图以生成热图:

Using MATLAB, I have a matrix (data) and am plotting using imagesc(data) to produce a heatmap:

data = [1 1 1 1 1 1 1 1 1 1; 1 1.04 1.04 1.04 1.03 1 1.01 1.01 1.03 1.01; 1.36 1.3 1.25 1.2 1.15 1.1 1.2 1.13 1.07 1.11; 3.65 3.16 2.94 2.68 2.39 2.22 2.17 1.95 1.79 1.81; 5.91 5.75 5.47 5.3 4.98 4.79 4.62 4.55 4.38 4.19; 6 6 5.99 5.83 5.49 5.33 5.14 4.94 4.77 4.74];
imagesc(data)

有没有办法'平滑'像素以产生更像这样的东西:

Is there a way to 'smooth' the pixels in order to produce something more like this:

推荐答案

interp2 可能会在这里使用。使用数据作为关键点,然后创建一个更精细的点网格,跨越相同的宽度和高度,并插入关键点之间。

interp2 may be of use here. Use the data as key points, then create a finer grid of points that span the same width and height and interpolate in between the key points.

这样的事情:

%// Define your data
data = [1 1 1 1 1 1 1 1 1 1; 1 1.04 1.04 1.04 1.03 1 1.01 1.01 1.03 1.01; 1.36 1.3 1.25 1.2 1.15 1.1 1.2 1.13 1.07 1.11; 3.65 3.16 2.94 2.68 2.39 2.22 2.17 1.95 1.79 1.81; 5.91 5.75 5.47 5.3 4.98 4.79 4.62 4.55 4.38 4.19; 6 6 5.99 5.83 5.49 5.33 5.14 4.94 4.77 4.74];    

%// Define integer grid of coordinates for the above data
[X,Y] = meshgrid(1:size(data,2), 1:size(data,1));

%// Define a finer grid of points
[X2,Y2] = meshgrid(1:0.01:size(data,2), 1:0.01:size(data,1));

%// Interpolate the data and show the output
outData = interp2(X, Y, data, X2, Y2, 'linear');
imagesc(outData);

%// Cosmetic changes for the axes
set(gca, 'XTick', linspace(1,size(X2,2),size(X,2))); 
set(gca, 'YTick', linspace(1,size(X2,1),size(X,1)));
set(gca, 'XTickLabel', 1:size(X,2));
set(gca, 'YTickLabel', 1:size(X,1));

%// Add colour bar
colorbar;

底部的代码是必需的,因为定义更精细的网格最终会增加图像的大小。我需要重新标记轴回到原始尺寸。

The code that's at the bottom is required because defining the finer grid ultimately increases the size of the image. I need to relabel the axes to go back to the original size.

我们得到这个:

我正在使用MATLAB R2014a,默认的颜色图是jet。你正在使用R2014b +,默认的颜色图是parula。你不会得到和我一样的颜色分布,但你会得到你想要的光滑度。

I'm using MATLAB R2014a, and the default colour map is jet. You're using R2014b+ and the default colour map is parula. You won't get the same colour distribution as me, but you will get the smoothness you desire.

这篇关于在matlab中,如何使用imagesc在2D热图中“平滑”像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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