python - scipy.ndimage.interpolate.affine_transform 失败

标签 python scipy affinetransform

这段代码:

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/

相关文章:

c++ - 使用单应性绕x/y轴旋转

python - 使用 Jupyter 笔记本将具有多个工作表的 Excel 文件转换为多个 csv 文件

Python/plotly : How to print y-trend values from LOWESS line?

Python 最小二乘自然样条

python - Python和Matlab上仿射变换的不同结果

c++ - 将找到的变换矩阵推广到整个图像

c# - Java 和 C# 和 Python 一样是 "customizable"吗?

python - 什么 pandas 操作可以帮助我进行 groupby 和按列组合聚合?

python - 我可以将目标函数和导数函数作为一个函数传递给 scipy.optimize.minimize 吗?

python - scipy.interpolate.Rbf() 的插值不准确