在 opencv 的图像上应用 RBFInterpolator

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

如何将

RBFInterpolator
应用到opencv加载的图像上?

我想通过定义图像点之间的插值对图像进行非仿射变换。我该怎么做?

python opencv interpolation
1个回答
0
投票
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('results/remap.png', warped_image)
© www.soinside.com 2019 - 2024. All rights reserved.