<jupyter_start><jupyter_text>Edge detectionIn clesperanto, multiple filters for edge-detection are implemented.See also* [Wikipedia](https://en.wikipedia.org/wiki/Edge_detection)<jupyter_code>import pyclesperanto_prototype as cle
from skimage.io import imread
import matplotlib.pyplot as plt

cle.select_device("RTX")
blobs = imread("../../data/blobs.tif")
blobs.shape
cle.imshow(blobs)<jupyter_output><empty_output><jupyter_text>Sobel operator[see also](https://en.wikipedia.org/wiki/Sobel_operator)<jupyter_code>blobs_sobel = cle.sobel(blobs)
cle.imshow(blobs_sobel)<jupyter_output><empty_output><jupyter_text>Laplace operator[see also](https://en.wikipedia.org/wiki/Discrete_Laplace_operator)<jupyter_code>blobs_laplace = cle.laplace_box(blobs)
cle.imshow(blobs_laplace)<jupyter_output><empty_output><jupyter_text>Laplacian of Gaussian[Also kown as the Mexican hat filter](https://en.wikipedia.org/wiki/Ricker_wavelet)<jupyter_code>blobs_laplacian_of_gaussian = cle.laplace_box(cle.gaussian_blur(blobs, sigma_x=1, sigma_y=1))
cle.imshow(blobs_laplacian_of_gaussian)
blobs_laplacian_of_gaussian = cle.laplace_box(cle.gaussian_blur(blobs, sigma_x=5, sigma_y=5))
cle.imshow(blobs_laplacian_of_gaussian)<jupyter_output><empty_output><jupyter_text>Local Variance filter<jupyter_code>blobs_edges = cle.variance_box(blobs, radius_x=5, radius_y=5)
cle.imshow(blobs_edges)<jupyter_output><empty_output><jupyter_text>Local standard deviation... is just the square root of the local variance<jupyter_code>blobs_edges = cle.standard_deviation_box(blobs, radius_x=5, radius_y=5)
cle.imshow(blobs_edges)<jupyter_output><empty_output><jupyter_text>Edge detection is not edge enhancementIntuitively, one could apply an edge detection filter to enhance edges in images showing edges. Let's try with an image showing membranes. It's a 3D image btw.<jupyter_code>image = imread("../../data/EM_C_6_c0.tif")
image.shape
cle.imshow(image[60])
image_sobel = cle.sobel(image)
cle.imshow(image_sobel[60])<jupyter_output><empty_output><jupyter_text>When looking very carefully, you may observe that the edges are a bit thicker in the second image. The edge detection filter detects two edges, the increasing signal side of the membrane and the decreasing signal on the opposite side. Let's zoom:<jupyter_code>fig, axs = plt.subplots(1, 2)
cle.imshow(                image[60, 125:145, 135:155], plot=axs[0])
cle.imshow(cle.pull(image_sobel)[60, 125:145, 135:155], plot=axs[1])<jupyter_output><empty_output><jupyter_text>Enhancing edgesThus, to enhance edges in a membrane image, other filters are more useful. Enhancement may for example mean making membranes thicker and potentially closing gaps.  Local standard deviation<jupyter_code>image_std = cle.standard_deviation_box(image, radius_x=5, radius_y=5, radius_z=5)
cle.imshow(image_std[60])<jupyter_output><empty_output><jupyter_text>Local maximum<jupyter_code>image_max = cle.maximum_box(image, radius_x=5, radius_y=5, radius_z=5)
cle.imshow(image_max[60])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10934393638170974, 'fraction_numerical': 0.018886679920477135, 'mean_word_length': 8.614649681528663, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '621630', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1066, 'n_words': 202, 'path': 'docs/18_image_filtering/edge_detection_and_enhancement.ipynb', 'repo_name': 'rayanirban/BioImageAnalysisNotebooks', 'size': '3018'}