开发者

Scipy interpolation how to resize/resample 3x3 matrix to 5x5?

开发者 https://www.devze.com 2023-02-22 16:49 出处:网络
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.RectBivariateSp

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).

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:

0,-2,0
-2,11,-2
0,-2,0

I want to compute a new 5x5 matrix ("I" meaning interpolated value):

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.

EDIT: Fixed and working Thanks to 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.)

2) Your sample size is too small for a kx and ky of 3 (default). Lower it to 2 and get a quadratic fit instead of cubic.

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:

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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号