I was wondering if someone ever tried to implement a rudimentary autofocus for the raspberry pi camera. From hardware point of view one needs some actuator, that is able to turn the little objective on the camera. From software point of view, one needs something, that is able to measure the overall contrast of the current image. Today I focued in the software part, and tried something with openCV, using histograms and shannon entropy. The higher the entropy of the image histogram, the more chaotic i.e. low-contrast the image turns out to be, and therefore out of focus... at least that's the idea. In practice I used a relatively sharp sample-image and blured it with different degrees. Expectation is, that the entropy increases with increasing blur. But the effect is not so great. The entropy doesnt change too much. That also depends on the image. I used the following one:


Probably the bad results with shannon entropy lie in the fact, that the majority of the image is already blurred. Therefore blurring out the vessels doesnt affect the overall entropy too much. But I will have to deal with these kind of images. This is why I am asking you for help. I know this question is more related to image processing, but I since I am using a raspberry pi camera, I want to know if someone here ever considered this problem?

If I ever manage to measure image sharpness robustly,do you think auto focusing is realizable for the raspi cam from hardware point of view?

By the way, I am working with the NoIR. The image above is a infrared lightbox.

3 Answers 3


The thing that all autofocus systems must do well is decide what to focus on. Trying to focus the whole image results in a negligible change in entropy, as you noted. However if you had a good idea of where it was important to focus (i.e. just the center, just certain points across the frame, etc) you would have a much more repeatable method. Observe the way autofocus works on other products: currently its common to attempt facial recognition and then improve focus in just the areas where faces are probably located. For pictures other than faces, some combination of image center and set points across the frame is used to prioritize the areas where focus should be optimized.

  • Problem with this approach is, that the objects( for example faces) that I want to focus on have to be somehow detected. And since the image may be blurred to begin with, detection, and thereby specification of regions of interest will be difficult. Jun 29, 2016 at 17:05
  • @AntonAlice right, deciding on what kicks off the algorithm is going to depend on what the camera is doing. Is it for security purposes? If so maybe a motion sensing first pass would be best to isolate the region where activity is and then focus there. Same for facial recog (even blurred images are often usable)
    – Jeff Meden
    Jun 29, 2016 at 17:27

You can use the picamera API and manually adjust values to get what you would like:

  • use specific keys to adjust a factor of the camera up/down or
  • Specify a key when all is set (like ENTER)
  • Also a good Idea to mess with the exposure time


Else, you will have to create a algorithm to adjust to specific pixels.

  • Also forgot to add, most of these definitions in the picamera API are bi-directional; you can set or obtain the values of the pi settings. His python API is great, but you must look at what he is wrapping and how that the base code focuses for better timing or/and better results: github.com/raspberrypi/userland/…
    – A-Square
    Jun 29, 2016 at 21:50

This would be a great (in several ways) project, but I think that a passive AF system using contrast measurement is not feasible with the kind of focus system possible with the Raspberry Pi camera.

So, in theory it could be done, but I'd say at a considerable development cost.

Look for an USB camera with autofokus.

  • why do you think its gonna be costly? The most intuitive idea is to use a gear glued on top of the objective. there are special glues used in model making, which are chemically agressive, and they can practically melt two plastics together. Jun 29, 2016 at 17:28
  • I was thinking in terms of software development cost, i.e. time.
    – jogco
    Jun 29, 2016 at 18:25
  • I can not use a webcam with autofocus, because I need 2 important features of the NoIR: 1. its has no IR filter. 2. I can capture at high exposure times (for low light conditions). Jun 30, 2016 at 0:01
  • Recently I took my old webcam and tried to remove the IR filter... after reassembling all the lenses the damn thing was completely out of focus. I havnt found out yet where the problem is, but I have just asked the manufacturer If he could send me a diagram of the lens system Jun 30, 2016 at 0:22
  • One possibly convenient hack would be to use a Foscam pan/tilt night vision webcam, and repurpose the pan or tilt stepper motor to do focus with a glued on gear. Image processing software in the Pi can then be used to fetch image and command the motor.
    – Jeff Meden
    Jun 30, 2016 at 16:12

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.