I'm rewriting a Matlab's code in Python Language. In Matlab code I have this function: gt= interp2(I(:,:,i)',xi,yi,'cubic')';
, where I
is a RGB image, xi
and yi
are 2D matrixes with same shape defining the x and y coordinates. After I set gt(isnan(gt))=0;
for the values outside the boundaries. This function runs perfectly on Matlab.
我正在用Python语言重写Matlab的代码。在Matlab代码中,我有这个函数:gt= interp2(I(:, I)',xi,yi,'立方体');在这里我是RGB图像,xi和yi是二维矩阵,具有相同的形状,定义了x和y坐标。当我设置gt(isnan(gt))= 0;对于边界之外的值。这个函数在Matlab中很好地运行。
In Python I wrote the following code: gt=interpolate.interp2d(x,y,img.T,kind='cubic',fill_value=0)
, where x
and y
are the same as the xi
and yi
in Matlab, and img is a gray-scale image. Anyway i get the following exception: "Invalid length for input z for non rectangular grid") ValueError: Invalid length for input z for non rectangular grid"
. What is wrong? Thank you very much.
在Python中,我编写了以下代码:gt=插值。interp2d(x,y,img. t,kind='cubic',fill_value=0),其中x和y与Matlab中的xi和yi相同,img是灰度图像。无论如何,我得到了如下例外:“非矩形网格输入z的长度无效”)ValueError:输入z的长度为非矩形网格的无效长度。是什么错了吗?非常感谢。
1 个解决方案
#1
0
Looking at the documentation you should note a few things:
看看这些文档,你应该注意到以下几点:
-
If x and y represent a regular grid, consider using RectBivariateSpline.
如果x和y表示一个规则网格,考虑使用RectBivariateSpline。
-
x
andy
should be 1D vectors unlike in Matlab and they match with Matlab'sx
andy
and NOT withxi
andyi
. Those come later.x和y应该是一维的,不像在Matlab中,它们与Matlab的x和y匹配,而不是与xi和yi。这些将来的。
-
SciPy will return a function to use to interpolate later. Matlab retuns the interpolated vector immediately.
SciPy将返回一个函数,用于稍后插入。用Matlab对插补矢量进行了快速的调整。
You've used it in Matlab but implicitly assuming that
你在Matlab中已经用过了,但隐含的假设。
[X, Y] = meshgrid(1:size(I,1), 1:size(I,2))
gt= interp2(X, Y, I(:,:,i)',xi,yi,'cubic')';
So it's actually this X
and Y
that you need to pass to interpolation.interp2d and NOT xi
and yi
, they come later.
这是你需要传递给插值的X和Y,而不是xi和yi,它们会在后面出现。
In SciPy you can actually skip the meshgrid
step and just use a normal range but also it outputs a function for later rather than performing the interpolation on the spot (note there might be small errors in my definitions of x
and y
, I'm not that familiar with arange
):
在SciPy中,你可以跳过网格的步骤,只需要使用一个正常的范围,但它也会输出一个函数,而不是在点上执行插值(注意,在我的x和y定义中可能有小错误,我不太熟悉arange):
x = arange(1, img.shape[0]+1)
y = arange(1, img.shape[1]+1)
f = interpolation.interp2d(x, y, img.T)
and then when you want to do the interpolation:
当你想做插值的时候
gt = f(xi, yi)
#1
0
Looking at the documentation you should note a few things:
看看这些文档,你应该注意到以下几点:
-
If x and y represent a regular grid, consider using RectBivariateSpline.
如果x和y表示一个规则网格,考虑使用RectBivariateSpline。
-
x
andy
should be 1D vectors unlike in Matlab and they match with Matlab'sx
andy
and NOT withxi
andyi
. Those come later.x和y应该是一维的,不像在Matlab中,它们与Matlab的x和y匹配,而不是与xi和yi。这些将来的。
-
SciPy will return a function to use to interpolate later. Matlab retuns the interpolated vector immediately.
SciPy将返回一个函数,用于稍后插入。用Matlab对插补矢量进行了快速的调整。
You've used it in Matlab but implicitly assuming that
你在Matlab中已经用过了,但隐含的假设。
[X, Y] = meshgrid(1:size(I,1), 1:size(I,2))
gt= interp2(X, Y, I(:,:,i)',xi,yi,'cubic')';
So it's actually this X
and Y
that you need to pass to interpolation.interp2d and NOT xi
and yi
, they come later.
这是你需要传递给插值的X和Y,而不是xi和yi,它们会在后面出现。
In SciPy you can actually skip the meshgrid
step and just use a normal range but also it outputs a function for later rather than performing the interpolation on the spot (note there might be small errors in my definitions of x
and y
, I'm not that familiar with arange
):
在SciPy中,你可以跳过网格的步骤,只需要使用一个正常的范围,但它也会输出一个函数,而不是在点上执行插值(注意,在我的x和y定义中可能有小错误,我不太熟悉arange):
x = arange(1, img.shape[0]+1)
y = arange(1, img.shape[1]+1)
f = interpolation.interp2d(x, y, img.T)
and then when you want to do the interpolation:
当你想做插值的时候
gt = f(xi, yi)