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...
Click on 'Webcam settings' link in blue letters. Click on the Camera Control tab. Uncheck the 'Auto' box next to the Focus setting.
Check the outer casing of your cam for a manual focus ring. This plastic ring is usually set within the frame of the cam. Turning the ring adjusts the focus, just like on a microscope or a camera lens.
OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2. imwrite() which saves the image to a specified file location. But, for saving a recorded video, we create a Video Writer object.
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 ;)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With