我对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)
我明白了
不幸的是,它没有围绕中心旋转.
那么我在这里缺少什么诀窍?
我试图通过使用模板匹配将2个图像拼接在一起找到3组点,我通过这些点来cv2.getAffineTransform()
获得一个经线矩阵,我将其传递cv2.warpAffine()
到对齐我的图像.
然而,当我加入我的图像时,我的大部分仿射图像都没有显示出来.我已经尝试过使用不同的技术来选择点,改变顺序或参数等等,但我只能得到一张瘦弱的图像来显示.
有人可以告诉我,我的方法是否有效,并建议我可能会出错?任何关于可能导致问题的猜测都将非常感激.提前致谢.
这是我得到的最终结果.下面是原始图像(1,2),并且我使用的代码:
编辑:这是变量的结果 trans
array([[ 1.00768049e+00, -3.76690353e-17, -3.13824885e+00],
[ 4.84461775e-03, 1.30769231e+00, 9.61912797e+02]])
Run Code Online (Sandbox Code Playgroud)
以下是传递给我们的观点cv2.getAffineTransform
:unified_pair1
array([[ 671., 1024.],
[ 15., 979.],
[ 15., 962.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
unified_pair2
array([[ 669., 45.],
[ 18., 13.],
[ 18., 0.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
import cv2
import numpy as np
def showimage(image, name="No name given"):
cv2.imshow(name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
image_a = cv2.imread('image_a.png')
image_b = cv2.imread('image_b.png')
def get_roi(image):
roi = cv2.selectROI(image) # spacebar to confirm …
Run Code Online (Sandbox Code Playgroud)