为什么用Python拍摄的网络摄像头图像如此暗? [英] Why are webcam images taken with Python so dark?

查看:195
本文介绍了为什么用Python拍摄的网络摄像头图像如此暗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以各种方式展示了如何使用Python使用网络摄像头拍摄图像(请参阅如何使用Python拍摄相机图像? ).您可以看到,使用Python拍摄的图像比使用JavaScript拍摄的图像要暗得多.怎么了?

图片示例

左图是使用 http://martin-thoma.com/html5/webcam拍摄的/,右侧是带有以下Python代码的代码.两者都是在相同(受控)的雷电情况下拍摄的(外面是黑暗的,我只有一些电灯亮着),并且使用了相同的网络摄像头.

代码示例

import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
return_value, image = camera.read()
cv2.imwrite("opencv.png", image)
del(camera)  # so that others can use the camera as soon as possible

问题

为什么用Python拍摄的图像比用JavaScript拍摄的图像要暗得多,我该如何解决?

(获得类似的图像质量;仅使其更亮可能无法解决它.)

如何解决"的注意事项:不需要是opencv.如果您知道可以使用Python 另一个包(或没有包)也可以.

解决方案

我认为您必须等待相机准备就绪.

此代码对我有用:

from SimpleCV import Camera
import time
cam = Camera()
time.sleep(3)
img = cam.getImage()
img.save("simplecv.png")

我从这个答案中得到了这个主意我发现的最令人信服的解释:

前几帧在某些设备上较暗,因为这是第一帧 初始化相机后,可能会拉出 几帧,以便相机有时间调整亮度 自动地. 参考

因此,恕我直言,为了确保图像质量,无论使用哪种编程语言,在照相设备启动时都必须等待几秒钟和/或丢弃几帧,然后再拍摄图像. /p>

I've showed in various ways how to take images with a webcam in Python (see How can I take camera images with Python?). You can see that the images taken with Python are considerably darker than images taken with JavaScript. What is wrong?

Image example

The image on the left was taken with http://martin-thoma.com/html5/webcam/, the one on the right with the following Python code. Both were taken with the same (controlled) lightning situation (it was dark outside and I only had some electrical lights on) and the same webcam.

Code example

import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
return_value, image = camera.read()
cv2.imwrite("opencv.png", image)
del(camera)  # so that others can use the camera as soon as possible

Question

Why is the image taken with Python image considerably darker than the one taken with JavaScript and how do I fix it?

(Getting a similar image quality; simply making it brighter will probably not fix it.)

Note to the "how do I fix it": It does not need to be opencv. If you know a possibility to take webcam images with Python with another package (or without a package) that is also ok.

解决方案

I think that you have to wait for the camera to be ready.

This code works for me:

from SimpleCV import Camera
import time
cam = Camera()
time.sleep(3)
img = cam.getImage()
img.save("simplecv.png")

I took the idea from this answer and this is the most convincing explanation I found:

The first few frames are dark on some devices because it's the first frame after initializing the camera and it may be required to pull a few frames so that the camera has time to adjust brightness automatically. reference

So IMHO in order to be sure about the quality of the image, regardless of the programming language, at the startup of a camera device is necessary to wait a few seconds and/or discard a few frames before taking an image.

这篇关于为什么用Python拍摄的网络摄像头图像如此暗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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