2016-09-30 68 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