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()

python - skimage : defining vertical shear-LMLPHP

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

最佳答案

您的问题(和链接页面)包含答案......因为 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()

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

关于python - skimage : defining vertical shear,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53298558/

10-12 23:24