如何在图像/ndarray上应用 scipy.interpolate.RBFInterpolator ?

问题描述 投票:0回答:1

例如,如何在 opencv 加载的图像上应用

RBFInterpolator
? 我需要使用 numpy 的向量运算来应用插值(速度很快)

我需要通过定义图像点之间的插值对图像进行非仿射变换。

我该怎么做?

python numpy scipy interpolation
1个回答
1
投票
import cv2
import numpy as np
from scipy.interpolate import RBFInterpolator

# Load the image
image = cv2.imread('image.png')
width,height = image.shape[:2]
w=width
h=height

# Your source points and corresponding destination points
src_points = np.array([
    [0, 0],
    [w,h],
    [w,0],
    [0,h],
])

dst_points = np.array([
    [0, 0],
    [w,h],
    [w,0],
    [0,h],
])

# Create the RBF interpolator instance
rbfx = RBFInterpolator(src_points,dst_points[:,0],kernel="thin_plate_spline")
rbfy = RBFInterpolator(src_points,dst_points[:,1],kernel="thin_plate_spline")

# Create a meshgrid to interpolate over the entire image
img_grid = np.mgrid[0:width, 0:height]
grid_x, grid_y = img_grid

# flatten grid so it could be feed into interpolation
flatten=img_grid.reshape(2, -1).T

# Interpolate the displacement using the RBF interpolators
map_x = rbfx(flatten).reshape(width,height).astype(np.float32)
map_y = rbfy(flatten).reshape(width,height).astype(np.float32)
# Apply the remapping to the image using OpenCV
warped_image = cv2.remap(image, map_y, map_x, cv2.INTER_LINEAR)

# Save or display the result
cv2.imwrite('remap.png', warped_image)

上面的代码应该生成与输入图像相同的图像。

改变源点和目标点进行图像变换。

我相信有一种更快的方法来进行插值,但这就是我想出的

© www.soinside.com 2019 - 2024. All rights reserved.