Opencv4.1 + Windows10 + Python 3.7.5 cv2.VideoCapture does not display correctly

Symptoms

environment

Opencv

VideoCapture.py


import cv2
 
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
 
while True:
    ret, frame = camera.read()
    cv2.imshow('camera', frame)
 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.imwrite("capout.jpg ", frame)
        break
 
camera.release()
cv2.destroyAllWindows()

capout.jpg

Windows standard camera

WIN_20191122_16_41_19_Pro.jpg
With the Windows standard camera, when 1080p is selected, the image is normally output from the webcam, but when using Opencv's Video capture, both sides are filled with black as shown in the reference image, and the resolution itself is also extended. I feel the roughness. The frame rate does not increase even though the camera can shoot 60fps movies at 1080p. (The set value is reflected in the get function of VideoCapture, but when the frame rate is actually measured, it peaks at 30 fps.)

solution

A similar phenomenon was reported on the following site, so I tried the solution and solved it. OpenCV capturing imagem with black side bars

VideoCapture2.py


import cv2
#Change streaming to DSHOW
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
 
while True:
    ret, frame = camera.read()
    cv2.imshow('camera', frame)
 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.imwrite("capout.jpg ", frame)
        break
 
camera.release()
cv2.destroyAllWindows()

Cause

I don't know if the direct cause is bad Windows or a problem with the version of Opencv, but I was able to solve it by using a streaming format called DirectSHOW. Please refer to the explanation about DirectSHOW below. qiita_directshow Until now, I have used Opencv's Video capture normally, but until now (another environment) there was no problem with the default streaming, so the question remains. In addition, if we can confirm options that cannot be used or significant delays due to this change, we will add them.

Recommended Posts

Opencv4.1 + Windows10 + Python 3.7.5 cv2.VideoCapture does not display correctly
Virtualenv does not work on Python3.5 (Windows)
Python version does not switch
Notes for using OpenCV on Windows10 Python 3.8.3.
[VScode] autopep8 format does not work [Python]
Python> Python does not include the last offset
tensorflow does not enter in windows + anaconda.
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Jinja2 2.9.6 does not work on Lambda Python 3 series
[Python] How to install OpenCV on Anaconda [Windows]
[Python] Expression (1,2) does not make tuples with parentheses
python Boolean operation does not return a Boolean value
Python2.7 + CentOS7 + OpenCV3
Python on Windows
python windows environment
Python installation (Windows)
OpenCV Samples (Python)
[Note] openCV + python
[Note] Python's OpenCV does not have C ++ copyTo, Clone
[Python] Boolean operator (or / and) does not return Boolean value
Key input that does not wait for key input in Python
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Display USB camera video with Python OpenCV with Raspberry Pi
Python OpenCV tried to display the image in text.