OpenCV使用黑色边条捕获图像 [英] OpenCV capturing image with black side bars

查看:118
本文介绍了OpenCV使用黑色边条捕获图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cv2.VideoCapture和宽高比为16:9的相机捕获照片和视频. OpenCV返回的所有图像都有黑色的侧边栏,裁剪图像.在我的示例中,它没有返回1280 x 720像素的图像,而是返回了960 x 720图像. C920网络摄像头(1920 x 1080)也会发生同样的事情.

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

我在做什么错了?

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

使用OpenCV:

使用Windows摄像头:

Using Windows Camera:

推荐答案

@luismesas的答案是完全正确的,并且对我有用.

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

但是对于像我这样不熟练的人,您需要保存cv2.VideoCapture返回的捕获.不是像cv2.VideoCapture(cv2.CAP_DSHOW)这样可以设置的参数,而是一种方法.

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

已在网络摄像头设备HD PRO WEBCAM C920上确认.

Confirmed on Webcam device HD PRO WEBCAM C920.

这篇关于OpenCV使用黑色边条捕获图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆