# How to locally quantify the 'sharpness' of an image?

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

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

plt.imshow(cat_sharpness, cmap="viridis")
plt.axis('off')
plt.colorbar()

plt.show()


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

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

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

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?

• What do you mean by "There is a black frame on top right"? – Navin Prashath Nov 13 '16 at 3:19
• I've updated the description. The gradient highlights the edges that have contrast. – FZNB Nov 13 '16 at 7:35
• 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. – Marcus Müller Nov 13 '16 at 8:59
• Grateful for the concept of bokeh! – Laurent Duval Nov 13 '16 at 22:10

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.

• Thanks for the excellent references. It's also nice that they provide MATLAB code, although for the second paper the link seems dead :/ – FZNB Nov 14 '16 at 17:43
• @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 – Laurent Duval Nov 14 '16 at 17:59
• I have a local version of the code for the 2nd paper. I'll make it available shortly – Laurent Duval Nov 15 '16 at 6:42
• @FZNB I have told the authors about the broken link, they said they would fix it. Meanwhile, I have posted a local version – Laurent Duval Nov 15 '16 at 20:26
• They did not fix it – Ignacio Peletier Aug 27 '19 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.