问题
I am working on a computer vision problem involving some basic transformations and could use your help.
Input image:
Transformed image
I understand that we take this quadrilateral and tilt it and stretch it to obtain a rectangle:
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