python-imaging-library - 用 PIL.QUAD 生成的正方形替换四边形
问题描述
我正在研究涉及一些基本转换的计算机视觉问题,可以使用您的帮助。
输入图像:
转换后的图像
我知道我们取这个四边形并倾斜它并拉伸它以获得一个矩形:
我想知道四边形被拉伸以产生矩形的比例是多少?四边形倾斜什么角度得到正方形?例如,如果我们必须用变换后的图像替换四边形部分(即用图像 2 替换图像 3),那么最好的方法是什么?
解决方案
呸!我在使用 PILLOW [PIL, Python] 合并透视校正图像与透明背景模板图像中使用这个有用的答案解决了这个问题
当您使用 QUAD 从quadrilateral
to 时rectangle
,您可以使用 perspective 从rectangle
to返回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')
推荐阅读
- excel - VBA 中用于访问 DB 的 CurrentDb 函数的变量未定义错误
- artificial-intelligence - 提升表示和接地表示之间有什么区别?
- vba - VBA 中的动态工作表循环
- html - 如何从angularjs中的表中获取选定的行
- r - 在 R 中创建一个 excel 单向数据表——我的 for 循环有问题
- selenium - 无法在 Internet Explorer 硒驱动程序中检测 iframe 内的元素
- amazon-web-services - 如何在 AWS Cloudformation 更新期间确保资源删除/创建顺序
- javascript - 从零开始 d3 轴时间刻度
- c++ - 下面的代码中虚函数是如何工作的
- php - 当id将与输入框名称扩展id匹配时,如何将动态输入框值插入到单列表格中