在网络摄像头流上画线 -Python

TsT*_*ime 3 python opencv

我试图在网络摄像头输出上画一条线。但是,我在使用以下代码时遇到了困难,特别是绘制线函数的“img”部分。我见过很多将图像添加到另一个图像的示例,所以请不要参考这些示例。这特别是网络摄像头输出的输出线或正方形的问题。

cv2.line(img= vc, pt1= 10, pt2= 50, color =black,thickness = 1, lineType = 8, shift = 0)
Run Code Online (Sandbox Code Playgroud)

下面是完整的代码:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break
    else:
        cv2.line(img= vc, pt1= 10, pt2= 50, color =black,thickness = 1, lineType = 8, shift = 0)
vc.release()
cv2.destroyWindow("preview")
Run Code Online (Sandbox Code Playgroud)

Mar*_*ans 6

你需要在frame你得到的东西上画线。请尝试以下操作:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break
    else:
        cv2.line(img=frame, pt1=(10, 10), pt2=(100, 10), color=(255, 0, 0), thickness=5, lineType=8, shift=0)

vc.release()
cv2.destroyWindow("preview")   
Run Code Online (Sandbox Code Playgroud)