在网络摄像头流上绘制线条-Python

4

我正在尝试在网络摄像头输出上绘制一条线。然而,我在以下代码以及“img”部分的绘制线条函数中遇到了困难。我已经看到了许多关于将图像添加到另一个图像中的例子,请不要参考这些例子。这是一个关于在网络摄像头输出上绘制线条或正方形的问题。

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

以下是完整的代码:
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")
1个回答

7
你需要在获取的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")   

谢谢@martin-evans。这个方法很好用。打印vc时出了问题,没有加括号。在这种情况下,我将其删除了,因为我不确定打印VC的目的是什么? - TsTeaTime
1
你可以移除那个,我只是在检查我的摄像头是否连接正常。 - Martin Evans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接