Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Video frame to numpy array

I just started with OpenCV. I have a small video clip that I want to process frame by frame. That's why I want to be able to access the pixel RGB values via Numpy arrays. What is the best way to accomplish this?

Now my code looks like this:

cap = cv2.VideoCapture('sample1.mkv')
cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
cv2.resizeWindow('frame', 800,800)

while(cap.isOpened()):
    ret, frame = cap.read()
    #np_frame = cv2.imread('video', frame) # does not work
    np_frame = np.asarray(cv2.GetMat(frame)) # does not work
    print(np_frame.shape)

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()