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:
capture.set
returns 0 ifcapture
was not initialized.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
How to Send a Signal from a Python Program
Decrypt Chrome Linux Blob Encrypted Cookies in Python
How to Organize My Tkinter Appllication
Gunicorn Command Not Found, But It's in My Requirements.Txt
Getting a List of All Subdirectories in the Current Directory
Extracting Data from HTML Table
Check If a String Contains a Number
How to Get Hard Disk Serial Number Using Python
How to Remove List Elements in a For Loop in Python
How to Find the Mountpoint a File Resides On
How to Get a List of Locally Installed Python Modules
How to Push a Subprocess.Call() Output to Terminal and File
How to Convert Seconds to Hours, Minutes and Seconds
Multiprocessing: Use Only the Physical Cores
Do Not Want the Images to Load and CSS to Render on Firefox in Selenium Webdriver - Python
Proxies With Python 'Requests' Module