How can I automatically create a mask of the in-focus areas in an image?
Asked 6/13/2014
3 views
2 answers
0
I need a way to isolate or mask only the sharp, in-focus parts of a photo for a research project. Photoshop CC is adding a feature for this, but are there other software options or workflows that can do it automatically or semi-automatically?
Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0
Photography Stack Exchange contributor
12y ago
2 Answers
5
I normally use GIMP for that --- this probably needs to be refined for automatic procedures. My procedure is as follow:
I load the image, in this case http://rlog.rgtti.com/wp-content/uploads/2013/01/10/macro-lenses-are-expensive-all-of-them/paseo_15sep_more_rosse.jpg
Now I copy the background layer, and I transform the new layer to grayscale.
I apply edge-detect by difference of gaussian:

playing with the parameters to have a nice area defined. Apply the filter
- Now you apply a threshold to the image:

...and this is a map of your sharp (how much sharp you decide in the previous step setting the parameters for the filters) are.
- You can now use the "select by color tools", click on the black, and then grow the selection a bit:

... once you are satisfied, you can save the selection to a channel.
Notice that the selection do not include the sharp areas which are uniformly colored, without edges. I do not know any way of doing this automatically (is it possible?); but you can copy the channel to a new layer and
- manually edit the layer to fill the sharp areas with black:

...and the repeat the select-by-color procedure to create a new mask.
Originally by user24174. Source · Licensed CC BY-SA 4.0
user24174
12y ago
0
Generated from our catalog & community — verify before relying on it.
Yes—this is possible, but it’s usually based on detecting local contrast/edge sharpness, so results may need cleanup.
A workable option is GIMP: duplicate the image layer, convert it to grayscale, run an edge-detection filter such as Difference of Gaussians, then apply a threshold to create a sharpness map. From that map, use color selection and optionally grow/refine the selection to build a mask of the in-focus regions.
If you have multiple images of the same subject focused at different distances, Hugin’s Enfuse can help. It’s mainly for focus stacking, but it can save the masks it generates using --save-masks. That gives you the areas it considers most in focus. This only applies to multi-image workflows.
So in short:
- For a single image: use an edge/local-contrast method in GIMP or similar software.
- For multiple focus-bracketed images: use Enfuse and save the masks.
Photoshop CC also adds a built-in feature for this, but the underlying idea is similar: identify sharper, higher-contrast regions.
Recommended products
UniqueBot
AI12y ago
Your Answer
Related Questions
Why does the Eraser leave partial transparency on a Photoshop layer mask?
How can I keep several areas in focus while other parts stay blurred?
Can existing out-of-focus areas be detected and selectively intensified in post-processing?
Is Portraiture still worth buying if I already have Lightroom and Photoshop AI retouching tools?
How can I make a skin-tone mask for skin smoothing in Photoshop?