使用 Python 进行图像变形

Din*_* K. 2 python image-processing

我需要在Python中扭曲相对较大尺寸(1679x1475)的图像。我有转换后的坐标。如何有效地将图像扭曲到变换后的坐标系。我尝试了 scipy.interpolate.griddata,但很快我的计算机内存不足。

T.A*_*.A. 6

您想要scipy.ndimage.map_coordinates。您可以配置插值方法及其处理原始图像之外的点的方式。一个例子:

import numpy as np
from scipy import misc
#create a 2D array that has a grayscale image of a raccoon
face = misc.face(gray=True)

import matplotlib.pyplot as plt
plt.imshow(face,cmap=plt.cm.gray)
Run Code Online (Sandbox Code Playgroud)

未扭曲的图像看起来像这样

#set up our new coordinate system
rows,cols = np.mgrid[0:768, 0:1024]
rows = rows**(1/2) * 767**(1/2)
cols = cols**(2) / 1023
rows = np.roll(rows,150,0)

from scipy import ndimage
#warp the image using a 3rd order (cubic) spline interpolation
new_img = ndimage.map_coordinates(face,[rows,cols], order=3)
plt.figure()
plt.imshow(new_img,cmap=plt.cm.gray)
Run Code Online (Sandbox Code Playgroud)

变形后的图像