如何使用 OpenCV python 为线条添加透明度?
How to add transparency to a line with OpenCV python?
我可以用 OpenCV 画线 Python 但我不能使线透明
def draw_from_pitch_to_image(image, reverse_output_points):
for i in range(0, len(reverse_output_points), 2):
x1, y1 = reverse_output_points[i]
x2, y2 = reverse_output_points[i + 1]
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
color = [255, 0, 0] if i < 1 else [0, 0, 255]
cv2.line(image, (x1, y1), (x2, y2), color, 2)
我更改了代码,但线条仍然不透明。我不知道为什么,有人可以帮我解决这个问题吗?
def draw_from_pitch_to_image(image, reverse_output_points):
for i in range(0, len(reverse_output_points), 2):
x1, y1 = reverse_output_points[i]
x2, y2 = reverse_output_points[i + 1]
alpha = 0.4 # Transparency factor.
overlay = image.copy()
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
color = [255, 0, 0] if i < 1 else [0, 0, 255]
cv2.line(overlay, (x1, y1), (x2, y2), color, 2)
cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)
一种方法是创建一个蒙版“叠加”图像(输入图像的副本),在该叠加图像上画一条线,然后使用 cv2.addWeighted()
对两个图像执行加权相加以模拟阿尔法通道。这是一个例子:
不透明的行 ->
结果 alpha=0.5
结果 alpha=0.25
这种应用透明度的方法可以推广到与任何其他绘图函数一起使用。这是 cv2.rectangle()
和 cv2.circle()
使用 alpha=0.5
透明度值的示例。
无透明度 ->
结果 alpha=0.5
代码
import cv2
# Load image and create a "overlay" image (copy of input image)
image = cv2.imread('2.jpg')
overlay = image.copy()
original = image.copy() # To show no transparency
# Test coordinates to draw a line
x, y, w, h = 108, 107, 193, 204
# Draw line on overlay and original input image to show difference
cv2.line(overlay, (x, y), (x + w, x + h), (36, 255, 12), 6)
cv2.line(original, (x, y), (x + w, x + h), (36, 255, 12), 6)
# Could also work with any other drawing function
# cv2.rectangle(overlay, (x, y), (x + w, y + h), (36, 255, 12), -1)
# cv2.rectangle(original, (x, y), (x + w, y + h), (36, 255, 12), -1)
# cv2.circle(overlay, (x, y), 80, (36, 255, 12), -1)
# cv2.circle(original, (x, y), 80, (36, 255, 12), -1)
# Transparency value
alpha = 0.50
# Perform weighted addition of the input image and the overlay
result = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0)
cv2.imshow('result', result)
cv2.imshow('original', original)
cv2.waitKey()
我可以用 OpenCV 画线 Python 但我不能使线透明
def draw_from_pitch_to_image(image, reverse_output_points):
for i in range(0, len(reverse_output_points), 2):
x1, y1 = reverse_output_points[i]
x2, y2 = reverse_output_points[i + 1]
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
color = [255, 0, 0] if i < 1 else [0, 0, 255]
cv2.line(image, (x1, y1), (x2, y2), color, 2)
我更改了代码,但线条仍然不透明。我不知道为什么,有人可以帮我解决这个问题吗?
def draw_from_pitch_to_image(image, reverse_output_points):
for i in range(0, len(reverse_output_points), 2):
x1, y1 = reverse_output_points[i]
x2, y2 = reverse_output_points[i + 1]
alpha = 0.4 # Transparency factor.
overlay = image.copy()
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
color = [255, 0, 0] if i < 1 else [0, 0, 255]
cv2.line(overlay, (x1, y1), (x2, y2), color, 2)
cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)
一种方法是创建一个蒙版“叠加”图像(输入图像的副本),在该叠加图像上画一条线,然后使用 cv2.addWeighted()
对两个图像执行加权相加以模拟阿尔法通道。这是一个例子:
不透明的行 ->
结果 alpha=0.5
结果 alpha=0.25
这种应用透明度的方法可以推广到与任何其他绘图函数一起使用。这是 cv2.rectangle()
和 cv2.circle()
使用 alpha=0.5
透明度值的示例。
无透明度 ->
结果 alpha=0.5
代码
import cv2
# Load image and create a "overlay" image (copy of input image)
image = cv2.imread('2.jpg')
overlay = image.copy()
original = image.copy() # To show no transparency
# Test coordinates to draw a line
x, y, w, h = 108, 107, 193, 204
# Draw line on overlay and original input image to show difference
cv2.line(overlay, (x, y), (x + w, x + h), (36, 255, 12), 6)
cv2.line(original, (x, y), (x + w, x + h), (36, 255, 12), 6)
# Could also work with any other drawing function
# cv2.rectangle(overlay, (x, y), (x + w, y + h), (36, 255, 12), -1)
# cv2.rectangle(original, (x, y), (x + w, y + h), (36, 255, 12), -1)
# cv2.circle(overlay, (x, y), 80, (36, 255, 12), -1)
# cv2.circle(original, (x, y), 80, (36, 255, 12), -1)
# Transparency value
alpha = 0.50
# Perform weighted addition of the input image and the overlay
result = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0)
cv2.imshow('result', result)
cv2.imshow('original', original)
cv2.waitKey()