我将坐标以这种方式存储在 x 轴和 y 轴上。
rects= [[715, 49], [716, 49], [711, 50], [712, 50], [713, 50],...]
image= cv2.imread("some_image.jpg")
如何在 rects
中存储的坐标中添加浅色透明图层到image
类似于下面的汽车顶部的蓝色层?最佳答案
这是一种选择,需要优化 (*)。
像您的图像一样定义一个白色蒙版,然后根据您的分割数据在其上绘制多边形:
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
这就是我从巴黎的图像中得到的:您还可以根据对象的类别更改颜色并添加轮廓线:
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)
并得到:(*) 规范化可以使用 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/