2016-12-21 1 views
-1

opencv_python을 사용하여 mp4 파일을 프레임으로 분해하려고합니다. 나중에 베개로 열거 나 이미지를 사용할 수 있습니다. 그들 자신의 방법을 실행하십시오.python 용 opencv에서 프레임 배열을 만드는 방법

다음 코드 스 니펫은 라이브 비디오 또는 녹화 된 비디오에서 프레임을 가져옵니다.

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

정확하게 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

이봐, 작은 문제가있어. –

관련 문제