插值二维矩阵数据 [英] Interpolating 2D Matrix Data
问题描述
我有一个二维矩阵,其元素表示可以进行颜色映射并表示为图像的数据。我想插入它们,但是我在一些我无法解释的边界上得到奇怪的行为。
I have a two-dimensional matrix whose elements represent data that can be colormapped and represented as an image. I want to interpolate them, but I get strange behavior at some of the boundaries that I can't explain.
这是原始图像,经过3次迭代后的图像插值程序,以及10次插值迭代后的图像。
Here is the original image, the image after 3 iterations of the interpolation routine, and the image after 10 interpolation iterations.
这是我的代码:
close all
ifactor = 0.9; % Interpolation factor
% Cut out the meaningless stuff at the edges
bshift_i = bshift(1:16, 1:50);
[m, n] = size(bshift_i);
% Plot the initial data using colormapping
figure()
imagesc(bshift_i, [7.5 10.5])
colorbar()
% Main processing loop
for ii = 1:10
% Every iteration, we generate a grid that has the same size as the
% existing data, and another one that whose axis step sizes are
% (ifactor) times smaller than the existing axes.
[b_x, b_y] = meshgrid(1:ifactor^(ii - 1):n, 1:ifactor^(ii - 1):m);
[b_xi, b_yi] = meshgrid(1:ifactor^ii:n, 1:ifactor^ii:m);
% Interpolate our data and reassign to bshift_i so that we can use it
% in the next iteration
bshift_i = interp2(b_x, b_y, bshift_i, b_xi, b_yi);
end
% Plot the interpolated image
figure()
imagesc(bshift_i, [7.5 10.5])
colorbar()
我主要想知道为什么会出现底部和右边缘的蓝色文物,如果是这样,我该如何解决/避免它们。
I'm mainly wondering why the blue artifacts at the bottom and right edges occur, and if so, how I can work around/avoid them.
推荐答案
问题是如何定义 x
和 y
插值的范围。
让我们看一下 1:ifactor ^(ii - 1):m
:
The problem is how you define the x
and y
ranges for the interpolation.
Let's take a look at 1:ifactor^(ii - 1):m
:
- 对于第一次迭代,您将得到16个值,来自
1
到16
。 - 对于第二次迭代,您将获得17个值,从
1
到15.4
- 对于第三次迭代,您将获得19个值,从
1
到15.58
- For the first iteration, you get 16 values, from
1
to16
. - For the second iteration, you get 17 values, from
1
to15.4
- For the third iteration, you get 19 values, from
1
to15.58
这足以说明问题所在。通过第二次迭代,一切都很好,因为上面的插值边界在值范围内。但是,对于第三次迭代,您的上限现在超出了值范围( 15.58> 15.4
)。
interp2
没有推断,它返回一个 NaN
(第3次迭代后, bshift_i(结束,结束)
是 NaN
)。这些 NaN
被绘制为 0
,因此为蓝色。
An this is enough to illustrate the problem. With the second iteration, everything is fine, because your upper interpolation bound is inside the value range. However, for the third iteration, your upper bound is now outside the value range (15.58 > 15.4
).
interp2
does not extrapolate, it returns a NaN
(after the 3rd iteration, bshift_i(end,end)
is NaN
). These NaN
s get plotted as 0
, hence blue.
要解决此问题,您必须确保 x
和 y
范围始终包含最后一个值。一种方法可以是:
To fix this, you have to ensure that your x
and y
range always include the last value. One way to do this could be:
[b_x, b_y] = meshgrid(linspace(1,n,n./(ifactor^(ii - 1))), ...
linspace(1,m,m./(ifactor^(ii - 1))));
[b_xi, b_yi] = meshgrid(linspace(1,n,n./(ifactor^(ii))), ...
linspace(1,m,m./(ifactor^(ii))));
linspace
始终包含第一个和最后一个元素。但是,第三个输入不增量,但元素数量。这就是为什么你必须调整这种条件以适应你的方法。
linspace
always includes the first and the last element. However, the third input is not the increment, but the number of elements. That's why you'd have to adapt that condition to resemble your approach.
这篇关于插值二维矩阵数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!