Can't set frame width and height with [OpenCV] cv2.VideoCapture.set()
The idea is to resize the frame without having to worry about setting the default frame size. Instead of using cv2.VideoCapture().set()
, you can use cv2.resize()
to resize the original 1920x1080
frame into 320x180
. But this method does not maintain aspect ratio. If you wanted to maintain aspect ratio, you can use the imutils
library. The imutils.resize()
function resizes the frame and maintains aspect ratio. Change the width
parameter to your desired resolution
import cv2
import imutils
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
ret, frame = cap.read()
frame = imutils.resize(frame, width=320)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()