pay*_*yam 6 python drawing opencv image image-processing
我可以用 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)
Run Code Online (Sandbox Code Playgroud)
我更改了代码,但该行仍然不透明。我不知道为什么,有人可以帮我解决这个问题吗?
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)
Run Code Online (Sandbox Code Playgroud)
nat*_*ncy 10
一种方法是创建蒙版“覆盖”图像(输入图像的副本),在该覆盖图像上绘制一条线,然后使用模拟cv2.addWeighted()Alpha 通道对两个图像执行加权相加。这是一个例子:
没有透明度的线条->结果为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()
Run Code Online (Sandbox Code Playgroud)