Search code examples
pythonscipyaffinetransform

Python affine_transform does not translate?


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


Solution

  • 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.