# Affine transformation matrix paramters conversion

**URL:** <https://discuss.pytorch.org/t/affine-transformation-matrix-paramters-conversion/19522>\
**Category:** vision\
**Created:** [June 11, 2018, 2:26pm UTC](https://discuss.pytorch.org/t/affine-transformation-matrix-paramters-conversion/19522 "2018-06-11T14:26:17Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 12, 2018, 11:34pm UTC](https://discuss.pytorch.org/t/affine-transformation-matrix-paramters-conversion/19522/2 "2018-06-12T23:34:33Z")

</div>

How are you comparing the results?  
It seems `scipy.ndimage.affine_transform` uses pixel values for the translation part, while `F.affine_grid` seems to want values in the range `[-1, 1]` (which you already provided).

This code tries to rotate and translate a line:

```auto
x = torch.eye(10).view(1, 1, 10, 10)
theta = torch.zeros(1, 2, 3)
angle = np.pi/2.
theta[:, :, :2] = torch.tensor([[np.cos(angle), -1.0*np.sin(angle)],
                                [np.sin(angle), np.cos(angle)]])
theta[:, :, 2] = 0.5

grid = F.affine_grid(theta, x.size())
x_trans = F.grid_sample(x, grid)

plt.imshow(x.squeeze().numpy())
plt.imshow(x_trans.squeeze().numpy())

```

Based on the values of `grid` the operation should work.  
The visualizations however look a bit strange, but this might be due to some interpolation,  
Maybe someone knows this better.

---

_[View the full topic](https://discuss.pytorch.org/t/affine-transformation-matrix-paramters-conversion/19522)._
