Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set frame width and height with [OpenCV] cv2.VideoCapture.set()

I am trying to reduce the frame/window size of my video capture to 320x180 but I can't seem to do it. I am using a Windows Kinect for Xbox One and its connected to my pc using an adapter.

I have tried setting the cv2.CAP_PROP_FRAME_WIDTH to 320 and cv2.CAP_PROP_FRAME_HEIGHT to 180 but once I try and get the values it returns 1920 and 1080. I have also tried installing and reinstalling the Kinect SDK and drivers.

import cv2
import numpy as np

vid = cv2.VideoCapture(0)

vid.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 180)
vid.set(cv2.CAP_PROP_FPS, 25)

print(vid.get(cv2.CAP_PROP_FPS))
print(vid.get(cv2.CAP_PROP_FRAME_WIDTH))

while True:
    ret, frame = vid.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

vid.release()
cv2.destroyAllWindows()

print output

I would like help in knowing where the problem stems from and hopefully get a fix.

like image 793
blinkThrice Avatar asked Aug 26 '19 15:08

blinkThrice


People also ask

How to set CV2 videocapture image size in Python?

How to set cv2.VideoCapture() image size in Python. Use cv2.CAP_PROP_FRAME_WIDTH and cv2.CAP_PROP_FRAME_HEIGHT in order to tell OpenCV which image size you would like. import cv2 video_capture = cv2.VideoCapture(0) # Check success if not video_capture.isOpened(): raise Exception("Could not open video device") # Set properties.

How to resize 1920x1080 frame to 320x180 using CV2 Video Capture?

Instead of using cv2.VideoCapture().set(), you can use cv2.resize()to resize the original 1920x1080frame into 320x180. But this method does not maintain aspect ratio. If you wanted to maintain aspect ratio, you can use the imutilslibrary. The imutils.resize()function resizes the frame and maintains aspect ratio.

How to set random resolution in OpenCV?

When you try to set random resolution, opencv sets nearest resolution if that resolution is not available. You can run below command to find out all the available resolutions of your webcam. uvcdynctrl -f And set those resolutions only.


2 Answers

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()
like image 165
nathancy Avatar answered Oct 17 '22 04:10

nathancy


Finally I have found that solved this problem

When you try to set random resolution, opencv sets nearest resolution if that resolution is not available.

You can run below command to find out all the available resolutions of your webcam.

uvcdynctrl -f

And set those resolutions only. (my webcam resolutions: 1280x720, 640x480, 640x360)

vid.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

Reference: https://techoverflow.net/2018/12/18/how-to-set-cv2-videocapture-image-size/

like image 25
naam Avatar answered Oct 17 '22 05:10

naam