[英]Scipy interpolation how to resize/resample 3x3 matrix to 5x5?
EDIT: Paul has solved this one below. 编辑:保罗在下面解决了这个问题。 Thanks!
谢谢!
I'm trying to resample (upscale) a 3x3 matrix to 5x5, filling in the intermediate points with either interpolate.interp2d or interpolate.RectBivariateSpline (or whatever works). 我正在尝试将3x3矩阵重新采样(升级)为5x5,使用interpolate.interp2d或interpolate.RectBivariateSpline(或其他任何工作)填充中间点。
If there's a simple, existing function to do this, I'd like to use it, but I haven't found it yet. 如果有一个简单的现有函数来执行此操作,我想使用它,但我还没有找到它。 For example, a function that would work like:
例如,一个函数可以像:
# upscale 2x2 to 4x4
matrixSmall = ([[-1,8],[3,5]])
matrixBig = matrixSmall.resample(4,4,cubic)
So, if I start with a 3x3 matrix / array: 那么,如果我从一个3x3矩阵/数组开始:
0,-2,0
-2,11,-2
0,-2,0
I want to compute a new 5x5 matrix ("I" meaning interpolated value): 我想计算一个新的5x5矩阵(“I”表示内插值):
0, I[1,0], -2, I[3,0], 0
I[0,1], I[1,1], I[2,1], I[3,1], I[4,1]
-2, I[1,2], 11, I[3,2], -2
I[0,3], I[1,3], I[2,3], I[3,3], I[4,3]
0, I[1,4], -2, I[3,4], 0
I've been searching and reading up and trying various different test code, but I haven't quite figured out the correct syntax for what I'm trying to do. 我一直在搜索和阅读并尝试各种不同的测试代码,但我还没有弄清楚我正在尝试做的正确语法。 I'm also not sure if I need to be using meshgrid, mgrid or linspace in certain lines.
我也不确定我是否需要在某些行中使用meshgrid,mgrid或linspace。
EDIT: Fixed and working Thanks to Paul 编辑:修复和工作感谢Paul
import numpy, scipy
from scipy import interpolate
kernelIn = numpy.array([[0,-2,0],
[-2,11,-2],
[0,-2,0]])
inKSize = len(kernelIn)
outKSize = 5
kernelOut = numpy.zeros((outKSize,outKSize),numpy.uint8)
x = numpy.array([0,1,2])
y = numpy.array([0,1,2])
z = kernelIn
xx = numpy.linspace(x.min(),x.max(),outKSize)
yy = numpy.linspace(y.min(),y.max(),outKSize)
newKernel = interpolate.RectBivariateSpline(x,y,z, kx=2,ky=2)
kernelOut = newKernel(xx,yy)
print kernelOut
Only two small problems: 只有两个小问题:
1) Your xx,yy is outside the bounds of x,y (you can extrapolate, but I'm guessing you don't want to.) 1)你的xx,yy超出x,y的范围(你可以推断,但我猜你不想。)
2) Your sample size is too small for a kx and ky of 3 (default). 2)您的样本量太小,kx和ky为3(默认值)。 Lower it to 2 and get a quadratic fit instead of cubic.
将它降低到2并得到二次拟合而不是立方。
import numpy, scipy
from scipy import interpolate
kernelIn = numpy.array([
[0,-2,0],
[-2,11,-2],
[0,-2,0]])
inKSize = len(kernelIn)
outKSize = 5
kernelOut = numpy.zeros((outKSize),numpy.uint8)
x = numpy.array([0,1,2])
y = numpy.array([0,1,2])
z = kernelIn
xx = numpy.linspace(x.min(),x.max(),outKSize)
yy = numpy.linspace(y.min(),y.max(),outKSize)
newKernel = interpolate.RectBivariateSpline(x,y,z, kx=2,ky=2)
kernelOut = newKernel(xx,yy)
print kernelOut
##[[ 0. -1.5 -2. -1.5 0. ]
## [ -1.5 5.4375 7.75 5.4375 -1.5 ]
## [ -2. 7.75 11. 7.75 -2. ]
## [ -1.5 5.4375 7.75 5.4375 -1.5 ]
## [ 0. -1.5 -2. -1.5 0. ]]
If you are using scipy already, I think scipy.ndimage.interpolate.zoom
can do what you need: 如果你已经使用scipy,我认为
scipy.ndimage.interpolate.zoom
可以做你需要的:
import numpy
import scipy.ndimage
a = numpy.array([[0.,-2.,0.], [-2.,11.,-2.], [0.,-2.,0.]])
out = numpy.round(scipy.ndimage.interpolation.zoom(input=a, zoom=(5./3), order = 2),1)
print out
#[[ 0. -1. -2. -1. 0. ]
# [ -1. 1.8 4.5 1.8 -1. ]
# [ -2. 4.5 11. 4.5 -2. ]
# [ -1. 1.8 4.5 1.8 -1. ]
# [ 0. -1. -2. -1. 0. ]]
Here the "zoom factor" is 5./3
because we are going from a 3x3 array to a 5x5 array. 这里“缩放因子”是
5./3
因为我们从3x3阵列变为5x5阵列。 If you read the docs, it says that you can also specify the zoom factor independently for the two axes, which means you can upscale non-square matrices as well. 如果您阅读文档,它表示您还可以为两个轴单独指定缩放系数,这意味着您也可以升级非方形矩阵。 By default, it uses third order spline interpolation, which I am not sure is best.
默认情况下,它使用三阶样条插值,我不确定是最好的。
I tried it on some images and it works nicely. 我在一些图像上尝试过,它运行得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.