Replacing a quadrilateral with a square generated from PIL.QUAD

我怕爱的太早我们不能终老 提交于 2020-05-09 08:05:29

问题


I am working on a computer vision problem involving some basic transformations and could use your help.

Input image:

huawei_0001_01-before transform

Transformed image

huawei_0001_01

I understand that we take this quadrilateral and tilt it and stretch it to obtain a rectangle: extracted

I am wondering by what ratio does the quadrilateral get stretched to produce the rectangle? By what angle is the quadrilateral tilted to get the square? For example, if we had to replace the quadrilateral section by the transformed image (i.e replace image 3 with image 2), what would be the best way to do this?


回答1:


phew! I figured this out using this helpful answer at Merging perspective corrected image with transparent background template image using PILLOW [PIL, Python]

While you use QUAD to go from quadrilateral to rectangle, you can use perspective to go back from 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')


来源:https://stackoverflow.com/questions/61270583/replacing-a-quadrilateral-with-a-square-generated-from-pil-quad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!