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)
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?