gpt4 book ai didi

python - 在 openCV 中使用单应性将 PNG 粘贴到图像上

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:51 29 4
gpt4 key购买 nike

我试图通过使用 this example. 在 openCV 中使用单应性将 png 图像粘贴到另一个图像 (jpg) 上jpg 格式适用于此,但当我尝试使用 png 时,白色区域显示。我还尝试在 imread 中添加 -1 和 cv2.IMREAD_UNCHANGED,这不会更改图像,但代码不会在此之后运行。请建议我一种粘贴 png 的方法。会有很大的帮助。代码是这样的,随后是图像。

import cv2
import numpy as np
from utils import mouse_handler
from utils import get_four_points
import sys


if __name__ == '__main__' :

# Read source image.
im_src = cv2.imread('first-image.jpg');
size = im_src.shape

# Create a vector of source points.
pts_src = np.array(
[
[0,0],
[size[1] - 1, 0],
[size[1] - 1, size[0] -1],
[0, size[0] - 1 ]
],dtype=float
);


# Read destination image
im_dst = cv2.imread('times-square.jpg');

# Get four corners of the billboard
print 'Click on four corners of a billboard and then press ENTER'
pts_dst = get_four_points(im_dst)

# Calculate Homography between source and destination points
h, status = cv2.findHomography(pts_src, pts_dst);

# Warp source image
im_temp = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))

# Black out polygonal area in destination image.
cv2.fillConvexPoly(im_dst, pts_dst.astype(int), 0, 16);

# Add warped source image to destination image.
im_dst = im_dst + im_temp;

# Display image.
cv2.imshow("Image", im_dst);
cv2.waitKey(0);

图像: https://github.com/spmallick/learnopencv/blob/master/Homography/first-image.jpg https://github.com/spmallick/learnopencv/blob/master/Homography/times-square.jpg

实用程序类 https://github.com/spmallick/learnopencv/blob/master/Homography/utils.py

如果你能给我一个答案就太好了。

最佳答案

问题在this link中解决. PNG 格式有一个称为“alpha”的额外 channel 用于透明度。

关于python - 在 openCV 中使用单应性将 PNG 粘贴到图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943806/

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