我试图在网络摄像头输出上画一条线。但是,对于以下代码,特别是绘制线功能的“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")

最佳答案

您需要在获得的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")

关于python - 在网络摄像头流上画线-Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39785476/

10-15 17:22