Search code examples
pythonopencvimage-processingcamera

How can I test the actual resolution of my camera when I acquire a frame using OpenCV?


I am working in Python/OpenCV, acquiring frames from a USB webcam (Logitech C615 Camera, supposedly HD 1080p). 1080p has a 16:9 aspect ratio and thus I should be able to acquire images at all of these resolutions:

1920 x 1080
1600 x 900
1366 x 768
1280 x 720
1024 x 576

I didn't write the camera driver however, so how do I know if I am really getting these pixels off of the camera? For example, I can specify 3840 x 2160 and I get a video frame of that size!

Is there a systematic way I can evaluate/determine the real resolution or effective resolution of the camera given these different resolution settings? Below is some Python/OpenCV code to demonstrate.

import numpy as np
import cv2, cv
import time

cap = cv2.VideoCapture(0) # note you may need to pass 1 instead of 0 into this to get your camera  

cap.set(3,3840)     #horizontal pixels
cap.set(4,2160)     #vertical pixels
cap.set(5, 15)      #frame rate
time.sleep(2)       #trying to solve a delay issue ... never mind this


#acquire the video from the camera
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow("captured video", frame)
    if cv2.waitKey(33) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Solution

  • import cv2
    cam = cv2.VideoCapture(0)
    w = cam.get(cv2.CAP_PROP_FRAME_WIDTH)
    h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
    print(w, h)
    while cam.isOpened():
        err, img = cam.read()
        cv2.imshow("lalala", img)
        k = cv2.waitKey(10) & 0xff
        if k == 27:
            break