Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are webcam images taken with Python so dark?

Tags:

python

opencv

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.

enter image description here

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.

like image 446
Martin Thoma Avatar asked Feb 17 '15 17:02

Martin Thoma


3 Answers

Faced the same problem. I tried this and it works.

import cv2
camera_port = 0 
ramp_frames = 30 
camera = cv2.VideoCapture(camera_port)
def get_image():
 retval, im = camera.read()
 return im 
for i in xrange(ramp_frames):
 temp = camera.read()

camera_capture = get_image()
filename = "image.jpg"
cv2.imwrite(filename,camera_capture)
del(camera)

I think it's about adjusting the camera to light. The former former and later images

like image 99
Keerthana Gopalakrishnan Avatar answered Sep 18 '22 08:09

Keerthana Gopalakrishnan


Tidying up Keerthana's answer results in my code looking like this

import cv2
import time

def main():
    capture = capture_write()


def capture_write(filename="image.jpeg", port=0, ramp_frames=30, x=1280, y=720):
    camera = cv2.VideoCapture(port)

    # Set Resolution
    camera.set(3, x)
    camera.set(4, y)

    # Adjust camera lighting
    for i in range(ramp_frames):
        temp = camera.read()
    retval, im = camera.read()
    cv2.imwrite(filename,im)
    del(camera)
    return True

if __name__ == '__main__':
    main()
like image 20
Julian Wise Avatar answered Sep 19 '22 08:09

Julian Wise


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.

like image 21
Lucas Alonso Avatar answered Sep 21 '22 08:09

Lucas Alonso