Python affine_transform 不翻译?

发布于 2025-01-05 07:40:19 字数 815 浏览 1 评论 0原文

我有一个图像(保存为 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

原图: http://img408.imageshack.us/img408/9373/eiffel.jpg

变换后的图像: http://img861.imageshack.us/img861/8971/blatm.jpg

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-12 07:40:19

我认为 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 is ndimage.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文