如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像? [英] How can I use scipy.ndimage.interpolation.affine_transform to rotate an image about its centre?

查看:393
本文介绍了如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

API使我感到困惑,以 scipy.ndimage.interpolation.affine_transform .通过此问题来判断,我不是唯一一个.实际上,我想用affine_transform做更多有趣的事情,而不只是旋转图像,但是旋转对于初学者来说会做的. (是的,我很清楚 ,但是在这里弄清楚如何驱动affine_transform是我的兴趣所在.)

I am perplexed by the API to scipy.ndimage.interpolation.affine_transform. And judging by this issue I'm not the only one. I'm actually wanting to do more interesting things with affine_transform than just rotating an image, but a rotation would do for starters. (And yes I'm well aware of scipy.ndimage.interpolation.rotate, but figuring out how to drive affine_transform is what interests me here).

当我想在像OpenGL这样的系统中做这种事情时,我认为是在计算对中心c应用2x2旋转矩阵R的变换时,因此要考虑点被转换的(p-c)R+c = pR+c-cR,它给出一个c-cR术语,用作转换的翻译组件. 但是,根据上述问题,scipy的affine_transform会先执行" offset ",因此我们实际上需要计算一个偏移量s,使得(p-c)R+c=(p+s)R重排的位为s=(c-cR)R',其中R'R的倒数.

When I want to do this sort of thing in systems like OpenGL, I'm think in terms of computing the transform which applies a 2x2 rotation matrix R about a centre c, and therefore thinking of points p being transformed (p-c)R+c = pR+c-cR, which gives a c-cR term to be used as the translation component of a transform. However, according to the issue above, scipy's affine_transform does "offset first" so we actually need to compute an offset s such that (p-c)R+c=(p+s)R which with a bit of rearrangement gives s=(c-cR)R' where R' is the inverse of R.

如果我将其插入ipython笔记本(pylab模式;下面的代码可能需要一些其他导入):

If I plug this into an ipython notebook (pylab mode; code below maybe needs some additional imports):

img=scipy.misc.lena()
#imshow(img,cmap=cm.gray);show()
centre=0.5*array(img.shape)
a=15.0*pi/180.0
rot=array([[cos(a),sin(a)],[-sin(a),cos(a)]])
offset=(centre-centre.dot(rot)).dot(linalg.inv(rot))
rotimg=scipy.ndimage.interpolation.affine_transform(
    img,rot,order=2,offset=offset,cval=0.0,output=float32
)
imshow(rotimg,cmap=cm.gray);show()

我知道

不幸的是,它没有绕中心旋转.

which unfortunately isn't rotated about the centre.

那我在这里想念的诀窍是什么?

So what's the trick I'm missing here?

推荐答案

只需快速进行&肮脏的测试我注意到,采用偏移量的负值似乎围绕中心旋转.

Just doing some quick & dirty testing I noticed that taking the negative value of your offset seems to rotate about the centre.

这篇关于如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆