gpt4 book ai didi

python - 如何改变盒子的不透明度(cv2.rectangle)?

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:12 24 4
gpt4 key购买 nike

我在 OpenCV 中绘制了一些矩形并在其中放置了文本。我的一般方法如下所示:

# Draw rectangle    p1(x,y)    p2(x,y)    Student name box
cv2.rectangle(frame, (500, 650), (800, 700), (42, 219, 151), cv2.FILLED )
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (510, 685), font, 1.0, (255, 255, 255), 1

到目前为止一切正常。唯一的问题是,所有框中的不透明度都是 100%。我的问题是:如何更改不透明度?

最终结果应该是这样的:

Desired outcome

最佳答案

我想对@HansHirse 的回答添加一个小的优化,我们可以先从 src 图像裁剪矩形,然后将其与 cv2.addWeighted 交换,而不是为整个图像创建 Canvas 。结果为:

import cv2
import numpy as np

img = cv2.imread("lena.png")

# First we crop the sub-rect from the image
x, y, w, h = 100, 100, 200, 100
sub_img = img[y:y+h, x:x+w]
white_rect = np.ones(sub_img.shape, dtype=np.uint8) * 255

res = cv2.addWeighted(sub_img, 0.5, white_rect, 0.5, 1.0)

# Putting the image back to its position
img[y:y+h, x:x+w] = res

enter image description here

关于python - 如何改变盒子的不透明度(cv2.rectangle)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56472024/

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