我对API感到困惑scipy.ndimage.interpolation.affine_transform.从这个问题来看,我不是唯一一个.我实际上想要做更多有趣的事情,affine_transform而不仅仅是旋转图像,但旋转会为初学者做.(是的,我很清楚scipy.ndimage.interpolation.rotate,但弄清楚如何驾驶affine_transform是我感兴趣的).
当我想在像OpenGL这样的系统中做这种事情的时候,我想在计算变换时会考虑一个2x2旋转矩阵R围绕一个中心c,因此想到p要被转换的点(p-c)R+c= pR+c-cR,这给出了一个c-cR术语来使用作为转换的翻译组件. 然而,根据上面的问题,scipy's affine_transform确实" 先偏移 ",所以我们实际上需要计算一个偏移量s,(p-c)R+c=(p+s)R以便通过一些重新排列给出s=(c-cR)R'其中R'的倒数R.
如果我把它插入ipython笔记本(pylab模式;下面的代码可能需要一些额外的导入):
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()
Run Code Online (Sandbox Code Playgroud)
我明白了

不幸的是,它没有围绕中心旋转.
那么我在这里缺少什么诀窍?