This is the 21st day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article shows you how to set camera resolution using OpenCV in Python3.

Setting the Camera Resolution

Usually when Opencv is called in Python3, after the VideoCapture object is created and the camera is successfully opened, you can use the function video.get(propId) to get some parameter information about the video.

Here propId can be any integer between 0 and 18. Each number represents an attribute of the video, as detailed in the following table.

methods explain
cv2.VideoCapture.get(0) The current position (play) of the video file is in milliseconds
cv2.VideoCapture.get(1) Index based on captured or decoded frames starting with 0
cv2.VideoCapture.get(2) Relative position of the video file (playing) : 0= beginning of the movie, 1= end of the movie.
cv2.VideoCapture.get(3) The width of the frame in the video stream
cv2.VideoCapture.get(4) The height of frames in the video stream
cv2.VideoCapture.get(5) Frame rate
cv2.VideoCapture.get(6) Codec 4 – character code
cv2.VideoCapture.get(7) Number of frames in a video file
cv2.VideoCapture.get(8) Returns the format of the object
cv2.VideoCapture.get(9) Returns a back-end specific value indicating the current capture mode
cv2.VideoCapture.get(10) Image brightness (camera only)
cv2.VideoCapture.get(11) Image contrast (camera only)
cv2.VideoCapture.get(12) Image saturation (camera only)
cv2.VideoCapture.get(13) Tonal image (camera only)
cv2.VideoCapture.get(14) Image Gain (camera only) (Gain in photography means increased white balance)
cv2.VideoCapture.get(15) Exposure (camera only)
cv2.VideoCapture.get(16) Indicates whether the image should be converted to RGB Boolean flags
cv2.VideoCapture.get(17) × Temporarily not supported
cv2.VideoCapture.get(18) Correction labeling for stereo cameras (currently only DC1394 V.2.x back end supports this function)

The counterpart of the get method is the set method, so some of the values can be modified using video.set(propId, value).

Value is the new value that you want to set to.

To check the resolution of the camera, you can use the video.shape function, where the first two values are the height and width values respectively.

By default, the resolution of the camera is 640X480, which can be changed using the set method.

As to 320 x240:

video.set(3.320), video(4.240).Copy the code

Code case

Here is an example of setting the camera resolution:

import cv2

if __name__ == "__main__":
    webcam = cv2.VideoCapture(0)
    
    if not webcam.isOpened():
        print("can't open the camera!!!")
    # cv2.namedWindow("video".0)
    # cv2.resizeWindow("video".960.720)
    # method 1:
    webcam.set(3.1920)  # width=1920
    webcam.set(4.1080)  # height=1080
    # method 2:
    # webcam.set(cv2.CAP_PROP_FRAME_WIDTH, 960)
    # webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
    while True:
        ret, frame = webcam.read()
        print(frame.shape[:2])  # just need the first two values.
        cv2.imshow("video", frame)
        # Hit 'q' on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord('q') :break

    # Release handle to the webcam
    webcam.release()
    cv2.destroyAllWindows()
Copy the code

The following methods can be used to change the display size of the window, but not equal to the resolution of the camera.

cv2.namedWindow("video".0)
cv2.resizeWindow("video".960.720)
Copy the code

A series of articles will follow this month,

Wonderful article, please pay attention.