简体   繁体   English

在 PNG 图像上画一条线 OpenCV2 Python

[英]Drawing a line on PNG image OpenCV2 Python

How to add colored line on rgba (.png) image using opencv?如何使用 opencv 在 rgba (.png) 图像上添加彩色线条?

I tried following but line drawn is transparent.我试过跟随但绘制的线是透明的。

import cv2
image = cv2.imread("/content/drive/My Drive/universe-fg.png",-1)
from google.colab.patches import cv2_imshow

image2 = cv2.resize(image,(150,150))
cv2.line(image2, (20, 30), (100, 80), (255, 255, 0), 10)
cv2_imshow(image2)

The result:结果:

输出图像

using cv2 version 4.1.2使用 cv2 版本 4.1.2

Your mistake is that you need to specify an opaque alpha value in your line color.你的错误是你需要在你的线条颜色中指定一个不透明的 alpha 值。 So use (255, 255, 0, 255) rather than (255, 255, 0).所以使用 (255, 255, 0, 255) 而不是 (255, 255, 0)。 The latter assumes a value of 0 (transparent) when not specified.后者在未指定时假定值为 0(透明)。

So here is how to do that in Python/OpenCV.所以这里是如何在 Python/OpenCV 中做到这一点。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# load transparent image
img = cv2.imread('blue_circle.png', cv2.IMREAD_UNCHANGED)
hh, ww = img.shape[:2]

# draw colored line as opaque
result = img.copy()
cv2.line(result, (20, 30), (100, 80), (255, 255, 0, 255), 10)

# save result
cv2.imwrite('blue_circle_line.png', result)

# display result, though it won't show transparency
cv2.imshow("IMAGE", img)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM