2016-12-31 13 views
2

私はOpenCV2を使ってウェブカメラでいくつかのタイムアウト写真を撮影しています。私はウェブカメラが見た最新のビューを抽出したいと思います。私はこれを達成しようとします。ウェブカメラから最新のフレームを取得

import cv2 
a = cv2.VideoCapture(1) 
ret, frame = a.read() 
#The following garbage just shows the image and waits for a key press 
#Put something in front of the webcam and then press a key 
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)] 
#Since something was placed in front of the webcam we naively expect 
#to see it when we read in the next image. We would be wrong. 
ret, frame = a.read() 
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)] 

ウェブカメラの前に置かれた画像は表示されません。

import cv2 
a = cv2.VideoCapture(1) 
ret, frame = a.read() 
#Place something in front of the webcam and then press a key 
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)] 

#Purge the buffer 
for i in range(10): #Annoyingly arbitrary constant 
    a.grab() 

#Get the next frame. Joy! 
ret, frame = a.read() 
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)] 

さて、これは動作しますが、それはうるさく非科学的と遅いです:バッファのいくつかの種類がありますかのようにだから私はそうのようなバッファは、ことをパージする...ほとんど

です。バッファー内の最新のイメージのみを特に要求する方法はありますか?それとも、バッファをパージするためのよりよい方法でしょうか?

答えて

-1

私はCapture single picture with opencvから少しのコードを見つけました。キャプチャされた最新の画像を連続して表示するように変更しました。それはバッファの問題を持っていないようですが、私はあなたの質問を誤解しているかもしれません。

import numpy as np 
import cv2 

cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop) 
ret,frame = cap.read() # return a single frame in variable `frame` 


while(True): 
    ret,frame = cap.read() # return a single frame in variable `frame 
    cv2.imshow('img1',frame) #display the captured image 
    if cv2.waitKey(1) & 0xFF == ord('y'): #save on pressing 'y' 
     cv2.imwrite('images/c1.png',frame) 
     cv2.destroyAllWindows() 
     break 

cap.release() 
+0

をすることができます。画像キャプチャの間に遅延があるので、バッファリングの問題が私のために現れます。 – Richard

0

IはVideoCaptureオブジェクトに5フレームバッファがあることを読んだこと、およびフレームをとり、それをデコードしない.grab方法があります。

だから、それが継続的にバッファを空にされているため、それはバッファの問題を持っていない可能性があり

cap = cv2.VideoCapture(0) 
for i in xrange(4): 
    cap.grab() 
ret, frame = cap.read() 
... 
+0

どこを読んでいますか?リンクを提供してください。 – Richard

+0

ここでは、バッファとそのサイズを設定する方法について言及しています(私にとってはうまくいかなかった)https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-set –

+0

ここでは特に5つのフレームのnoticeigですhttp://answers.opencv.org/question/29957/highguivideocapture-buffer-introducing-lag/ –

関連する問題