Interpolating 2 numpy arrays

Is there a numpy or scipy or python function to interpolate between two numpy 2D arrays? I have two 2D numpy arrays, and I want to apply the changes to the first numpy array to make it look like a second 2D array. The limitation is that I want the changes to be smooth. for example, let arrays:

A
[[1 1 1
  1 1 1
  1 1 1]]

and

B
[[34  100 15
  62  17  87
  17  34  60]]

To make A look like B, I could add 33 to the first grid cell A, etc. However, to make the changes smoother, I plan to calculate the average using a 2x2 window in the array B, and then apply the resulting changes to the array A. Is there a built-in numpy or scipy method for this, or follow this approach without using for a loop.

+4
2

convolve2d:

import numpy as np
from scipy import signal

B = np.array([[34, 100, 15],
              [62,  17, 87],
              [17,  34, 60]])
kernel = np.full((2, 2), .25)
smoothed = signal.convolve2d(B, kernel)
# [[  8.5   33.5   28.75   3.75]
#  [ 24.    53.25  54.75  25.5 ]
#  [ 19.75  32.5   49.5   36.75]
#  [  4.25  12.75  23.5   15.  ]]

, 2x2, .

, 3x3 (, np.full((3, 3), 1/9)) mode='same' convolve2d B , "". , , .

A B, , , : A = .2 * A + .8 * smoothed.

+1

/ Kalman. A, , B, . A, B, - . - A B, . :

import numpy as np

# Make a matrix of the distances between points in an array
def dist(M):
    nx = M.shape[0]
    ny = M.shape[1]
    x = np.ravel(np.tile(np.arange(nx),(ny,1))).reshape((nx*ny,1))
    y = np.ravel(np.tile(np.arange(ny),(nx,1))).reshape((nx*ny,1))
    n,m = np.meshgrid(x,y)
    d = np.sqrt((n-n.T)**2+(m-m.T)**2)
    return d

# Turn a distance matrix into a covariance matrix. Here is a linear covariance matrix.
def covariance(d,scaling_factor):
    c = (-d/np.amax(d) + 1)*scaling_factor
    return c

A = np.array([[1,1,1],[1,1,1],[1,1,1]]) # background state
B = np.array([[34,100,15],[62,17,87],[17,34,60]]) # observations

x = np.ravel(A).reshape((9,1)) # vector representation
y = np.ravel(B).reshape((9,1)) # vector representation

P_a = np.eye(9)*50 # background error covariance matrix (set to diagonal here)
P_b = covariance(dist(B),2) # observation error covariance matrix (set to a function of distance here)

# Compute the Kalman gain matrix
K = P_a.dot(np.linalg.inv(P_a+P_b))

x_new = x + K.dot(y-x)
A_new = x_new.reshape(A.shape)

print(A)
print(B)
print(A_new)

, . , (A) (B). . , , . Kalman .

, :

[[ 27.92920141  90.65490699   7.17920141]
 [ 55.92920141   7.65490699  79.17920141]
 [ 10.92920141  24.65490699  52.17920141]]
+2

Source: https://habr.com/ru/post/1656886/


All Articles