skimage:定义垂直剪切

时间:2018-11-14 10:56:00

标签: python scikit-image

Python skimage程序包具有函数transform.AffineTransform(),其中一个选项是shear,它执行horizontal shear

很明显,我可以通过来回切换轴来进行垂直剪切。这就是我要做的:

from skimage import data, transform
import matplotlib.pyplot as plt
import numpy as np

img = data.astronaut()/255

v = 0.3

tf = transform.AffineTransform(shear=-v)
img2 = transform.warp(img, tf, order=1, preserve_range=True, mode='constant')

img3 = np.swapaxes(img, 0, 1)
img3 = transform.warp(img3, tf, order=1, preserve_range=True, mode='constant')
img3 = np.swapaxes(img3, 0, 1)

plt.imshow(np.hstack([img, img2, img3]))
plt.show()

shear

无论如何,我感到惊讶的是,没有更多直接的方法来定义垂直剪切选项...我弄错了吗?

1 个答案:

答案 0 :(得分:2)

您的问题(和链接的页面)保留了答案... AffineTransform允许您指定转换矩阵,而链接的Wiki页面显示了这是什么,因此很简单地减少了通过直接指定转换矩阵进行运算,例如

from skimage import data, transform
import matplotlib.pyplot as plt
import numpy as np

img = data.astronaut()/255

v = 0.3

tf = transform.AffineTransform(shear=-v)
img2 = transform.warp(img, tf, order=1, preserve_range=True, mode='constant')

img3 = np.swapaxes(img, 0, 1)
img3 = transform.warp(img3, tf, order=1, preserve_range=True, mode='constant')
img3 = np.swapaxes(img3, 0, 1)

plt.imshow(np.hstack([img, img2, img3]))

# Using the transformation matrix directly...

tf_h = transform.AffineTransform(
    np.array([[1, 0.3, 0], [0, 1, 0], [0, 0, 1]]))
img4 = transform.warp(img, tf, order=1, preserve_range=True, mode='constant')
tf_v = transform.AffineTransform(
    np.array([[1, 0, 0], [0.3, 1, 0], [0, 0, 1]]))
img4 = transform.warp(img, tf_h, order=1, preserve_range=True, mode='constant')
img5 = transform.warp(img, tf_v, order=1, preserve_range=True, mode='constant')

plt.figure()
plt.imshow(np.hstack([img, img4, img5]))

plt.show()

您应该看到两个具有相同图像集的图形。