与平常一样,旋转时,需要平移到原点,然后旋转,然后平移回原点。在这里,我们可以将图像的中心作为原点。
import numpy as npimport matplotlib.pyplot as pltfrom scipy import miscfrom scipy.ndimage import rotatedata_orig = misc.face()x0,y0 = 580,300 # left eye; (xrot,yrot) should point theredef rot(image, xy, angle): im_rot = rotate(image,angle) org_center = (np.array(image.shape[:2][::-1])-1)/2. rot_center = (np.array(im_rot.shape[:2][::-1])-1)/2. org = xy-org_center a = np.deg2rad(angle) new = np.array([org[0]*np.cos(a) + org[1]*np.sin(a), -org[0]*np.sin(a) + org[1]*np.cos(a) ]) return im_rot, new+rot_centerfig,axes = plt.subplots(2,2)axes[0,0].imshow(data_orig)axes[0,0].scatter(x0,y0,c="r" )axes[0,0].set_title("original")for i, angle in enumerate([66,-32,90]): data_rot, (x1,y1) = rot(data_orig, np.array([x0,y0]), angle) axes.flatten()[i+1].imshow(data_rot) axes.flatten()[i+1].scatter(x1,y1,c="r" ) axes.flatten()[i+1].set_title("Rotation: {}deg".format(angle))plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)