Determine if an image is in focus using OpenCV

Zahid Parvez
4 min readFeb 3, 2023

Determining if an image is tricky and it isn't as straight forward as using the variance from the Laplacian function...

Consider the following images, which images do you consider blurry?

Disclaimer — images 2 to 4 was generated with thispersondoesnotexist.com

These images are an interesting example as each presents a different problem in the realm of computer vision.

  • Image 1 — As a photographer, I don't consider the image blurry, the background of the image out of focus however the subject is in sharp focus. You can observe this in images 2 as well.
  • Image 2 — the subject of the image is sharp but there is a blurry background
  • Image 3 — the overall image is slightly blurry (compared to image 2), however, the image is salvageable using basic processing.
  • Image 4 — no part of this image is sharp
  • Image 5 — This is a closeup picture of a blanket. is the image sharp? there are no defining edges on this image that can be used to determine if the image is sharp.

All of these images, apart from #2, present a problem to the Laplacian function due to depth of field and soft images. As such, some preprocessing should be done before the Laplacian function is used to reduce…

--

--

Zahid Parvez

I am an analyst with a passion for data, software, and integration. In my free time, I also like to dabble in design, photography, and philosophy.