2016-12-21 6 views
-1

opencv_pythonを使ってmp4ファイルをフレームに分割しようとしていますので、後でピローで開くか、少なくとも画像を使うことができます。私の方法をそれらの上で実行する。pythonのopencvでmp4からフレームの配列を作成するには

次のコードスニペットは、ライブビデオまたは録画済みビデオからフレームを取得することを理解します。

import cv2 
    cap = cv2.VideoCapture("myfile.mp4") 
    boolean, frame = cap.read() 

正確には読み取り関数が返すものと、変更可能な画像の配列を作成する方法を教えてください。

+0

参照:http://docs.opencv.org/ 2.4/modules/highgui/doc/reading_and_writing_images_and_video.html – putonspectacles

+0

も参照してくださいhttp://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html – putonspectacles

+0

リンクありがとうございますが、これは答えません私の質問。私の目標は、実際にディスクスペースを使用せずにこれらのイメージをアレイに格納することです。 –

答えて

0

How to process images of a video, frame by frame in video streaming using Opencv pythonに適合している。テストされていない。しかし、フレームは、numpy配列に読み込まれ、すべてのフレームが読み込まれるとnumpy配列に変換されたリストに追加されます。

import cv2 
import numpy as np 


images = [] 

cap = cv2.VideoCapture("./out.mp4") 
while not cap.isOpened(): 
    cap = cv2.VideoCapture("./out.mp4") 
    cv2.waitKey(1000) 
    print "Wait for the header" 

pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) 
while True: 
    boolen, frame = cap.read() # get the frame 
    if flag: 
     # The frame is ready and already captured 
     # cv2.imshow('video', frame) 

     # store the current frame in as a numpy array 
     np_frame = cv2.imread('video', frame) 
     images.append(np_frame) 

     pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) 
    else: 
     # The next frame is not ready, so we try to read it again 
     cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1) 
     print "frame is not ready" 
     # It is better to wait for a while for the next frame to be ready 
     cv2.waitKey(1000) 

    if cv2.waitKey(10) == 27: 
     break 
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): 
     # If the number of captured frames is equal to the total number of frames, 
     # we stop 
     break 

all_frames = np.array(images) 
+0

ありがとうございます。コードをコメントしていただきありがとうございます。私はそれをよく理解していることが大好きです –

+0

問題はありません。それが成功した場合は、答えとしてマークすることを検討してください。 – putonspectacles

+0

ねえ、私は小さな問題があります。 –

関連する問題