6
votes

I'm trying to capture photos and videos using cv2.VideoCapture and cameras with an aspect ratio of 16:9. All image is returned by OpenCV have black sidebars, cropping the image. In my example, instead of returning an image with 1280 x 720 pixels, it returns a 960 x 720 image. The same thing happens with a C920 webcam (1920 x 1080).

What am I doing wrong?

import cv2

video = cv2.VideoCapture(0)
video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while True:
    conected, frame = video.read()
    cv2.imshow("Video", frame)
    if cv2.waitKey(1) == ord('s'):
        video.release()
        break

cv2.destroyAllWindows()

Using OpenCV:

Image with black sides

Using Windows Camera:

enter image description here

4
maybe you should upgrade OpenCV version to 3 latestseokrae.kim
Im using OpenCV 3.4.2MarceloSouza
Maybe your camera isn't capable of 1280x720, not all cameras shoot all resolutions.Mark Setchell
When I use Sarxos library or use Windows Camera application, the image is shown correctly.MarceloSouza
I tried to change order, but didn't workMarceloSouza

4 Answers

8
votes

I had this exact issue with a Logitech wide angle in windows camera and I was wondering about a driver problem.

So I solved it using the DirectShow driver instead of the native driver using this:

cv2.VideoCapture(cv2.CAP_DSHOW)

If you have more than one camera add the index to that value like this

cv2.VideoCapture(cv2.CAP_DSHOW + camera_index)

It will accept the desired resolution by applying the right aspect ratio without having the sidebars.

5
votes

The Answer of @luismesas is completely right and worked for me.

But for people being as unskilled as I am you need to save the capture returned by cv2.VideoCapture. It is not a Parameter you can set like cv2.VideoCapture(cv2.CAP_DSHOW), it is a method.

camera_index = 0
cap = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW)
ret, frame = cap.read()

Confirmed on Webcam device HD PRO WEBCAM C920.

2
votes

I had the same issue too, but only on Windows 10, OpenCV 3.4 and Python 3.7. I get the full resolution without the black side bars on a Mac OS.

I used PyGame to capture the webcam input and got the full resolution of 1920x1080 on Windows.

-1
votes

Just resize the received frame:

   cv::Mat dst;
   cv:resize(frame,dst,cv::Size(1280,720));
   cv::imshow("Video",dst);

Check it!