10

I am using Video Input library to get frames from a webcam. I want to set FOCUS of this camera in C code.

Camera has AUTO FOCUS enabled. Isn't there a way to disable autofocus and set a specific focus value.

Regards, Saleh...

3
  • I know i can set focus in Video Input Library using following code; setVideoSettingCamera(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue = false); But it's not working.
    – Saleh
    Feb 8, 2012 at 10:34
  • The answer might be platform dependent. What OS/hardware are you using? Feb 8, 2012 at 15:31
  • 1
    windows... you can consider any of these: 7 64/32 or windows xp 32 ...
    – Saleh
    Feb 9, 2012 at 8:29

4 Answers 4

15

If you use the OpenCV 3.1.0-dev version and Python 2.7.5, the following code snipped should help you ;)

cap = cv2.VideoCapture(1) # my webcam
cap.set(3, 1280) # set the resolution
cap.set(4, 720)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0) # turn the autofocus off

With my Logitech HD Pro Webcam C920 is works fine. There are many other cool control functions inside cv2, like cv2.CAP_PROP_BRITHNESS or cv2.CAP_PROP_CONTRAST. Check out what the auto-complete is showing you ;)

5

Have you tried this: https://stackoverflow.com/a/1718009/7531 It requires you use directshow, but this should be possible.

Otherwise, have you looked at the OpenCV documentation for CameraCapture. the section concerning camera parameters shows how --in general- you control camera parameters, a quick look in videoinput.h should show you the parameters for autofocus. This is a complete example of setting autofocus this way.

IAMCameraControl is the windows interface for controlling parameters.

Note that depending on the webcam you use, and/or the specific firmware version it might not be possible to control focus / autofocus at all.

3
  • oh yes. I can disable it via the settings windows using mouse cursor in VideoInput Library. It means Camera drivers have support for enabling n disabling AutoFocus. but the problem is that its not configurable via coding. You've mentioned OpenCV's implementation for camera capture. In this implementation no property (Focus,Zoom,Brightness etc) is working. :(
    – Saleh
    Feb 10, 2012 at 12:36
  • @ saleh - How did you fix your problem? Jan 30, 2013 at 7:15
  • @Saleh, Please mention that how you fix the problem. thanks
    – Abc
    Sep 4, 2017 at 12:10
3

I have searched for this problems for couples of days and tried videoinput library and directshow. If you just want to set up the camera parameters for once (manually) inside opencv, the easiest way I found is:

VideoCapture cap(0);
cap.set(CV_CAP_PROP_SETTINGS, 1);

it will pop out a window for you to set the parameters. enough to disable autofocus.

The inconvenience of this method is that if you want to control the focal length by program, it cannot do that.

1
  • For newer versions you can use cap.set(cv2.CAP_PROP_SETTINGS, 1) Jan 4 at 22:28
1

The functionality you are looking for is camera and driver specific. The OpenCV camera property controls provide only very rudimentary parameters.
There is no generic way to make that call through OpenCV.

To do it, not through OpenCV, you can do do one of the following:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.