For a research project it would be handy to get only the in focus part of an image. Does anyone know some software that can do this? I know that the next version (in a few weeks) of Photoshop CC will have something like it, but are there other options?

  • \$\begingroup\$ FYI, your use of "get" is very ambiguous here. What do you mean by "get only this part" of the image? Do you mean that's the only pixels you want remaining in the file? Or do you simply want to identify this area for some kind of analysis or what? "Select" would be a better term, or "mask" or even "identify" \$\endgroup\$
    – Jasmine
    Jun 13, 2014 at 20:12
  • \$\begingroup\$ If you have more than one image, (i.e. similar to focus stacking but without actual stacking), I'd have an answer that would give you in-focus masks. \$\endgroup\$
    – Unapiedra
    Jun 14, 2014 at 9:26
  • \$\begingroup\$ Good point @Jasmine. I have branches of a shrub in focus against an background trees/shrubs (putting up a blue-key isn't an option), so to reduce the error I want to limit the analysis to the branches. This can be done by masking, or by have an reduced file with only the in-focus pixels. \$\endgroup\$
    – LaurensP
    Jun 14, 2014 at 20:23
  • \$\begingroup\$ @Unapiedra, it's the first time hear about focus stacking. How would that method or derivative give me an in-focus mask? I'll have several pictures a day over a month, fixed frame and focal distance. \$\endgroup\$
    – LaurensP
    Jun 14, 2014 at 20:24
  • \$\begingroup\$ @LaurensP: It wouldn't help in this case. I strongly suspect that your question is wrong here: It is a programming question and should be moved to StackOverflow. You could also expand your question to say what you actually want to do/analyse/etc. So far you revealed that you want to analyse branches of a shrub, how and why you didn't say. Maybe clustering and segmentation is an option? Please upload a sample image, while you're at it. \$\endgroup\$
    – Unapiedra
    Jun 15, 2014 at 8:04

3 Answers 3


I normally use GIMP for that --- this probably needs to be refined for automatic procedures. My procedure is as follow:

  1. 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

  2. Now I copy the background layer, and I transform the new layer to grayscale.

  3. I apply edge-detect by difference of gaussian:

enter image description here

playing with the parameters to have a nice area defined. Apply the filter

  1. Now you apply a threshold to the image:

enter image description here

...and this is a map of your sharp (how much sharp you decide in the previous step setting the parameters for the filters) are.

  1. You can now use the "select by color tools", click on the black, and then grow the selection a bit:

enter image description here

... 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

  1. manually edit the layer to fill the sharp areas with black:

enter image description here

...and the repeat the select-by-color procedure to create a new mask.

  • \$\begingroup\$ This will give you textured areas (with edges). Your use of growing is clever but the result isn't perfect. Look at the inside of the leaf on the left hand side. It is in focus, but not completely black in the mask. I think this might actually be impossible to solve as one can't distinguish between out-of-focus and texture-less. \$\endgroup\$
    – Unapiedra
    Jun 14, 2014 at 8:55

In Hugin you can use a tool that does it. It is called enfuse.

Problem: It only works on multiple images.

Here you can find a tutorial on using enfuse for focus-stacking. You can get the masks (which is what you want) by using --save-masks.

enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 \
       --hard-mask --save-masks --output=base.tif INPUTImage*.tif

However, note that while this is automated, the basic approach is the same as Rmano's answer: Detect large local contrast. For much more in-detail explanation have a look at Local Contrast Detection and Advanced Focus Stacking in the Enfuse documentation.


Adobe Photoshop CC will do this after June 18th. Here's the link to the preview feature

  • 5
    \$\begingroup\$ The question is specifically asking for options other than Photoshop :-) \$\endgroup\$
    – Philip Kendall
    Jun 13, 2014 at 16:00
  • \$\begingroup\$ It didn't initially \$\endgroup\$ Jun 13, 2014 at 21:13
  • 1
    \$\begingroup\$ It's never been edited. \$\endgroup\$
    – Joanne C
    Jun 13, 2014 at 22:22
  • \$\begingroup\$ Oh well. Let's keep down voting I guess. we're at -4 now. Anyone else? \$\endgroup\$ Jun 14, 2014 at 5:13

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.