首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用interpn?

如何使用interpn?
EN

Stack Overflow用户
提问于 2019-10-11 13:59:35
回答 1查看 1.6K关注 0票数 2

我正在尝试使用interpn (在python中使用Scipy)使用interp3从Matlab中复制结果。然而,我正在努力构建我的论点。我尝试了下面这行:

代码语言:javascript
运行
复制
f = interpn(blur_maps, fx, fy, pyr_level)

其中,模糊贴图是表示七个模糊级别的灰度图像的600 x 800 x 7fxfy是七个贴图的索引。fxfy都是二维数组。pyr_level是一个2d数组,它包含从1到7的值,表示要插值的模糊贴图。

我的问题是,既然我错误地安排了参数,我如何才能以一种有效的方式来安排它们?我试着查找示例,但我没有看到任何类似的东西。下面是我尝试插入的数据的一个示例:

代码语言:javascript
运行
复制
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中,但我不确定如何修复它。有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-12 05:51:09

scipy.interpolate.interpn的文档指出,第一个参数是要插值的数据网格(这只是像素数的整数),第二个参数是数据(blur_maps),第三个参数是(npoints, ndims)形式的插值点。所以你必须做一些类似这样的事情:

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58335066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档