7

I have a Logitech C920 hooked to my PC and am trying to use it to click pictures using OpenCV.

I know that I can capture images using :

cam = cv2.VideoCapture(1)
s, im = cam.read() # captures image
cv2.imshow("Test Picture", im) # displays captured image
cv2.imwrite("test.bmp",im) # writes image test.bmp to disk

But it get me the 15MP still photography that my camera is capable of. What I get from above is far inferior to what I can expect if I click pictures.

So, is there a way to take pictures (like it can be done in the official WebCam software) ?

Darshan Chaudhary
  • 2,093
  • 3
  • 23
  • 42
  • Hi, have you seen this [previous answer](http://stackoverflow.com/q/11420748/5066845)? If your camera is compatible, you will be able to adjust the settings to create a larger/clearer picture. I'll post a sample program to read out the current settings of the camera and an example of how to set the camera parameters. – Steven Correia Oct 05 '15 at 07:40

1 Answers1

8

After testing with this previous question as a guide, I wrote the following program to check my camera's settings (Logitech S7500 USB Webcam). I am using Python 2.7 and OpenCV 2.4.6 on Windows 7.

Hopefully you can use this code as a guide to modify the setting on your camera to get the resolution (or close to it) you need (using the set() function from OpenCV.)

If you get all 0.0 as the values for each camera setting, then you may not have the ability to adjust the camera settings. I experienced this when running this program on my laptop's built-in webcam.

from __future__ import print_function
import sys
import cv2


def main(argv):
    #capture from camera at location 0
    cap = cv2.VideoCapture(0)
    # Change the camera setting using the set() function
    # cap.set(cv2.cv.CV_CAP_PROP_EXPOSURE, -6.0)
    # cap.set(cv2.cv.CV_CAP_PROP_GAIN, 4.0)
    # cap.set(cv2.cv.CV_CAP_PROP_BRIGHTNESS, 144.0)
    # cap.set(cv2.cv.CV_CAP_PROP_CONTRAST, 27.0)
    # cap.set(cv2.cv.CV_CAP_PROP_HUE, 13.0) # 13.0
    # cap.set(cv2.cv.CV_CAP_PROP_SATURATION, 28.0)
    # Read the current setting from the camera
    test = cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC)
    ratio = cap.get(cv2.cv.CV_CAP_PROP_POS_AVI_RATIO)
    frame_rate = cap.get(cv2.cv.CV_CAP_PROP_FPS)
    width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
    brightness = cap.get(cv2.cv.CV_CAP_PROP_BRIGHTNESS)
    contrast = cap.get(cv2.cv.CV_CAP_PROP_CONTRAST)
    saturation = cap.get(cv2.cv.CV_CAP_PROP_SATURATION)
    hue = cap.get(cv2.cv.CV_CAP_PROP_HUE)
    gain = cap.get(cv2.cv.CV_CAP_PROP_GAIN)
    exposure = cap.get(cv2.cv.CV_CAP_PROP_EXPOSURE)
    print("Test: ", test)
    print("Ratio: ", ratio)
    print("Frame Rate: ", frame_rate)
    print("Height: ", height)
    print("Width: ", width)
    print("Brightness: ", brightness)
    print("Contrast: ", contrast)
    print("Saturation: ", saturation)
    print("Hue: ", hue)
    print("Gain: ", gain)
    print("Exposure: ", exposure)
    while True:
        ret, img = cap.read()
        cv2.imshow("input", img)

        key = cv2.waitKey(10)
        if key == 27:
            break

    cv2.destroyAllWindows()
    cv2.VideoCapture(0).release()


#   0  CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
#   1  CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
#   2  CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
#   3  CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
#   4  CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
#   5  CV_CAP_PROP_FPS Frame rate.
#   6  CV_CAP_PROP_FOURCC 4-character code of codec.
#   7  CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
#   8  CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
#   9 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
#   10 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
#   11 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
#   12 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
#   13 CV_CAP_PROP_HUE Hue of the image (only for cameras).
#   14 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
#   15 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
#   16 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
#   17 CV_CAP_PROP_WHITE_BALANCE Currently unsupported
#   18 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

if __name__ == '__main__':
    main(sys.argv)

Here is a link to an OpenCV question about this topic and the OpenCV Documentation

Community
  • 1
  • 1
Steven Correia
  • 461
  • 10
  • 18
  • I am able to record full HD video and pull images off it, that is sorted. What I wished for was a more natural way to click pictures. I hoped for something like : `img = cv2.ClickImage(1, quality='15MP')` – Darshan Chaudhary Oct 05 '15 at 08:43
  • 1
    You'll probably have to check the OpenCV documentation, this is the best way (and simplest) that I've found that works for me. – Steven Correia Oct 05 '15 at 08:59
  • I get `AttributeError: 'module' object has no attribute 'cv'` for `cv2.cv` – Martin Thoma Jul 23 '17 at 19:16
  • Hi, which version of opeCV are you using? Thing have changed for OpenCV 3. If you have a newer version, this might help -> https://stackoverflow.com/a/33199152/5066845 – Steven Correia Jul 27 '17 at 08:47