Ask Your Question
0

Video frame to numpy array

asked 2017-10-04 05:00:59 -0600

demuno gravatar image

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()
edit retag flag offensive close merge delete

Comments

1

frame is a numpy array already, just use it "as is".

berak gravatar imageberak ( 2017-10-04 05:05:21 -0600 )edit

cv2.GetMax doesn't work on python3, So used this height, width = img.shape[:2]

supra56 gravatar imagesupra56 ( 2018-11-04 20:28:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-11-04 13:33:26 -0600

punnerud gravatar image

updated 2018-11-04 13:35:01 -0600

In CV2 everything is returned as NumPy objects: What is different between all these OpenCV Python interfaces?

Using frame directly, then it works:

import cv2
cap = cv2.VideoCapture('rain.avi')
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)
    print(frame.shape)

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

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-04 05:00:59 -0600

Seen: 24,443 times

Last updated: Nov 04 '18