Page 1 of 1

Detecting out-of-focus images?

Posted: 2018-07-28T10:32:46-07:00
by taxisaga
I'm working with photo data and need to split the images into two sets: focused and unfocused.

Without getting into complications like depth of field where images can have some regions focused and others non-focused... just keeping it simple and assuming the entire image is unfocused due to either poor settings, bad lighting, motion, or other reasons...

is there any one-liner type command that can be run with identify or any simple test that can be hacked together with convert to detect which images are sharp and which are not?

Re: Detecting out-of-focus images?

Posted: 2018-07-28T10:54:14-07:00
by snibgo
A measure for out-of-focus is local standard deviation (SD). This measures contrast over a small area, such as every 15x15 pixel square. Then we find the largest SD across the entire image. This ranges from 0.0 (image is a total blur) to 0.5 (some part of the image is highly sharp).

Code: Select all

f:\web\im>%IMG7%magick toes.png -statistic StandardDeviation 15x15 -format %[fx:
maxima] info:
0.250858
To show the point, we repeat with a slightly blurred input:

Code: Select all

f:\web\im>%IMG7%magick toes.png -blur 0x1 -statistic StandardDeviation 15x15 -fo
rmat %[fx:maxima] info:
0.239658
As expected, the maximum SD is lower.

Then you need to decide on a threshold for "in focus" or "out of focus". If you have samples in each category, find the maximum SDs and they will probably show you the cutoff.

If you have many large images, this will be slow. You might first resize them all to be smaller. Or use a technique such as Integral images.

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:09:06-07:00
by taxisaga
Wow that is a very nice web page! Great work.

Thanks for the reply. I'm trying it out, with some hiccups so far.

Tried your command and got this:

Code: Select all

magick: unable to open image 'maxima]': No such file or directory @ error/blob.c/OpenBlob/3457.
Playing around with quotes around the format argument in case those got stripped off by the board here, but no luck yet.

My exact command line was:

Code: Select all

for n in *.jpg; do magick $n -statistic StandardDeviation 15x15 -format %[fx: maxima] info:; done
Version: ImageMagick 7.0.7-39 Q16 x86_64 2018-06-11 https://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: https://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:10:39-07:00
by taxisaga
Oh I see what it was; I had an extra space before "maxima".

Wow yes it is slow! But a great thing to start with, thanks!

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:21:47-07:00
by taxisaga
Running on some test images:

Code: Select all

$ cd blurry/
sagaraga :blurry$ for n in *.jpg; do magick $n -statistic StandardDeviation 15x15 -format %[fx:maxima] info:; echo ""; done
0.360601
0.374182
0.385077
0.347387
0.302251
sagaraga :blurry$ cd ../non_blurry/
sagaraga :non_blurry$ for n in *.jpg; do magick $n -statistic StandardDeviation 15x15 -format %[fx:maxima] info:; echo ""; done
0.449119
0.427832
0.457877
0.429221
0.441047
I sort of wish the values were farther apart, but the difference seems clear. I will try out different thresholds.

Also I assume playing with the 15x15 value might help.

Maybe there's a way to tweak the -format so I won't need the extra echo "' on each invocation... Will look into that.

And finally with more work maybe cropping out sample regions of each image and submitting only the sample regions for evaluation as a way to speed this up... unless anyone has better ideas.

Thanks again!

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:24:44-07:00
by fmw42
add \n to %[fx:maxima] so that you get (with quotes on Unix-like systems)

Code: Select all

"%[fx:maxima]\n"
That should avoid needing the extra echo. It makes the output on a new line for each image

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:33:09-07:00
by taxisaga
Also I am going to chew on that other technique you documented in that web page. Integral images. Looks promising but the challenge for me will be to boil it down to a script that returns a single comparable value (or maybe it's not hard... haven't tackled the whole page yet, it looks a bit past my level, but we'll see if I can figure something out... and to be straight about it, I'll just say, if you have any suggestions on that front I'm all ears!).

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:33:48-07:00
by taxisaga

Code: Select all

"%[fx:maxima]\n"
Thanks!

Re: Detecting out-of-focus images?

Posted: 2018-07-28T11:34:38-07:00
by snibgo
15x15 is a decent window for web-sized images, eg 600x600, or resized to 600x600. For large inputs such as 7000x5000 a larger window will be more appropriate (but will take forever, using "-statistic").

Using integral images gives a massive performance boost, but needs a rebuild of IM.

Windowed mean and standard deviation surveys different methods of finding windowed SD. IM's "-statistic" is the slowest; I show improvements of a factor of 1000.

Details, details shows methods for finding where detail is in an image, and these can easily be extended to find whether an image has detail.

Re: Detecting out-of-focus images?

Posted: 2018-07-28T18:37:44-07:00
by taxisaga
Is there a build time switch or setting for enabling integral images then? I don't normally tackle building IM but I could give it a shot. Running a beta OS (macOS Mojave) at the moment so I could have some problems but I could try.

Haven't looked at the new links yet but looking forward to that soon.

Re: Detecting out-of-focus images?

Posted: 2018-07-29T06:02:38-07:00
by snibgo
taxisaga wrote:Is there a build time switch or setting for enabling integral images then?
No. To incorporate integral images, or any of my process modules, download my source code from my Process modules page into the "filters" subdirectory and re-build IM.

Building IM on Windows is easy. It's harder on a Mac because the delegates have to be done first. See posts by fmw42 on the topic.

Re: Detecting out-of-focus images?

Posted: 2018-07-29T10:11:09-07:00
by fmw42
I believe the post to which snibgo is referring is viewtopic.php?f=1&t=29100

Re: Detecting out-of-focus images?

Posted: 2018-08-15T14:01:34-07:00
by taxisaga
Thanks! Maybe I'll try building when I'm feeling especially brave...