import cv2 as cv
import numpy as np
path_video = 'rsc/video.mp4'
capture = cv.VideoCapture(path_video)
path_video can be following values.
# retval is boolean, image is a frame of video
retval, image = capture.read()
The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call.
# dsize = (width, height)
# fx, fy = dst image's scale of src.size
# interpolation has default; INTER_LINEAR
image = cv.resize(src, dsize, dst, fx, fy, interpolation)
Resizes an image.
To shrink an image, it will generally look best with INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with INTER_CUBIC (slow) or INTER_LINEAR (faster but still looks OK).
# window's name; winname = 'Video'
cv.imshow('Video', image)
Displays an image in the specified window.
# flags has deflaut; WINDOW_AUTOSIZE
cv.namedWindow(winname, flags)
Creates a window.
WINDOW_NORMAL
the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.
WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
import cv2 as cv
import numpy as np
# Define function but it is nonessential
def rescale_frame(frame, scale=0.75):
width = int(frame.shape[1] * scale)
height = int(frame.shape[0] * scale)
dimension = width, height
return cv.resize(frame, dimension, interpolation=cv.INTER_AREA)
# reading dog video
video_path = 'opencv-course/Resources/Videos/dog.mp4'
capture = cv.VideoCapture(video_path)
ret, image = capture.read()
while ret:
ret, image = capture.read()
cv.imshow('Video', rescale_frame(frame))
# keyboard binding; to break out of playing, press 'd'
if cv.waitKey(20) & 0xFF == ord('d'):
break
capture.release()
cv.destroyAllWindows()