我正在尝试使用interpn (在python中使用Scipy)使用interp3从Matlab中复制结果。然而,我正在努力构建我的论点。我尝试了下面这行:
f = interpn(blur_maps, fx, fy, pyr_level)
其中,模糊贴图是表示七个模糊级别的灰度图像的600 x 800 x 7
,fx
和fy
是七个贴图的索引。fx
和fy
都是二维数组。pyr_level
是一个2d数组,它包含从1到7的值,表示要插值的模糊贴图。
我的问题是,既然我错误地安排了参数,我如何才能以一种有效的方式来安排它们?我试着查找示例,但我没有看到任何类似的东西。下面是我尝试插入的数据的一个示例:
import numpy as np
import cv2, math
from scipy.interpolate import interpn
levels = 7
img_path = '/Users/alimahdi/Desktop/i4.jpg'
img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2GRAY)
row, col = img.shape
x_range = np.arange(0, col)
y_range = np.arange(0, row)
fx, fy = np.meshgrid(x_range, y_range)
e = np.exp(np.sqrt(fx ** 2 + fy ** 2))
pyr_level = 7 * (e - np.min(e)) / (np.max(e) - np.min(e))
blur_maps = np.zeros((row, col, levels))
blur_maps[:, :, 0] = img
for i in range(levels - 1):
img = cv2.pyrDown(img)
r, c = img.shape
tmp = img
for j in range(int(math.log(row / r, 2))):
tmp = cv2.pyrUp(tmp)
blur_maps[:, :, i + 1] = tmp
pixelGrid = [np.arange(x) for x in blur_maps.shape]
interpPoints = np.array([fx.flatten(), fy.flatten(), pyr_level.flatten()])
interpValues = interpn(pixelGrid, blur_maps, interpPoints.T)
finalValues = np.reshape(interpValues, fx.shape)
我现在收到以下错误:ValueError: One of the requested xi is out of bounds in dimension 0
我确实知道问题出在interpPoints
中,但我不确定如何修复它。有什么建议吗?
发布于 2019-10-12 05:51:09
scipy.interpolate.interpn
的文档指出,第一个参数是要插值的数据网格(这只是像素数的整数),第二个参数是数据(blur_maps
),第三个参数是(npoints, ndims)
形式的插值点。所以你必须做一些类似这样的事情:
import scipy.interpolate
pixelGrid = [np.arange(x) for x in blur_maps.shape] # create grid of pixel numbers as per the docs
interpPoints = np.array([fx.flatten(), fy.flatten(), pyr_level.flatten()])
# interpolate
interpValues = scipy.interpolate.interpn(pixelGrid, blur_maps, interpPoints.T)
# now reshape the output array to get in the original format you wanted
finalValues = np.reshape(interpValues, fx.shape)
https://stackoverflow.com/questions/58335066
复制相似问题