Python affine_transform 不翻译?
我有一个图像(保存为 numpy 数组),我想用变换矩阵对其进行变换。 可以说转换矩阵是:
[[ 0.99729046 -0.07356456 22.57990962]
[ 0.07356456 0.99729046 -12.99879896]
[ 0. 0. 1. ]]
我想通过`scipy.ndimage.interpolation 来做到这一点。
image = affine_transform(image, matrix, mode="reflect")
如果我只是旋转它:
[[ 0.99729046 -0.07356456 0.]
[ 0.07356456 0.99729046 0.]
[ 0. 0. 1.]]
它工作正常,但是当我想旋转它并平移它时,只需平移它,结果看起来像很奇怪。我不知道为什么:S
i have an image (saved as numpy-array) and i want to transform it with a transformation-matrix.
lets say the transformationatrix is:
[[ 0.99729046 -0.07356456 22.57990962]
[ 0.07356456 0.99729046 -12.99879896]
[ 0. 0. 1. ]]
i wanted to do this via `scipy.ndimage.interpolation.
image = affine_transform(image, matrix, mode="reflect")
if i just rotate it:
[[ 0.99729046 -0.07356456 0.]
[ 0.07356456 0.99729046 0.]
[ 0. 0. 1.]]
it works fine, but when i want to rotate it AND translate it, of just translate it, the result lookes quite weird. and i dont know why :S
original image:
http://img408.imageshack.us/img408/9373/eiffel.jpg
transformed image:
http://img861.imageshack.us/img861/8971/blatm.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 ndimage 实际上并不处理彩色图像。它将数组的最后一个 (3,) 维度视为第三个空间维度。
您给出的矩阵实际上只是旋转矩阵,而不是仿射矩阵。文档似乎对此并不清楚。您应该通过
offset
参数传入移位向量。偏移向量还包括“颜色偏移”作为第三元素。这同样适用于上面 Travis Vaught 评论中的
shift
方法。正确的语法是 ndimage.shift(img, (10.0, 10.0, 0.0), mode="wrap") ——当然,除非你想做一些有趣的颜色偏移。原则上,您可以告诉 ndimage 不要沿颜色轴移动图像等,如上所述,但单独对每个颜色元素进行操作应该会更快一些。
I think ndimage doesn't actually handle color images. It treats the last (3,) dimension of your array as a third spatial dimension.
The matrix you give in is actually only the rotation matrix, and not the affine matrix. The documentation seems to not be clear on this. You're supposed to pass in the shift vector via the
offset
parameter. The shift vector also includes a "color shift" as the third element.The same applies to the
shift
method that in Travis Vaught's comment above. The correct syntax isndimage.shift(img, (10.0, 10.0, 0.0), mode="wrap")
--- of course, unless you want to do some funny color shifting.In principle you can tell ndimage not to shift etc. the image along the color axis, as above, but operating on each color element separately should be a bit faster.