Search code examples
pythonnumpyimage-processingcomputer-visiongaussianblur

Gaussian filters create holes in histogram


I'm using a simple gaussian filter to blur the image in the MWE below

import imageio
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter

image = imageio.imread('imageio:camera.png')
image_blurred = np.copy(image).astype(np.float)
image_blurred /= 255
image_blurred = gaussian_filter(image_blurred, sigma=3)
image_blurred *= 255
image_blurred = np.round(image_blurred).astype(np.uint8)

plt.close('all')
fig, ax = plt.subplots(2,2, figsize=(6,8))

ax[0][0].imshow(image, cmap='gray')
ax[0][0].set_title('Original')
ax[1][0].hist(image.ravel(), 256, density=True)

ax[0][1].imshow(image_blurred, cmap='gray')
ax[0][1].set_title('Blur 3')
ax[1][1].hist(image_blurred.ravel(), 256, density=True)
plt.show()

The problem is, the image after a gaussian filter shows some holes in historgram as we can see below in the histogram on right

enter image description here

Is that expected? If so which operation can I apply on image to smooth the histogram ?


Solution

  • Take note of the x-axis of your histograms: the 2nd one has a smaller range. If you create 256 bins in the range of, say, 0-240, you should get 15 bins that don’t contain an integer value. Because you round your pixel values, these bins will remain empty.

    This happens because blurring tends to remove extreme values from the image.

    When computing the histogram of integer-values images, make sure your bins have an integer width.