Search code examples
pythonscikit-imageentropy

What is the 'FootPrint Parameter' in skimage.filters.rank.entropy? (Scikit-Image, Python)


I am trying to replicate this example below from the package documentation. However after adjusting the 'Footprint' parameter (in skimage.filters.rank's entropy function) with 'disk(2)' or 'disk(10)' I am getting a blurrier image each time. The documentation states 'The neighborhood expressed as an ndarray of 1’s and 0’s' but what does the 'Footprint' parameter represent in terms of pixels and the Entropy formula?

import matplotlib.pyplot as plt
import numpy as np

from skimage import data
from skimage.util import img_as_ubyte
from skimage.filters.rank import entropy
from skimage.morphology import disk

rng = np.random.default_rng()

noise_mask = np.full((128, 128), 28, dtype=np.uint8)
noise_mask[32:-32, 32:-32] = 30

noise = (noise_mask * rng.random(noise_mask.shape) - 0.5
         * noise_mask).astype(np.uint8)
img = noise + 128

#entr_img = entropy(img, disk(2)) # try another 'Footprint'
#entr_img = entropy(img, disk(5)) # try another 'Footprint'
entr_img = entropy(img, disk(10))

fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(10, 4))

img0 = ax0.imshow(noise_mask, cmap='gray')
ax0.set_title("Object")
ax1.imshow(img, cmap='gray')
ax1.set_title("Noisy image")
ax2.imshow(entr_img, cmap='viridis')
ax2.set_title("Local entropy")

fig.tight_layout()

Example link: https://scikit-image.org/docs/dev/auto_examples/filters/plot_entropy.html

Documentation for skimage.filters.rank.entropy: https://scikit-image.org/docs/dev/api/skimage.filters.rank.html#skimage.filters.rank.entropy


Solution

  • I am now facing the same problem of yours. I tried to look at the original function in skimage src, but it points to the code in cython, and I am not good at coding so can only guess.

    My hypothesis is: the footprint is the range of neighbouring pixels taking into calculation when computing the local entropy. As this page in skimage introduces, disk(X) produce a disk-like range with radius equals to X+1 (including the centroid).

    https://scikit-image.org/docs/dev/auto_examples/numpy_operations/plot_structuring_elements.html#sphx-glr-auto-examples-numpy-operations-plot-structuring-elements-py

    To test this hypothesis, as we know the entropy of image is calculated as:

    formula of entropy

    and if there is only 1 pixel (e.g. disk(0)), pF(i)=1, log2(pF(i))=0 and the entropy always equals to 0.

    So I tried entropy(image, footprint=disk(0)), and just as I expected, everywhere in the resultant image equals to 0.

    situation when footprint=disk(0)

    Hope this could help you... (although I am not 100% sure about this answer.)


    Addition: I later tested the situation where footprint = disk(1). As I hypothesed, disk(1) include 5 pixels, and the maximum entropy is:

    5 * (-0.2 * log2(0.2)) = 2.32192809 (when the 5 pixels all have different gray levels)

    And just as I expected, the resultant entropy image has the maximum value of 2.32192809.