1

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. 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

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?

More information about the version:
Python 3.6.0  [MSC v.1900 64 bit (AMD64)] on win32
>>> import cv2
>>> cv2.__version__
'3.4.0'
5
  • 2
    As far as I know, VideoCapture is used for a variety of things, like video cameras, files, etc. Some of the properties are there to use only the get part, some of them can be set and some of them can be set only for certain cases (like video cams). In this case you cannot change it, for that you need to re do the video with such properties or maybe read it and resize and if wanted create a new video with the resized resolution (you may use cv2.resize for that purpose) Commented Mar 9, 2018 at 7:40
  • ^^ This is the right answer, @api55 you should post it as one. Commented Mar 9, 2018 at 8:16
  • @GPPK I was just being lazy to write it as a complete answer :), but I will do it in some minutes Commented Mar 9, 2018 at 8:19
  • Can always do it for you ;) Commented Mar 9, 2018 at 8:25
  • @GPPK this time I did it, but if you see any other one that I just comment feel free to write the answer :) Commented Mar 9, 2018 at 8:48

1 Answer 1

2

First, it is important to understand that VideoCapture is more than just a class to playback a video file. 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. 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. 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:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Just a note, Resizing a matrix will add on processing time. If you are running with video input you can use the set functions on (i.e. a webcam) then it will set the stream coming into your program and not add any additional overheads
Great answer and thanks. Last buy not least, i used imutils.resize finally, and the code as below: ret, frame = cap.read() frame = imutils.resize(frame, width= ResizeWidth)
@Question-erXDD great, that also works, just one more thing. Don't forget to check the ret value that it should be true... or you may have some unexpected errors if it fails to grab a frame :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.