<jupyter_start><jupyter_text>Filtering imagesImage filters take an image and produce a new image. Typically, a filter kernel goes from pixel to pixel and computes a new pixel value depending on an old pixel and its neighborhood.See also* [Filters in scikit-image](https://scikit-image.org/docs/dev/api/skimage.filters.html)We start with a test image and apply a Gaussian blur filter.<jupyter_code>import numpy as np
from pyclesperanto_prototype import imshow
from skimage.filters import gaussian
from skimage import filters
import matplotlib.pyplot as plt
from skimage.morphology import disk
from skimage.io import imread
test_image = np.zeros((10,10))
test_image[5,3] = 1
test_image[5,7] = 1
test_image
imshow(test_image)<jupyter_output><empty_output><jupyter_text>The [Gaussian filter](https://scikit-image.org/docs/dev/api/skimage.filters.htmlskimage.filters.gaussian) takes two important arguments, the image to blur and a sigma parameter. The sigma determines how wide the Gaussian blur should show effects.<jupyter_code>blurred = gaussian(test_image, sigma=1)
imshow(blurred)<jupyter_output><empty_output><jupyter_text>Let's compare Gaussian blurred images with different sigma<jupyter_code>blurred05 = gaussian(test_image, sigma=0.5)
blurred1 = gaussian(test_image, sigma=1)
blurred2 = gaussian(test_image, sigma=2)
blurred3 = gaussian(test_image, sigma=3)

fig, axs = plt.subplots(1, 4, figsize=(15,15))

axs[0].imshow(blurred05)
axs[1].imshow(blurred1)
axs[2].imshow(blurred2)
axs[3].imshow(blurred3)<jupyter_output><empty_output><jupyter_text>Note: If the sigma is larger than the distance between two spots, the spots cannot be differentiated as two local maxima in the blurred image anymore.   Filters with structuring elementsSome filters ask for a structuring element as [parameter](https://scikit-image.org/docs/dev/auto_examples/numpy_operations/plot_structuring_elements.htmlsphx-glr-auto-examples-numpy-operations-plot-structuring-elements-py).In biological/microscopy applications, the [disk](https://scikit-image.org/docs/dev/api/skimage.morphology.htmlskimage.morphology.disk) sturcturing element is most commonly used.To understand it, we should visualize it first. A disk with radius 4 is a 9x9 large image containing black pixels(intensity=0) and disk-shaped pixels with value 1.<jupyter_code>from skimage.morphology import disk

imshow(disk(4))<jupyter_output><empty_output><jupyter_text>Disks with other radii look like this:<jupyter_code>max_radius = 5

fig, axs = plt.subplots(1, max_radius, figsize=(15,15))

for r in range(1, max_radius + 1):
    axs[r - 1].imshow(disk(r))
    axs[r - 1].set_title("Radius " + str(r))<jupyter_output><empty_output><jupyter_text>Coming back to our test image, this is how it looks like when we apply a mean filter with different disk radii.<jupyter_code>filtered1 = filters.rank.mean(test_image, disk(1))
filtered2 = filters.rank.mean(test_image, disk(2))
filtered3 = filters.rank.mean(test_image, disk(3))

fig, axs = plt.subplots(1, 4, figsize=(15,15))
axs[0].imshow(test_image)
axs[0].set_title('original')

axs[1].imshow(filtered1)
axs[1].set_title('Mean (radius = 1)')

axs[2].imshow(filtered2)
axs[2].set_title('Mean (radius = 2)')

axs[3].imshow(filtered3)
axs[3].set_title('Mean (radius = 3)')<jupyter_output>/var/folders/p1/6svzckgd1y5906pfgm71fvmr0000gn/T/ipykernel_12011/3573640970.py:1: UserWarning: Possible precision loss converting image of type float64 to uint8 as required by rank filters. Convert manually using skimage.util.img_as_ubyte to silence this warning.
  filtered1 = filters.rank.mean(test_image, disk(1))
/var/folders/p1/6svzckgd1y5906pfgm71fvmr0000gn/T/ipykernel_12011/3573640970.py:2: UserWarning: Possible precision loss converting image of type float64 to uint8 as required by rank filters. Convert manually using skimage.util.img_as_ubyte to silence this warning.
  filtered2 = filters.rank.mean(test_image, disk(2))
/var/folders/p1/6svzckgd1y5906pfgm71fvmr0000gn/T/ipykernel_12011/3573640970.py:3: UserWarning: Possible precision loss converting image of type float64 to uint8 as required by rank filters. Convert manually using skimage.util.img_as_ubyte to silence this warning.
  filtered3 = filters.rank.mean(test_image, disk(3))
{'fraction_non_alphanumeric': 0.1016548463356974, 'fraction_numerical': 0.0425531914893617, 'mean_word_length': 6.52846975088968, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '140947', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1430, 'n_words': 371, 'path': 'docs/18_image_filtering/01_Filtering.ipynb', 'repo_name': 'zoccoler/BioImageAnalysisNotebooks', 'size': '4230'}