I am trying to quantify how much sharpness (or acutance) is in a picture which has some bokeh (out of focus background).

I am using the Python scikit image for that. Here is my naive approach:

import matplotlib.pyplot as plt

from skimage import data
from skimage.color import rgb2gray
from skimage.morphology import disk
from skimage.filters.rank import gradient

cat = data.chelsea() # cat is a 300-by-451 pixel RGB image
cat_gray = rgb2gray(cat)

selection_element = disk(5) # matrix of n pixels with a disk shape

cat_sharpness = gradient(cat_gray, selection_element)

plt.imshow(cat_sharpness, cmap="viridis")


So here is the picture (notice the background not in focus) cat

And here is the gradient, which actually measures the difference in contrast: Gradient

This difference in contrast problem is more obvious if one uses the Lena image: enter image description here

Here, the background is caught as well due to the difference in contrast (there is a black frame on the top right)

Any ideas about how to give an scalar value where only the focused areas are highlighted?

  • $\begingroup$ What do you mean by "There is a black frame on top right"? $\endgroup$ Commented Nov 13, 2016 at 3:19
  • $\begingroup$ I've updated the description. The gradient highlights the edges that have contrast. $\endgroup$
    – FZNB
    Commented Nov 13, 2016 at 7:35
  • $\begingroup$ Your differentiating disc might simply be too large. In essence, a gradient is not much more than a high pass filter, and early autofocus units just did that: convolve the image with $(+1,-1,+1,-1)$ in its center and adjust the focus until they found a maximum. $\endgroup$ Commented Nov 13, 2016 at 8:59
  • $\begingroup$ Grateful for the concept of bokeh! $\endgroup$ Commented Nov 13, 2016 at 22:10

2 Answers 2


The recent works I am aware of make use of tools that go beyond mere gradients. Here are a few references that could be starting points:

This paper presents an algorithm designed to measure the local perceived sharpness in an image. Our method utilizes both spectral and spatial properties of the image: For each block, we measure the slope of the magnitude spectrum and the total spatial variation. These measures are then adjusted to account for visual perception, and then, the adjusted measures are combined via a weighted geometric mean. The resulting measure, i.e., S3 (spectral and spatial sharpness), yields a perceived sharpness map in which greater values denote perceptually sharper regions

Sharpness is an important determinant in visual assessment of image quality. The human visual system is able to effortlessly detect blur and evaluate sharpness of visual images, but the underlying mechanism is not fully understood. Existing blur/sharpness evaluation algorithms are mostly based on edge width, local gradient, or energy reduction of global/local high frequency content. Here we understand the subject from a different perspective, where sharpness is identified as strong local phase coherence (LPC) near distinctive image features evaluated in the complex wavelet transform domain. Previous LPC computation is restricted to be applied to complex coefficients spread in three consecutive dyadic scales in the scale-space. Here we propose a flexible framework that allows for LPC computation in arbitrary fractional scales.

The given examples and comparisons across different could provide you with some hints toward your goal.

  • 1
    $\begingroup$ Thanks for the excellent references. It's also nice that they provide MATLAB code, although for the second paper the link seems dead :/ $\endgroup$
    – FZNB
    Commented Nov 14, 2016 at 17:43
  • 1
    $\begingroup$ @FZNB Sorry for that. Looking for an alternative. You can find more recent refs that cite these two. I tend to believe playing in the phase domain can be useful to that goal $\endgroup$ Commented Nov 14, 2016 at 17:59
  • 1
    $\begingroup$ I have a local version of the code for the 2nd paper. I'll make it available shortly $\endgroup$ Commented Nov 15, 2016 at 6:42
  • 1
    $\begingroup$ @FZNB I have told the authors about the broken link, they said they would fix it. Meanwhile, I have posted a local version $\endgroup$ Commented Nov 15, 2016 at 20:26
  • $\begingroup$ They did not fix it $\endgroup$ Commented Aug 27, 2019 at 13:59

If you see the full Lenna image she will be standing so close to the mirror(black frame is a part of the mirror,so the black frame is also in focus), that's why you get that edge when calculating gradient. This is the reason why you are calculating gradient for black frame in this particular image.

If you need a general method, this is something I could think of:

Finding the the maximum gradient of the image

Get the approximate depth map(Hardest part to extract from single image)

Filter the gradients that have more or less the same depth as the maximum gradient.


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.