无法使用[OpenCV] cv2.VideoCapture.set()设置帧的宽度和高度 [英] Can't set frame width and height with [OpenCV] cv2.VideoCapture.set()

查看:1611
本文介绍了无法使用[OpenCV] cv2.VideoCapture.set()设置帧的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视频捕获的帧/窗口大小减小到320x180,但是我似乎做不到.我正在使用Windows Kinect for Xbox One,并使用适配器将其连接到我的PC.

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.

我尝试将cv2.CAP_PROP_FRAME_WIDTH设置为320,将cv2.CAP_PROP_FRAME_HEIGHT设置为180,但是一旦获得值,它就会返回1920和1080.我还尝试安装并重新安装Kinect SDK和驱动程序.

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

打印输出

我想帮助您了解问题的根源,并希望得到解决.

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

推荐答案

该想法是调整框架的大小,而不必担心设置默认框架大小.除了使用cv2.VideoCapture().set(),您还可以使用 cv2.resize() 将原始1920x1080帧的大小调整为<​​c0>.但是这种方法不能保持宽高比.如果要保持宽高比,可以使用 imutils 库. imutils.resize()功能调整框架的大小并保持宽高比.将width参数更改为所需的分辨率

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

这篇关于无法使用[OpenCV] cv2.VideoCapture.set()设置帧的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆