gpt4 book ai didi

python - scipy.ndimage.interpolate.affine_transform 失败

转载 作者:行者123 更新时间:2023-11-28 16:21:24 25 4
gpt4 key购买 nike

这段代码:

from scipy.ndimage.interpolation import affine_transform
import numpy as np
...
nzoom = 1.2
newimage = affine_transform(self.image, matrix=np.array([[nzoom, 0],[0, nzoom]]))

失败:

RuntimeError: affine matrix has wrong number of rows

矩阵有什么问题?我还尝试了 matrix=[nzoom, nzoom],根据我对文档的阅读,它应该做同样的事情,但它以同样的方式失败。

最佳答案

原始代码不适用于 2x2 矩阵的原因是因为所讨论的图像是 3 维的。请注意,第 3 维是[R,G,B],但是 scipy.ndimage 不知道非空间维度;它将所有维度都视为空间维度。使用 2x2 矩阵的示例都是二维“灰色”图像。

解决方案 #1:

affine_transform 将输出坐标 o 映射到源(输入)坐标 s 为:

s = numpy.dot(matrix,o) + offset

其中 matrixoffsetaffine_transform 的参数。在多 channel 图像的情况下,我们不想变换 3 维。即,我们想要对应于输出点的源坐标

o == [x, y, z]  # column vector

成为

s == [c00*x + c01*y + dx, c10*x + c11*y + dy, z]  # column vector

为了达到我们需要的结果

matrix = [[ c00, c01,  0],
[ c10, c11, 0],
[ 0, 0, 1]]

offset = [dx, dy, 0] # column vector

解决方案#2:

另一种解决方案是将 RGB 图像分成 3 个 channel ,分别对每个 channel 进行变换,然后将它们组合在一起,

r = rgb[..., 0]
g = rgb[..., 1]
b = rgb[..., 2]
matrix = np.array([[c00, c01], [c10, c11]])
offset = [dx dy]
r = affine_transform(r, matrix=matrix, offset=offset)
g = affine_transform(g, matrix=matrix, offset=offset)
b = affine_transform(b, matrix=matrix, offset=offset)
rgb = np.dstack((r, g, b))

我没有为任何一种解决方案计时,但我预计 #2 会比 #1 慢。

关于python - scipy.ndimage.interpolate.affine_transform 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40060786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com