7

I try to change video resolution (with mp4!) (to 800x600) in this way : but its doesn't work, when I use cap.get(3) and (4), its return every time defualt 1280x720!

import cv2
cap = cv2.VideoCapture('file')
while(cap.isOpened()):
    cv2.waitKey(10)

    ret, frame = cap.read()
    cap.set(3, 800)
    cap.set(4, 600)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) 
    print cap.get(3) # return default 1280       

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

cap.release()
cv2.destroyAllWindows()

What I'm doing wrong?

I tried -

cv2.resizeWindow("ssss", 300, 300), 

and

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)

no effect !

3
  • try with your webcam by cv2.VideoCapture(0) it give you 800 not 1280 Commented Aug 10, 2017 at 5:43
  • 1
    With webcam its work, change to file ! - mp4 Commented Aug 10, 2017 at 5:50
  • 1
    @TheRutubeify If you find the answer on Stack OverFlow. You should follow these rules. stackoverflow.com/help/someone-answers. If you know the answer to the question. You should give the answer in Answer panel. Don't give the answer in question panel. Commented Aug 10, 2017 at 7:31

3 Answers 3

10
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    cv2.waitKey(10)

    ret, frame = cap.read()
    cap.set(3, 800)
    cap.set(4, 600)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) 
    print cap.get(3) # return default 1280       

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

cap.release()
cv2.destroyAllWindows()

This your code works with webcame, not with file

for a video file, you can resize the window

cv2.resizeWindow(winname, width, height) 

for that first define window with name and resize it

example

  cv2.namedWindow("frame", 0);
  cv2.resizeWindow("frame", 800,600);

for Detail resize window

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

6 Comments

How to use it ? cv2.resizeWindow("cap", 300, 300) in loop ? I tried this way, no change (
Its just open new "blank" empty window with head "Final"
@TheRutubeify cv2.imshow('frame',gray) line change to cv2.imshow('Final',gray)
Yes its helpful, because I can't find in docs and internet any solution about this simple feature !
@TheRutubeify accept the answer so for another user also can get right answer
|
4

I think there are a few things in your code that might need attention.

  1. As described in the OpenCV documentation for VideoCapture, if you want to access your default WebCam, you'd need to initialise the class as follows:

    cap = cv2.VideoCapture('file')
    

    If you are trying to then change the resolution of the camera, I'd suggest to move the two set lines right below the initialisation of cap and only perform it once - not each time you read in the frame. You can also use constants to access the right attributes:

    cap = cv2.VideoCapture('file')
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    
    # Your while loop and the rest of the code...
    
  2. If you are trying to read the frame from a file and want to change it's resolution, you'd probably want to use the resize method as described here. This would need to be done inside the loop, right after you read in the frame. It could be something like:

    resize(ret, ret, Size(800, 600), 0, 0, INTER_CUBIC); 
    

I hope this helps.

2 Comments

I tried cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600) Its does't help !
Its the same - cap.set(3, 800) and cap.set(4, 600) like I wrote above!
1

cap.set() has no effect below resolutions of 640,480 (at least for my macbook pro) You can increase the resolution, but for example setting it to 300,300 has no effect. As for my experience you ned to call resize() on the frame after you read() it.

1 Comment

Same for me on macbook pro High Sierra. Always printing width 640 no matter what I put lower than 640.

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.