OpenCV显示单图像摄像头

时间:2014-11-12 22:26:55

标签: python python-2.7 opencv webcam

我的网络摄像头有一个流。但是当我按下一个键(空格键)并在另一个窗口中显示这个单帧时,我想拍一个帧。每次按下按钮,窗口都会使用新图片进行更新。这可能吗?

到目前为止,这是我的代码:

import cv2
import sys

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break   
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:0)

您所要做的就是在变量中捕获waitKey的输出。您已经在循环中针对qbreak对其进行了检查。如果是空格,您可以使用imshow在不同的窗口中显示它。

答案 1 :(得分:0)

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

if not(cap.isOpened()):
    cap.open()

while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    k= cv2.waitKey(1)
    if k == 32:#when u press spacebar display that frame in another window
        cv2.imshow('new',frame)
    elif k == ord('q'):#press q to quit
        break

cap.release()
cv2.destroyAllWindows()