gpt4 book ai didi

python - 在 opencv python (cv2) 中调整透明图像的大小

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

我有一个形状为 (120, 470, 4) 的图像,我想将其调整为另一个尺寸,例如(100、400、4)。我尝试通过使用 cv2.resize 函数来实现它,如下所示

reshaped_image = cv2.resize(图像, (100, 400))

并将 reshaped_image 粘贴到更大的透明图像中,但问题是 reshaped_image 的形状是 (100, 400, 3) 而不是 (100, 400, 4)。

是我做错了什么,还是 cv2.resize 函数会丢失透明度信息?最后,我很高兴知道您的解决方案。 (如果可能的话用简单的例子)

问候

最佳答案

这是一个加载 3 channel 图像的示例,获取其蓝色 channel 并将其复制为 alpha channel (这根本没有意义,但我手头没有 4 channel 图像)并再次将其合并为 4 channel 图像。然后调整此图像的大小。

如果加载4 channel 图片,标志位-1表示图片原样加载,所以可以直接加载和拆分所有4 channel 。

打印所有图像的形状,并调整大小。我正在使用 opencv 版本 3.1.0。

希望对您有所帮助。

import cv2
import numpy as np

img = cv2.imread('image.jpg', -1)
print img.shape

b_channel, g_channel, r_channel = cv2.split(img)
alpha_channel = b_channel
print b_channel.shape
print g_channel.shape
print r_channel.shape
print alpha_channel.shape

img_RGBA = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
print img_RGBA.shape

res = cv2.resize(img_RGBA,(100, 400), interpolation = cv2.INTER_CUBIC)
print res.shape

关于python - 在 opencv python (cv2) 中调整透明图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512119/

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