python opencv设置分辨率

 

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()

 

以下方法可以用来改变窗口的显示大小,但是不等于摄像头的分辨率。(不相信的伙伴可以自己试试)

cv2.namedWindow("video", 0)  # 0 即 cv2.WINDOW_NORMAL,表示可以自己调整窗口大小。注意:此“winname”参数应与后面的inshow()中一致。
cv2.resizeWindow("video", 960, 720)  # 修改窗口大小为960X720
 

你可能感兴趣的:(python宝典)