在opencv python中创建透明图像 [英] Create transparent image in opencv python

查看:98
本文介绍了在opencv python中创建透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作透明图像并在其上绘制,然后在基础图像上添加权重.

I am trying to make a transparent image and draw on it, and after I will addWeighted over the base image.

如何在openCV python中初始化具有宽度和高度的全透明图像?

How can I initialize fully transparent image with width and hight in openCV python?

我想像在Photoshop中那样制作效果,将图层堆叠在一起,所有堆叠的图层最初都是透明的,并且绘制是在完全透明的图层上进行的.最后,我将合并所有图层以获得最终图像

I want to make a effect like in Photoshop, having stack of the layers, all stacked layers are initially transparent and drawing is performed on fully transparent layer. On the end I will merge all layers to get final image

推荐答案

如果要在多个图层"上绘制,然后将图形堆叠在一起,那么如何做:

If you want to draw on several "layers" and then stack the drawings together, then how about this:

import cv2
import numpy as np

#create 3 separate BGRA images as our "layers"
layer1 = np.zeros((500, 500, 4))
layer2 = np.zeros((500, 500, 4))
layer3 = np.zeros((500, 500, 4))

#draw a red circle on the first "layer",
#a green rectangle on the second "layer",
#a blue line on the third "layer"
red_color = (0, 0, 255, 255)
green_color = (0, 255, 0, 255)
blue_color = (255, 0, 0, 255)
cv2.circle(layer1, (255, 255), 100, red_color, 5)
cv2.rectangle(layer2, (175, 175), (335, 335), green_color, 5)
cv2.line(layer3, (170, 170), (340, 340), blue_color, 5)

res = layer1[:] #copy the first layer into the resulting image

#copy only the pixels we were drawing on from the 2nd and 3rd layers
#(if you don't do this, the black background will also be copied)
cnd = layer2[:, :, 3] > 0
res[cnd] = layer2[cnd]
cnd = layer3[:, :, 3] > 0
res[cnd] = layer3[cnd]

cv2.imwrite("out.png", res)

这篇关于在opencv python中创建透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆