gpt4 book ai didi

python - 如何在同一图像的不同部分上使用warpperspective?

转载 作者:行者123 更新时间:2023-12-02 17:07:01 27 4
gpt4 key购买 nike

假设我有一个要“展平”的书的封面图片。为此,我似乎需要执行2个透视变换:一个仅用于封面,一个仅用于后封面:

enter image description here

什么是最有效的方法?

最佳答案

使用600x600像素的图片homograpy-test.jpg:
enter image description here

import cv2
import numpy as np

#load image
img = cv2.imread('homography-test.jpg', cv2.IMREAD_COLOR)

#corners of book covers (before)
frontCoverPtsBefore = np.array([[32, 48], [279, 136], [247, 430], [39, 281]], dtype="float32")
backCoverPtsBefore = np.array([[279, 136], [474, 36], [463, 316], [247, 430]], dtype="float32")

#corners of book covers (after)
frontCoverPtsAfter = np.array([[0, 0], [299, 0], [299, 599], [0, 599]], dtype="float32")
backCoverPtsAfter = np.array([[300, 0], [599, 0], [599, 599], [300, 599]], dtype="float32")

#get the transformation matrices for both covers
M_front = cv2.getPerspectiveTransform(frontCoverPtsBefore, frontCoverPtsAfter)
M_back = cv2.getPerspectiveTransform(backCoverPtsBefore, backCoverPtsAfter)

#warpPerspective both images
img_front = cv2.warpPerspective(img, M_front, (600, 600))
img_back = cv2.warpPerspective(img, M_back, (600, 600))

#copy half of the warped back cover into the warped front cover
np.copyto(img_front[:, 300:, :], img_back[:, 300:, :])

#display before and after
cv2.imshow('img', img)
cv2.imshow('img_front', img_front)
cv2.waitKey(0)
cv2.destroyAllWindows()

之前和之后:
enter image description here

关于python - 如何在同一图像的不同部分上使用warpperspective?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52175245/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com