Python scipy.ndimage.interpolate.affine_变换失败
此代码:Python scipy.ndimage.interpolate.affine_变换失败,python,scipy,affinetransform,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 矩阵有什么问
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矩阵的原因是,所讨论的图像是三维的。请注意,第三维是[R,G,B]
,但是scipy.ndimage
不知道非空间维度;它将所有维度视为空间维度。使用2x2矩阵的示例都是2D“灰色”图像
解决方案#1:
affine_transform
将输出坐标o
映射到源(输入)坐标s
为:
s = numpy.dot(matrix,o) + offset
其中矩阵
和偏移
是仿射变换
的参数。在多通道图像的情况下,我们不想变换三维。也就是说,我们需要对应于输出点的源坐标
o == [x, y, z] # column vector
将来
为了实现这一结果,我们需要
matrix = [[ c00, c01, 0],
[ c10, c11, 0],
[ 0, 0, 1]]
offset = [dx, dy, 0] # column vector
解决方案#2:
另一种解决方案是将RGB图像分割为3个通道,分别变换每个通道,然后将它们组合在一起
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慢
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))