python - 如何改变盒子的不透明度(cv2.rectangle)?
问题描述
我在 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%。我的问题是:如何更改不透明度?
最终结果应如下所示:
解决方案
我想为@HansHirse 答案添加一个小优化,我们可以先从 src 图像中裁剪矩形,然后将其与cv2.addWeighted
结果交换,而不是为整个图像创建画布:
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
推荐阅读
- python - 使用 matplotlib 在两个子图上的条形之间添加空间
- python - 如何从 Pandas Dataframe 中提取多个数字
- c++ - 使用模板在 C++ 中编译排序的链接列表对象/类时链接器命令失败
- asp.net-core-webapi - .NET Core 3.1 WebAPI IISExpress 在调试时挂起
- r - 导入数据时出现数字错误
- java - Android Studio:房间中的插入功能未正确存储实体
- php - Live Server 无法加载其他页面并出现 Apache 404 错误
- reactjs - 反应:尝试用 src/ 镜像 __tests__/
- javascript - 评论按钮在 jquery 函数中不起作用
- javascript - 对网站的 Http 请求以获取特定 html 元素的内容