2

I have a python program which uses opencv VideoCapture to capture webcam image (in my case logitech c922). It has autofocus feature which is great but I don't know when the refocus is done and that makes the image that I capture blurred (not focus yet)

Is there any way to know when the camera already focusses?

Abhishek Arya
  • 450
  • 4
  • 16
  • You should probably search for a library which could interact with this specific camera, Only that library can give you a finer control as to what camera hardware is doing, OpenCV has no such functionality to extract the data from the camera. – ZdaR Aug 08 '18 at 05:38

2 Answers2

1

Besides interacting with camera hardware that @ZdaR has mentioned, you can determine whether the image is sharp or not every frame. If the image is sharp, most probably the camera is in focus.

There are some great answers here on determining the sharpness of an image.

In the case of having a depth-of-view (the object is sharp while the background is blurry), you can set the threshold on some of the sharpest pixels only (i.e. sharpest 20% pixels). Since a out-of-focus or focusing image should be blurry altogether.

hkchengrex
  • 4,361
  • 23
  • 33
  • i already tried it. but if the image is to sparse its return as blur. – Muhammad Salman Galileo Aug 08 '18 at 05:53
  • @MuhammadSalmanGalileo What do you mean by sparse? It would be great if you can add that (that you have tried this method and the result that you get) in your question too so that others can help. – hkchengrex Aug 08 '18 at 05:55
  • if i tried to do that to white only image (sparse), its return 0. its also happend when the image is mostly the same thing like flat wall (its return small number). i use this example https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/ – Muhammad Salman Galileo Aug 08 '18 at 06:13
0

You can set the focus manually so that the camera is focused already when you need to use the camera.

Here is the code:

cap.set(3, 1280) # set the resolution
cap.set(4, 720)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
hypadr1v3
  • 543
  • 4
  • 26