File Exchange

## Focus Measure

version 2.2.0.0 (4.62 KB) by
A function to measure the relative degree of focus of an image.

Updated 31 Aug 2017

This function measures the relative degree of focus of an image. Several up-to-date focus measuring algorithms have been implemented and the function supports uint8 or double images. For futher details on each focus measuring algorithm the reader is referred to [1] and the references therein. For further information and datasets, see https://sites.google.com/view/cvia/focus-measure
[1] S. Pertuz et al., Analysis of focus measure operators for shape-from-focus. Pattern Recognition, 46(5):1415:1432, 2013.

### Cite As

Said Pertuz (2020). Focus Measure (https://www.mathworks.com/matlabcentral/fileexchange/27314-focus-measure), MATLAB Central File Exchange. Retrieved .

Steve Boege

Hi Said,
In the Brenner calculation, squaring after choosing max is resulting in smaller magnitudes when difference is negative. Using max is actually making the result different for {myImage, fliplr(myImage) and flipud(myImage)}. This code give the same result for mirrored images.

case 'BREN_I' % Brenner's (Santos97)
[M, N] = size(Image);
DH = zeros(M, N);
DV = zeros(M, N);
DV(1:M-2,:) = Image(3:end,:)-Image(1:end-2,:);
DV = DV.*DV;
DH(:,1:N-2) = Image(:,3:end)-Image(:,1:end-2);
DH=DH.*DH;
FM = DV + DH;
FM = sqrt(mean2(FM)) * M * N / (M * N - 4);

Steve

Meryem Uzun Per

jonna ganesh

Steve Boege

One thing to note: Brenner and perhaps other calculated metrics for an image and the exact same image horizontally flipped can be slightly different.

sang min sung

Amir Bernat

Thank you Said for this great code!
A couple of points I noticed while working on it, please accept them in good spirit.
1. I highly recommend to make sure to cast variable not to be Uint8 before using many of the algorithms here, this can cause clipping and affect the results in an undesirable way.
2. When using the "diff" command on an image with size N*M, the returned result is size (N-1)*(M-1) therefore it would be better to initialize the result arrays to be of that size. For example, look at GREA where both Ix and Iy retain the original dimension but only update a 1:dim-1 of the new array. This effect can be large for small or very flat images.

Again, thank you for this great resource!

Amir

mysiak

Said Pertuz

Hi Bilal, you can set the internal parameter WSize=3, and then invoke the function as: fm = focusmeasure(im, method). Then you can retrieve the measure at the point of interest as fm(y0,x0).

Hi, what if i just want to measure the focus value of only 3x3 window? e.g. if i input ROI = [xo,y0,2,2]. Thanks!

Antoine

It works perfectly

Omid Hoseini

very useful. Thanks.

Ryan Lane

Mikhail Monakhov

TENV algorithm is the best (for HEK cells on glass coverslip)

Ralph

Extremely useful. My only feedback - many of the methods require 2D images. I ended up wrapping the code in a try-catch block. If the function fails on the initial image I throw a warning and then call the function body with rgb2gray(I) for the image input.

Yin Cheng

changsub lee

Hi , I want to test my image so i got your code.
but i don't know how can i use that. how do i process your code?

Said Pertuz

Thanks @Joel Cottrell for spotting this bug! The problem has been fixed

Joel Cottrell

This is a very useful function for quickly trying out a range of focus measures, thank you.
Is there possibly a bug in the AcMomentum function, on line 245? It seems like the mean of the image shouldn't be multiplied by 255, because it is already in the 0-255 range (not 0.00-1.00).

Thanh Pham Chi

If SFRQ is computed, the gradient components are initialized with the image. This yields overly high focus measure values at right and bottom image borders. I suggest to initialize both gradient components with zeros.

Nam Yeo Kang

user001

DCTE and DCTR return exclusively NaN for inputs of type double (haven't tried uint8 inputs).

John Sullivan

shike

6666666666666666

Said Pertuz

@Anthony, the normalization is not strictly necessary since the operations are performed in double precision. However, your suggestion is quite correct if we want to avoid stability issues. I have corrected the code accordingly, thanks!

Anthony

Concerning the GDER criterion, I wonder wether the divisions in l 80/81 should not be replaced by

Gx = Gx / max(max(abs(Gx))) (resp. Gy)

in order to normalize the kernels?
The symmetry of the kernels implicates sum(Gx(:)) -> 0 thus creating extremely high values in resulting Gx & Gy (order of 10^13).

Sorry if I completely missed the point!

Pascal Böhmler

Thanks.
In your implementation of Brenners metric there seems to be a bug. diff(Image,2,1) and diff(Image,2,2) is used. This is somewhat like calculating the second order discrete derivation but shouldn't it be the (~first order) difference between two pixels with a distance d=2?

Jesse

Sayonics.com seems to have gone away.

Misha

Ahmed

Thanks very much :)

Nicole Roamans

How do you choose WSize=15 and sig=N/2.5 in 'GDER'? If anyone can help I would appreciate it.
Thank you for providing your code.

George Aprilis

Very helpful, allows you to try quickly many different methods and see what suits your case.
There is for sure a small bug in SFIL case, between 270 and 315 degrees, R(:,:,7) appears twice instead of R(:,:,7) and R(:,:,8)

Rishabh Battulwar

MTJ1990

Engin

It seems like some of the metrics (options) needs Wavelet Toolbox (if there is something like this?), because a lot of functions for WAVS, WAVV and WAVR are missing.

Moreover, it is not exactly clear if an image should be of type uint8 or double or what - however results of some metrics depends on datatype of image.

Karthik MSwamy

Quite a useful tool to measure various focus measures.

Rob

I was wondering how you got the plot.

Bosheng

Nek Valous

Jonathan

Great tool, exactly what I was looking for!

Note that the ACMO method as written also requires the Fixed-Point Toolbox. I just commented out the isdouble() check, and make sure my images are uint8 before passing them in.

Dave Holden

Very useful

Mohamed Shams

 31 Aug 2017 2.2.0.0 Fixed a bug in the function AcMomentum 29 Mar 2017 2.1.0.0 - Description updated 12 Mar 2016 2.1.0.0 - Bug fixed in GDER focus operator 25 Jan 2016 2.0.0.0 - Some bugs have been fixed (Brenner's and GDER's focus measure operators) 22 Nov 2012 1.3.0.0 - The function no longer requires the floating-point toolbox.
##### MATLAB Release Compatibility
Created with R2008b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux