python - 在已知坐标的图像上应用轻微透明的蒙版

标签 python image opencv python-imaging-library

我将坐标以这种方式存储在 x 轴和 y 轴上。

rects= [[715, 49], [716, 49], [711, 50], [712, 50], [713, 50],...]
image= cv2.imread("some_image.jpg")
如何在 rects 中存储的坐标中添加浅色透明图层到image类似于下面的汽车顶部的蓝色层?
enter image description here

最佳答案

这是一种选择,需要优化 (*)。
像您的图像一样定义一个白色蒙版,然后根据您的分割数据在其上绘制多边形:

mask = np.ones_like(img) * 255
points = np.array([(320, 40), (450, 350), (250, 350)], np.int32)

color = (0, 255, 255)
cv2.fillPoly(mask, [points], color)
现在你有一个带有黄色三角形的白色蒙版,将图像乘以蒙版,然后归一化:
img_with_overlay = np.int64(img) * mask # <- use int64 terms
max_px_val = np.amax(img_with_overlay) # <-- Max pixel alue
img_with_overlay = np.uint8((img_with_overlay/max_px_val) * 255) # <- normalize and convert back to uint8
这就是我从巴黎的图像中得到的:
enter image description here

您还可以根据对象的类别更改颜色并添加轮廓线:
points = np.array([(100, 150), (150, 250), (100, 300), (50, 250)], np.int32)
cv2.fillPoly(mask, [points], (200, 255, 200))
cv2.polylines(mask, [points], True, (100, 200, 100), 2)
并得到:
enter image description here

(*) 规范化可以使用 cv.normalize 重写:
img_with_overlay = cv2.normalize(np.int64(img) * mask, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)

关于python - 在已知坐标的图像上应用轻微透明的蒙版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63177320/

相关文章:

C++ Opencv 将 CSV 文件加载为 cv::Mat

opencv - 使用线条的简单绘图的近似照片

python - 在 openCV 中使用 approxPolyDP() 检测矩形不准确

Python: 'global' 和 globals().update(var) 之间的区别

python - PJSUA 使用 c 进行 sip 注册时出错

python - 与 python3 asyncio 建立连接

ios - iOS 中的 OpenCV 颜色提取

android - 删除图像文件创建一个具有相同名称的 0 字节文件

javascript - 用图像替换文本 Google 脚本?

python - 如何强制列表为固定大小?