Skip to content
Advertisement

Get In Focus Pixels of an Image

How to detect which pixels of an image are in focus compared to the blurry ones. Something like the ‘Focus Peaking’ feature lots of cameras have?

The idea is to color the pixels that are in focus so that it assists the user while clicking a picture. Looking for an implementation through Python.

Advertisement

Answer

You can find the edges, which are sharp or high contrast and then superimpose them onto the original image.

So, starting with this image:

enter image description here Credit: Rita Kochmarjova – Fotolia

You can do:

JavaScript

enter image description here

You can see the effect most clearly in the stones at water’s edge.

Here is the intermediate edges.png. You could dilate the white pixels somewhat, or threshold to make the in-focus parts more heavily defined.

enter image description here


Here it is with the edges dilated a little to make them more obvious:

JavaScript

enter image description here


You can also do it in Terminal with ImageMagick without writing any code:

JavaScript

enter image description here

Or this, which is more similar to the Python:

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement