I was wondering if there was any photo management software out there that could auto detect and "rate" images on how in focus they are. This comes up for me quite commonly when i am looking through a large series of macro images of the same subject.
In a perfect world, Light Room (or whatever program) could highlight in focus and out of focus areas the same way that it detects burned out areas of an image. Also the algorithm I am hoping exists would be smart and would not be distracted by blurred backgrounds and only seeks that some sufficient area is in enough focus.
Anything like that out there?

As another thought for people talking about pictures being in focus on the wrong spot, and that camera focus models already look into this. Another large factor for a lack of clarity in a macro image is blur from camera movement. This damages all points on an image, and is not something the camera accounted for when focusing.

  • \$\begingroup\$ Which camera are you using? Some cameras include the focus point information in their RAW files and EXIF data, with a little effort, it wouldn't be too difficult to use that information to gauge the sharpness and contrast to determine a rating of focus. I don't know anything that does this though... \$\endgroup\$ Commented Jan 20, 2011 at 22:45
  • 2
    \$\begingroup\$ @Benjamin Anderson - this is only a partial solution. Usually you focus then recompose, so the focus point indication will actually be misleading in this case and will rate many perfect images as out-of-focus \$\endgroup\$
    – ysap
    Commented Jan 20, 2011 at 22:47
  • \$\begingroup\$ @ysap - True, especially when it comes to macro shots. \$\endgroup\$ Commented Jan 20, 2011 at 22:50
  • \$\begingroup\$ It would be even better if you could more clearly see what is in focus before taking a picture. For example, live view could use colour coding to clearly highlight in-focus and out-of-focus areas. I wonder if there are any cameras that can do that... \$\endgroup\$ Commented Jan 20, 2011 at 23:38
  • 1
    \$\begingroup\$ @Jukka what you're asking for is called colour peaking and it's found on high end digital video cameras (which are often manually focussed). I believe you can get it on the Canon 5D mkII via the Magic Lantern firmware hack. \$\endgroup\$
    – Matt Grum
    Commented Jan 21, 2011 at 12:11

8 Answers 8


It's an interesting question. It's certainly possible for software to detect the parts of an image that is in focus, as it's the basis for focus-stacking software like Helicon Focus.

Focus stacking is a technique used by macro photographers. The depth of field in many macro shots is very shallow, so to extend this it's possible to take a set of photos of the subject, modifying the focal point in each one. Helicon Focus takes the stack of photos and detects the most in-focus parts of each image, and blends them together to produce a result where the entire subject is in focus. It's also possible to do this with some of the technology behind Hugin, but it's a bit more tricky to set up.

I think the difficulty in extending this for a general "in focus" check would be determining whether the subject is in focus - how do we automatically determine what the subject is? How much depth of field was required by the photographer?

  • 3
    \$\begingroup\$ I think the need for control over the camera's autofocus system demonstrates that it's impossible to know, without the photographer's input, what the intent is. There's also a sort of Godel problem, where I can take an out-of-focus picture on the wall, and take a picture of it, that is itself correctly focused. It would be impossible to identify the latter as being correct. \$\endgroup\$ Commented Apr 11, 2012 at 13:58

What you are asking for is problematic in the sense that a software based focus detection will essentially use the same algorithm as a contrast based camera autofocus system uses. That means that you need to scan the image and look for the most contrasty place to determine the amount of "focusness". However, it is possible you were shooting a low-detail (low-contrast) subject, so even though your focus is spot-on your target, still the software algorithm will determine a low focus or out-of-focus image.

Additionally, how will the software know what was your actual intended focus point? If you're shooting a portrait, and the eyes are mis-focused, the software will detect perfect focus on the ear, but this really is a low-quality or unusable image.

  • 1
    \$\begingroup\$ I'm starting to see how it's possible to design such a program. For faces, well, do a facial recognition type program, look at the eyes and see if they are in focus. Something should be possible for this, although it might be somewhat difficult... \$\endgroup\$ Commented Jan 20, 2011 at 22:55
  • \$\begingroup\$ Well, the proposed technique (highlighting areas that seemed to be in focus) would not have the problem of ear vs. eye. I'd guess that seeing which areas are relatively contrasty vs. not contrasty would be similarly interpretable - and if you're concerned with a series of similar images, then comparing them to each other rather than an absolute "focusness" quality seems like the right way to do it. \$\endgroup\$
    – Reid
    Commented Jan 21, 2011 at 14:12
  • \$\begingroup\$ @Reid - I assume the intent of the OP is to have an automated process for removing bad pictures. My answer addresses this requirement by pointing out why this feature will be hard to accomplish, and I also gave examples in comments to other answers. If there is a human in the loop, then everything is possible... \$\endgroup\$
    – ysap
    Commented Jan 21, 2011 at 16:52
  • \$\begingroup\$ My reading of the question is that the OP does seem to be pretty clearly describing a mixed initiative process, not a fully automated one (e.g., "highlight in focus and out of focus ares"). \$\endgroup\$
    – Reid
    Commented Jan 22, 2011 at 23:16

In a perfect world, light room (or whatever program) could highlight in focus and out of focus ares the same way that it detects burned out areas of an image.

So, to start with, the Darktable manual includes:

ctrl-z fully zoom into the image and show areas in focus

And that looks like this on an image in partial focus:

enter image description here

More specifically I think that you can get what you are looking for by quantizing the amount of high-frequency information in the fourier transform of the image. (the high frequencies are the sharply focused bits we care about).

Helpfully another SE answer (which I've upvoted for the privilege of the copy paste) gives code for pulling out the frequencies. If it's interesting to people I might come back to this answer and see if I can write the code that orders a set of images by how much sharp(rather than total) focus they have.


As a human, I would rather rate them myself with my subjective observation, seeing as I know where the focus should or shouldn't have been, but I suppose there could be a rough way to determine the focus quality of a shot based on the aperture, focal length, subject distance and focus/unfocused areas of the image.

For example, long focal length, small aperture, close subject means there is likely to be a small point of focus with a lot of bokeh (for common applications of this recipe).

On the other hand, short focal length with a longer subject distance means there's likely to be more in focus areas than out of focus areas (think landscape or group shot).

I guess the most important thing in any photo is, wherever the most focused area of a photo is, it's in focus. This simple "is the most focused point in focus" check would be one I could possibly use as there's most commonly no point in using an out of focus or back-focused shot unless you think it will work.

  • \$\begingroup\$ your last point is only partially true, as most of the time you will focus and then recompose. Like the example I gave in other comment, it can be that the ear will be in perfect focus (and as such your image will have great most focus point) but the eye will be OOF. \$\endgroup\$
    – ysap
    Commented Jan 21, 2011 at 0:23
  • \$\begingroup\$ I didn't even think of that, and yes, I very often focus and recompose, so there could be a slight shift in the focus plane. \$\endgroup\$ Commented Jan 21, 2011 at 3:22

I've just come across this (now almost) 9 year-old question. There is some good information in these answers, but many are quite old now, and none really answer the OP's original question: Can software auto-detect image focus?

After reviewing the posts here, I found an application called Fast Raw Viewer that has at least a partial solution.


A really cheap expedient for images taken with the same quality settings of the camera and the same scene and light setup is to look at the file size of the JPEG. The more material is in focus, the more visibly discernible information is considered by the lossy JPEG compression algorithm worth preserving, increasing the file size.

Of course this makes only sense if the bulk of the image content is what you want to be in focus. It doesn't help against smaller defocused subjects against an in-focus background, for example.

But it can be useful as a quick tie breaker.


I have a similar problem. I take pictures of the moon, and I need to choose the sharpest ones for stacking. As the images are very similar to each other, it is easy to compare. I was shooting with a t7i and a Canon 55-250mm f5.6. After doing a lot of research yesterday, I found some OpenCV routines for working with images. I still made an experimental program, in C# (but it could be done in Python or others languages). I haven't been able to upload the codes anywhere yet but I'll leave some tips. I found the result incredible for such a short experiment. There is a method in the OpenCV library that calculates the sharpness (and focus comes with). I first convert the temp image to grayscale and then calculate the sharpness. I've only tested it with the final jpg, but it will probably work fine with RAW and other extensions as well. See the most relevant part of my code:

//Convert the image to grayscale(just temp image for calc, the original image still the same)
CvInvoke.CvtColor(originalImage, gray, 

//Calculate the sharpness
var laplacian = new Mat();
CvInvoke.Laplacian(gray, laplacian, 
MCvScalar mean = new MCvScalar();
MCvScalar stddev = new MCvScalar();
CvInvoke.MeanStdDev(laplacian, ref mean, ref stddev);
return stddev.V0 * stddev.V0;

This "return" would already be the calculated sharpness, in decimals. In my tests, of the 298 photos I took 146 were above average in sharpness and I saved them in a separate folder to reuse in stacking. The stacking result in Registax is much better.

Average sharpness across all photos: 14.86047
Maximum: 20.89885
Minimum: 9.91646
Above average quantity: 146

Little sample of what kind of results can by achieved(note gaps for excluded images). The greater the number, best sharpness:

IMG_0946: 14.955297037037036
IMG_0948: 18.22463111111111
IMG_0950: 14.944277037037036
IMG_0952: 16.061483703703704
IMG_0957: 14.979610370370372
IMG_0963: 16.130337037037037

I generated two images, in one I stacked all 298 photos and in the other only the 146 images with above average sharpness. I passed these two images resulting from stacking in the sharpness evaluation and the result was this below. Something like 30% more sharpness in the final result. The greater the number, best sharpness:

IMG_0940_ONLY_146_SHARPEST: 306,70520267333194
IMG_0940_ALL_298_IMAGES: 232,35437676592045

In short, I really enjoyed the result and I recommend that anyone who is interested should look for more on the subject. I make myself available to help in any way I can.


Capture 1 maybe what you need!

  • 2
    \$\begingroup\$ Could you expand this answer to explain what "Capture 1" is and why it might help? \$\endgroup\$
    – Philip Kendall
    Commented Jan 26, 2016 at 15:57

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.