2016-09-30 67 views
1

我正在尝试绘制一条线到网络摄像头输出。但是,我对以下代码有困难,特别是与“img”部分的绘制线功能有关。我已经看到了很多将图像添加到其他图像的例子,所以请不要将这些例子转介给我。这是网络摄像机输出的输出上的线或方形问题。在网络摄像头流上绘制线-Python

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") 

回答

2

你需要借鉴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") 
+0

谢谢@ martin-evans。这很好。没有()打印vc时出现问题。我在这种情况下删除了它,因为我不确定打印VC的目的是什么? – TsTeaTime

+1

你可以删除,这只是我检查我的摄像头连接。 –

+0

明白了......再次感谢您的帮助 – TsTeaTime