cv2 resolution of captured picture

Hello I have this simple code:

from cv2 import *
import time
# initialize the camera


for x in range (10):
    cam = VideoCapture(0)   # 0 -> index of camera
    s,img = cam.read()
    imwrite(str(x) + ".jpg",img) #save image
    time.sleep(3)
    print(x)
    cam.release()

I wonder about two things: - the default resolution of my webcam is higher than resolution that script is saving my picture with? - is there any command to check max resolution of webcam (I would like to set this res as default output)?

1 answer

  • answered 2018-07-18 20:00 Jeru Luke

    Actually there is way to check the default resolution of your webcam.

    • First initiate the webcam using cap = cv2.VideoCapture(0).
    • Then using the get() method, enter the following and on printing w and h you will get the width and height of your webcam:

      w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
      h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
      

    This is in response to your second question. Yes, you can alter the resolution of your webcam by choosing a width and height of your choice using the following set() method:

    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    

    Even though you change the width and height, the camera driver maintains an aspect ratio. So you may not always get the exact height/width you expect.

    Example: when I set the width and height to be 80 each. It did not set it. It rather set width = 320 and height = 180. Also when I set both to 800 it returned width = 848 and height = 480.

    The camera driver this maintains an aspect ratio.