gpt4 book ai didi

python - OpenCV warpPerspective与透明图像有关的问题

转载 作者:行者123 更新时间:2023-12-02 17:10:47 40 4
gpt4 key购买 nike

我正在尝试使用OpenCV更改假眼镜图像的透 View ,但是透明部分和不透明性消失了。生成的图像没有透明胶片。
我想更改透 View ,以便将生成的图像标记在另一个图像上。

我可以使用OpenCV做到这一点吗?

#!/usr/bin/python
import numpy as np
import cv2

glasses = cv2.imread('fake_glasses.png')

RES_SIZE = (500,640)

pts1 = np.float32([[ 0, 0], [599, 0],
[ 0,208], [599,208]])
pts2 = np.float32([[ 94,231], [354,181],
[115,316], [375,281]])

M = cv2.getPerspectiveTransform(pts1,pts2)

rotated = cv2.warpPerspective(glasses, M, RES_SIZE)
cv2.imwrite("rotated_glasses.png", rotated)

fake_glasses.png (with transparent parts

mask.png

最佳答案

您加载的图像不正确,因此放置了透明层。这很容易验证-加载图像后打印图像的形状。

>>> img1 = cv2.imread('fake_glasses.png')
>>> print(img1.shape)
(209, 600, 3)

如果未指定, imread 的flags参数设置为 IMREAD_COLOR。根据 the documentation,这意味着

If set, always convert image to the 3 channel BGR color image.



相反,您应该使用 IMREAD_UNCHANGED

If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).



进行此更改后,图像将正确加载,包括Alpha平面。
>>> img2 = cv2.imread('fake_glasses.png', cv2.IMREAD_UNCHANGED)
>>> print(img2.shape)
(209, 600, 4)

关于python - OpenCV warpPerspective与透明图像有关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157653/

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