dverleysen
dverleysen

Reputation: 23

USB Microsoft webcam - over exposed image - code issue?

I have a Microsoft USB webcam connected to my Raspberry Pi 2B, but sometimes I've got a totally white picture and sometimes a bit of a normal picture. See image, it's like the picture is over exposed.

See image here: https://www.raspberrypi.org/forums/download/file.php?id=14467

It's something that can be fixed with some changes on the code? I've added already a kind of time before and after that the picture is taken, but that isn't a solution - time.sleep(t).

The code that I currently use

import os
import pygame, sys
from pygame.locals import *
import pygame.camera

width = 1280
height = 720

#initialise pygame
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))

cam.start()
#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,32)
pygame.display.set_caption('Camera')
#take a picture
time.sleep(4)
image = cam.get_image()
time.sleep(1)
cam.stop()

#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()

#save picture
pygame.image.save(windowSurfaceObj,'/home/pi/python/data/usbwebcamimage.jpg')

used usb cam: https://www.microsoft.com/hardware/en-gb/p/lifecam-cinema

Thanks for the feedback!

Upvotes: 2

Views: 661

Answers (1)

John Doe
John Doe

Reputation: 136

Honestly, I don't think that this is something that's able to be fixed by code, since you're not manipulating the image.

I looked at the camera you're using, and it uses auto-focus, so its possible that its focused on a darker area and compensates by making the image brighter. Look at something called auto-exposure: http://www.webopedia.com/TERM/A/automatic_exposure_mode.html

Can it be fixed by code? Possibly, depending on what you need the camera for.

If you don't need color, then using the following code should cause the exposure to go down a bit.

Code:

def histeq(im, nbr_bins=256):
    """ Histogram equalization of a grayscale image. """

    imhist, bins = np.histogram(im.flatten(), nbr_bins, normed=True)

    cdf = imhist.cumsum()
    cdf = 255 * cdf / cdf[-1]

    im2 = np.interp(im.flatten(), bins[:-1], cdf)

    return im2.reshape(im.shape), cdf


def lowerImage(image):

    image = np.array(Image.open(image).convert("F"))

    newimage, cdf = histeq(image)

    FinalImage = Image.fromarray(newimage).convert("RGB")

    FinalImage.save("equal.jpg")  # Change the name to whatever you want to

Before:

Your Image

After:

Output Image

What this is called is "histogram equalization", and what it does is it distributes the values on the histogram evenly. As you can see in the second image, you recover detail in the leaf, wires, and fencing.

While it does help a lot, it really can't do much if the entire photo is white. That's most likely the camera exposing for a darker area of the image.

If you do need color, poke around with numpy.array and scipy.misc.imsave to see if you can get the array with color.

To sum up:

  1. It's possible that its just hardware, so try to see if you can expose the camera manually.
  2. To help if the photo is over exposed, use the code above.

Upvotes: 0

Related Questions