Scipy中2D插值的问题 [英] Issues with 2D-Interpolation in Scipy

查看:149
本文介绍了Scipy中2D插值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,数据数据是在失真的网格上采样的,我想将其重新采样到一个未失真的网格.为了对此进行测试,我编写了该程序,具有示例失真和简单的数据功能:

In my application, the data data is sampled on a distorted grid, and I would like to resample it to a nondistorted grid. In order to test this, I wrote this program with examplary distortions and a simple function as data:

from __future__ import division

import numpy as np
import scipy.interpolate as intp
import pylab as plt

# Defining some variables:

quadratic = -3/128
linear = 1/16
pn = np.poly1d([quadratic, linear,0])

pixels_x = 50
pixels_y = 30
frame = np.zeros((pixels_x,pixels_y))

x_width= np.concatenate((np.linspace(8,7.8,57) , np.linspace(7.8,8,pixels_y-57)))

def data(x,y):
    z = y*(np.exp(-(x-5)**2/3) + np.exp(-(x)**2/5) + np.exp(-(x+5)**2))
    return(z)

# Generating grid coordinates

yt = np.arange(380,380+pixels_y*4,4)
xt = np.linspace(-7.8,7.8,pixels_x)
X, Y = np.meshgrid(xt,yt)
Y=Y.T
X=X.T

Y_m = np.zeros((pixels_x,pixels_y))
X_m = np.zeros((pixels_x,pixels_y))

# generating distorted grid coordinates:    

for i in range(pixels_y):
    Y_m[:,i] = Y[:,i] - pn(xt)
    X_m[:,i] = np.linspace(-x_width[i],x_width[i],pixels_x)


# Sample data:
for i in range(pixels_y):
    for j in range(pixels_x):
        frame[j,i] = data(X_m[j,i],Y_m[j,i])


Y_m = Y_m.flatten()
X_m = X_m.flatten()
frame = frame.flatten()
##
Y = Y.flatten()
X = X.flatten()
ipf = intp.interp2d(X_m,Y_m,frame)
interpolated_frame = ipf(xt,yt)

在这一点上,我不得不问:

At this point, I have to questions:

  1. 该代码有效,但出现以下警告:

  1. The code works, but I get the the following warning:

警告:由于B样条系数的数量,无法添加更多的结 已经超过数据点的数量m.可能的原因: s或m太小. (fp> s) kx,ky = 1,1 nx,ny = 54,31 m = 1500 fp = 0.000006 s = 0.000000

Warning: No more knots can be added because the number of B-spline coefficients already exceeds the number of data points m. Probably causes: either s or m too small. (fp>s) kx,ky=1,1 nx,ny=54,31 m=1500 fp=0.000006 s=0.000000

此外,还会出现一些插值伪像,我认为它们与警告有关-你们知道我在做什么错吗?

Also, some interpolation artifacts appear, and I assume that they are related to the warning - Do you guys know what I am doing wrong?

  1. 对于我的实际应用,框架需要大约为500 * 100,但是这样做时,我会遇到MemoryError-除了将框架分成几部分之外,我还能做些什么来帮助您吗?

谢谢!

推荐答案

此问题很可能与 interp2d . 文档提到他们使用 s = 0.0 的欺骗因素,并且如果要进一步控制<需要.相关的 docs 提到,应该在(m-sqrt(2 * m),m + sqrt(2 * m))之间找到 s ,其中 m 是用于构造样条线的点数.我有一个类似的问题,发现直接使用 bisplrep bisplev 可以解决,其中 s 仅是可选的.

This problem is most likely related to the usage of bisplrep and bisplev within interp2d. The docs mention that they use a smooting factor of s=0.0 and that bisplrep and bisplev should be used directly if more control over s is needed. The related docs mention that s should be found between (m-sqrt(2*m),m+sqrt(2*m)) where m is the number of points used to construct the splines. I had a similar problem and found it solved when using bisplrep and bisplev directly, where s is only optional.

这篇关于Scipy中2D插值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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