OpenCV 3 Python blur detection


logoIn order to detect if picture is in focus modern consumer cameras use complicated phase detection circuitry and specialized sensors. But how to determine if captured picture is in focus after it has been taken. Having this measurement information can help in many ways (select best picture in a sequence, control motorized lens, clean time-lapse video and many other).

In our case Laplacian transformation, while not being perfect solution, can distinguish between same scene focused and blurred frames. While it is hard to describe what Lapliacian function does, you can always read more details on Wikipedia pages.

Again I will be using OpenCV to solve this problem. Let’s capture short video clip and run script to see the results. Script is in a way that displays video file with quality bar at the bottom and saves text file with numerical result for further analysis.


Note red bar at the bottom indicating focus quality

Whole clip analysis indicated that function can distinguish if in focus very precisely. Unfortunately has hard times determining blur level in extreme conditions.

To display focus/blur distribution over time I used LibreOffice graph function. Below is visual blur over frame nr representation.


Focus quality distribution vs time


Comments ( 2 )

  1. Valentin
    Interesting approach using variance as a measurement of focus-indicator. However, it works only in some cases. It reflects the nature of blureness only by chance. Our perception of the sharpnes is based on edges, not the histogram or other spread-functions. Besides, finding the "sharpnes"-funciton is only one small and the easiest step. Easiest, because can be simulated and analysed using the test data. On real optical systems there are many other factors: - Shaking - Reflections - Positioning errors - Changing lightening conditions while focusing (consider fluorescent objective, there the light beam changes together with the objective movement) - Controlling exposure while focusing - Moving object: attach at leaset 20x objective, and the up/down movement will also result in x/y movement, e.g because objective/probe are not exact perpendicular. - Last but not the least - context. Focusing with higher magnification on 3d objects (practicaly all natural probes) will gain in many well focused levels - e.g. glass Saying all that, a good focusing algorithm can't be implemented without ai.
    • saulius
      Hi Valentin, thank you for commenting on this topic. Laplacian transformation is really basic function to evaluate image contrast. It would be interesting to use AI for other feature detection but I see it's limitation and lack of algorithmic transparency.

Leave a reply

Your email address will not be published.