在Python中歪曲数组 [英] Skewing an array in Python
问题描述
我有一个2D数组,我将使用 scipy.misc.toimage()
保存为灰度图像。在这之前,我想将图像倾斜一个给定的角度,插值如 scipy.ndimage.interpolation.rotate()
:
I have a 2D array that I will save as a grayscale image using scipy.misc.toimage()
. Before doing so, I want to skew the image by a given angle, interpolating like scipy.ndimage.interpolation.rotate()
:
上图仅用于说明偏斜过程。我知道我必须放大我的图像才能包含偏斜的版本。我怎样才能做到这一点?我更喜欢使用scipy。
The image above is just to illustrate the skewing process. I am aware that I have to enlarge my image in order to contain the skewed version. How can I achieve this? I would prefer to use scipy for this.
推荐答案
这个脚本可以做到这一点。
This script can do that.
a=imread("sorNB.png")
h,l=a.shape
dl=50
b=numpy.zeros((h,l+dl),dtype=a.dtype)
for y in range(h):
dec=(dl*(h-y))//h
b[y,dec:dec+l]=a[y,:]
由于内部作业(b [y,dec:dec + l] = a [y,:])
是纯粹的numpy,这非常快。
Since the inner assignment (b[y,dec:dec+l]=a[y,:])
is pure numpy, this is very fast.
编辑
感谢ivan_pozdeev。插值的方法:
thanks to ivan_pozdeev. a way for interpolation :
from scipy.ndimage.interpolation import geometric_transform
a=imread("sorNB.png")
h,l=a.shape
def mapping(lc):
l,c=lc
dec=(dl*(l-h))/h
return l,c+dec
figure(1)
dl=50;c=geometric_transform(a,mapping,(h,l+dl),order=5,mode='nearest')
imshow (concatenate((a,zeros((225,50)),c),axis=-1),cmap=cm.gray)
这篇关于在Python中歪曲数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!