首页 > 解决方案 > 用 PIL.QUAD 生成的正方形替换四边形

问题描述

我正在研究涉及一些基本转换的计算机视觉问题,可以使用您的帮助。

输入图像:

huawei_0001_01-改造前

转换后的图像

华为_0001_01

我知道我们取这个四边形并倾斜它并拉伸它以获得一个矩形: 提取的

我想知道四边形被拉伸以产生矩形的比例是多少?四边形倾斜什么角度得到正方形?例如,如果我们必须用变换后的图像替换四边形部分(即用图像 2 替换图像 3),那么最好的方法是什么?

标签: python-imaging-library

解决方案


呸!我在使用 PILLOW [PIL, Python] 合并透视校正图像与透明背景模板图像中使用这个有用的答案解决了这个问题

当您使用 QUAD 从quadrilateralto 时rectangle,您可以使用 perspective 从rectangleto返回quadrilateral

#!/usr/bin/env python3

from itertools import chain
from wand.color import Color
from wand.image import Image

with Image(filename='image2') as cover, Image(filename='image3') as template:
    w, h = cover.size
    cover.virtual_pixel = 'transparent'
    source_points = (
        (0, 0),
        (w, 0),
        (w, h),
        (0, h)
    )
    destination_points = (
        (628+78.37203406,  35.24937345),
        (628+577.65062655,  62.72203406),
        (628+550.17796594, 562.00062655),
        (628+50.89937345, 534.52796594)
    )
    order = chain.from_iterable(zip(source_points, destination_points))
    arguments = list(chain.from_iterable(order))
    cover.distort('perspective', arguments)

    # Overlay cover onto template and save
    template.composite(cover,left=0,top=0)
    template.save(filename='result.png')

推荐阅读