简体   繁体   English

无法在Python环境下通过OpenCV调整分辨率

[英]Unable to adjust resolution by opencv under python environment

I am currently try to adjust the resolution of the video by opencv , according to my research, cap.set is the method to do so. 根据我的研究,我目前正在尝试通过opencv调整视频的分辨率, cap.set是实现此目的的方法。 thus, I write the code below: 因此,我编写以下代码:

cap = cv2.VideoCapture('IMG_2957.MOV')
for i in range(19):
    print (""+str(i)+" "+str(cap.get(i)))
cap.set(3,160) #Width
cap.set(4,120) #Height
for i in range(19):
    print (""+str(i)+" "+str(cap.get(i)))

very surprise, the value of "CV_CAP_PROP_FRAME_WIDTH" and "CV_CAP_PROP_FRAME_HEIGHT" remain unchanged 非常令人惊讶的是,“ CV_CAP_PROP_FRAME_WIDTH”和“ CV_CAP_PROP_FRAME_HEIGHT”的值保持不变

Before the cv.set 
3 1920.0
4 1080.0

And after the setting, the frame was not set as what I want 设置后,框架没有设置为我想要的

After the cv.set 
3 1920.0
4 1080.0

Is that we are not able to change the resolution by setting its property? 是不是我们无法通过设置其属性来更改分辨率? Can anyone advice me the usage of .set of cv2.videocapture ? 谁能建议我cv2.videocapture .setcv2.videocapture

More information about the version:
Python 3.6.0  [MSC v.1900 64 bit (AMD64)] on win32
>>> import cv2
>>> cv2.__version__
'3.4.0'

First, it is important to understand that VideoCapture is more than just a class to playback a video file. 首先,重要的是要了解VideoCapture不仅仅是播放视频文件的类。 It can also connect to a stream or a camera for example. 例如,它还可以连接到流或摄像机。 Then it may have properties that will work only for specific cases and that is why they have a set / get generic function. 然后,它可能具有仅适用于特定情况的属性,这就是为什么它们具有set / get泛型函数的原因。 With a webcam, if your webcam/drivers allow it you may set the resolution for example with the code snippet you supply. 对于网络摄像头,如果您的网络摄像头/驱动程序允许,则可以使用提供的代码段设置分辨率。

The set function returns a bool value in C++ (and in python) not sure if this means that it is set or not, but you can check it out, probably yours returns false. set函数在C ++(和python)中返回布尔值,不确定是否表示已设置,但您可以将其检出,很可能返回false。 I am not sure, since I had not checked that and the documentation does not provide an explanation. 我不确定,因为我没有检查过,文档也没有提供解释。

In a video, it will read each frame, but it may not change the properties of the video. 在视频中,它将读取每一帧,但可能不会更改视频的属性。 What can you do then? 那你怎么办 Depends on what you are planning to do with it. 取决于您打算如何处理。

You may use the resize , for example: 您可以使用resize ,例如:

retval, frame = cap.read()
res = cv2.resize(frame,(160, 120), interpolation = cv2.INTER_CUBIC)

Then you may save the video with VideoWritter or do any image manipulation to it. 然后,您可以使用VideoWritter保存视频或对其进行任何图像处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM