7

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")
plt.axis('off')
plt.colorbar()

plt.show()

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?

FZNB
  • 173
  • 1
  • 1
  • 5

2 Answers2

7

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.

Laurent Duval
  • 31,850
  • 3
  • 33
  • 101
2

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.

Navin Prashath
  • 700
  • 7
  • 15