Python OpenCV access webcam maximum resolution

前端 未结 6 1593
栀梦
栀梦 2020-12-16 12:44

I am trying to acquire images from my webcam using a python code that imports OpenCV. The code is the following:

import sys
sys.path.append(\"C:\\\\opencv\\         


        
相关标签:
6条回答
  • 2020-12-16 13:05

    I have the same problem as you, that I cant get a higher resolution in opencv different "application". But with the Logitech Software I am able to record videos in 720p (camera C270 ). After some days of research, I came to the same explanation as @break that they restricted the resolution in the driver. I gave it up and will buy a different, non Logitech, one...

    0 讨论(0)
  • 2020-12-16 13:07

    I used the different resolutions to set image resolution from List of common resolutions by looping over

    def set_res(cap, x,y):
        cap.set(cv.CV_CAP_PROP_FRAME_WIDTH, int(x))
        cap.set(cv.CV_CAP_PROP_FRAME_HEIGHT, int(y))
        return str(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH)),str(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
    

    It seems that OpenCV or my camera allows only certain resolutions.

    • 160.0 x 120.0

    • 176.0 x 144.0

    • 320.0 x 240.0

    • 352.0 x 288.0

    • 640.0 x 480.0

    • 1024.0 x 768.0

    • 1280.0 x 1024.0

    0 讨论(0)
  • 2020-12-16 13:11

    The problem as mentioned above is caused by the camera driver. I was able to fix it using Direct Show as a backend. I read (sorry, but I do not remember where) that almost all cameras provide a driver that allows their use from DirectShow. Therefore, I used DirectShow in Windows to interact with the cameras and I was able to configure the resolution as I wanted and also get the native aspect ratio of my camera (16: 9). You can try this code to see if this works for you.

    import cv2
    
    cam = cv2.VideoCapture(0,cv2.CAP_DSHOW)
    
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
    
    r, frame = cam.read()
    ...
    print('Resolution: ' + str(frame.shape[0]) + ' x ' + str(frame.shape[1]))
    

    In the OpenCV documentation, I found the following information for those who want to know more about OpenCV backends (OpenCV docs)

    I hope this can help you!

    0 讨论(0)
  • 2020-12-16 13:12

    I got it to work, so this post is for others experiencing the same problem:

    I am running on the Logitech C270 as well. For some reason it would only show 640x480 even though the webcam supports 1280x720. Same issue persists with the built-in webcam in my laptop.

    If I set it to 800x600 in the code it shows 640x480. However, if I set it to 1024x768 it becomes 800x600. And if I set it to something silly like 2000x2000 it becomes 1280x720.

    This is in C++ on OpenCV 3.0, but perhaps it applies to Python as well.

    0 讨论(0)
  • 2020-12-16 13:21

    For cv2 just change to this.

        cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
    
    0 讨论(0)
  • 2020-12-16 13:21

    Try the following code to obtain the maximum camera resolution, using this you can capture your photos or video using maximum resolution:

    import cv2
    
    HIGH_VALUE = 10000
    WIDTH = HIGH_VALUE
    HEIGHT = HIGH_VALUE
    
    capture = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
    width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    print(width,height)
    
    0 讨论(0)
提交回复
热议问题