python - 在 matplotlib 中裁剪变换后的图像

标签 python python-3.x numpy matplotlib

我正在尝试在 matplotlib 中显示由 3x3 矩阵转换的图像(使用齐次坐标)。我有以下内容:

import numpy as np
import matplotlib as mp
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

class GenericTransform(mp.transforms.Transform):
    input_dims = 2
    output_dims = 2
    is_separable = False

    # expects a 3x3 matrix as a numpy array
    def __init__(self, matrix):
        super().__init__()
        self.matrix = matrix
        self.has_inverse = (np.linalg.det(matrix) != 0)

    def transform(self, values):
        # append 1 to each coordinate in `values`
        hvals = np.insert(values, 2, 1, axis=1)

        # do the transformations
        thvals = np.matmul(self.matrix, hvals.transpose()).transpose()

        # TODO: perspective divison?

        # get rid of extra dimension and return
        return thvals[:, 0:2]

    def transform_non_affine(self, values):
        return self.transform(values)

    def inverted(self):
        return GenericTransform(np.linalg.inv(self.matrix))



# add an image to the plot with a transformation
def addImage(filename, matrix):
    transform = GenericTransform(matrix)
    img = mpimg.imread(filename)
    imgShow = plt.imshow(img, alpha=1)
    imgShow.set_transform(transform + imgShow.get_transform())

我使用图像文件名和矩阵调用 addImage,然后调用 plt.show(),并且转换似乎正确应用。然而,绘制的图像似乎超出了其应有的范围,就好像填充了由图像角定义的轴对齐框: transformed image goes beyond expected bounds 似乎有“反射”的线是我想要简单地切断图像的地方。我对 matplotlib 不太熟悉,所以我很困惑为什么会发生这种情况。我松散地关注this 。我还尝试使用路径手动剪辑图像:

# add an image to the plot with a transformation
def addImage(filename, matrix):
    transform = GenericTransform(matrix)
    img = mpimg.imread(filename)
    x = img.shape[1]
    y = img.shape[0]
    path = transform.transform_path(mp.path.Path([[0,0], [x,0], [x,y], [0,y], [0,0]]))
    patch = mp.patches.PathPatch(path, facecolor="none")
    imgShow = plt.imshow(img, alpha=1, clip_on=True, clip_path=patch)
    imgShow.set_transform(transform + imgShow.get_transform())

但这并没有完全按照我想要的方式工作。具体来说,每当我调整窗口大小或移动绘图时,剪切区域随着背景中的图像移动而保持静止。如何让它正确剪辑我的图像?

此外,我知道我进行转换的方式对于透视转换可能是错误的,但我想自己解决这个问题。

最佳答案

几个小时后,我终于明白了。我不太明白如何将变换应用于剪辑路径。最终,这奏效了:

def addImage(filename, matrix):
    transform = GenericTransform(matrix)
    img = mpimg.imread(filename)
    imgShow = plt.imshow(img, alpha=1, clip_on=True, interpolation="bilinear")
    oldTransform = imgShow.get_transform()
    imgShow.set_transform(transform + oldTransform)

    w = img.shape[1]
    h = img.shape[0]
    path = transform.transform_path(mp.path.Path([[0,0], [w,0], [w,h], [0,h], [0,0]]))
    patch = mp.patches.PathPatch(path, facecolor="none")
    patch.set_transform(oldTransform)
    imgShow.set_clip_path(patch)

关于python - 在 matplotlib 中裁剪变换后的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43726941/

相关文章:

python - Scipy curve_fit似乎没有改变初始参数

python - 将 Sublime Text 2 与可移植 Python 结合使用

c# - 从 C# 调用 Python

python - 全新安装后 Django-admin 无法运行

python - 将两个列表相乘求和

python-3.x - Azure 函数可以将数据写回 Synapse 数据库吗

python - “列表”对象没有属性 'reshape'

python - 用货币标记列值,字符串单元格除外

python - 在 Python 3.8 上安装 Matplotlib 时出错

python - 向量化 numpy 运算