Opencv (Via Python) on Linux: Set Frame Width/Height

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()

Setting frame size of QuickCam Pro 3000 with OpenCV?

It seems that your camera was not initialized properly. The following code works for me.

using namespace cv;

[...]

VideoCapture capture(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, width);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, height);

I experimented with it a bit and found the following issues:

  1. capture.set returns 0 if capture was not initialized.
  2. capture.set returns 0 if camera is busy (another process using it).

It is not guaranteed that calling VideoCapture::set will change camera resolution to your desired resolution. For example, with my Logitech HD Pro Webcam C290, setting resolution to 640x480 and 1920x1080 works. But when I try 1024x768, VideoCapture::set returns true, but actual resolution is set to 960x720. So, check the actual resolution after reading a frame.

Get dimensions of a video file

In my last company we had similar problem and I couldn't find any python library to do this. So I ended up using mediainfo from python, media info also has a command line option and it is very easy to parse the output, so practically your python module which uses media-info will be sufficient. It has further advantage because eventually you will find all media-info type software doesn't support all codecs/format so you can use multiple software/libs under the hood with single python wrapper.



Related Topics



Leave a reply



Submit