<jupyter_start><jupyter_text>Image segmentationImage segmentation is the process of separating an image into multiple regions. See also* [Image manipulation and processing using Numpy and Scipy by <NAME> and <NAME>](https://scipy-lectures.org/advanced/image_processing/index.htmlbasic-image)* [Tutorial on image segmentation with scikit-image](https://scikit-image.org/docs/dev/user_guide/tutorial_segmentation.html)Let's start again by defining an image as a two dimensional array and visualize it using pyclesperanto.<jupyter_code>import numpy as np
from pyclesperanto_prototype import imshow
image = np.asarray([
    [1, 0, 2, 1, 0, 0, 0],
    [0, 3, 1, 0, 1, 0, 1],
    [0, 5, 5, 1, 0, 1, 0],
    [0, 6, 6, 5, 1, 0, 2],
    [0, 0, 5, 6, 3, 0, 1],
    [0, 1, 2, 1, 0, 0, 1],
    [1, 0, 1, 0, 0, 1, 0]
])
imshow(image, colorbar=True)<jupyter_output><empty_output><jupyter_text>Binary imagesThe most basic way of that is binarization, turning the image into a "positive" and a "negative" region. Typically, binary images are used for that, which could for example contain two different pixel values `True` and `False` representing "positive" and "negative", respectively. Technically, every image can be interpreted as a binary image using the rationale "Every pixel is considered positive that is neither `False` nor `0`." Image thresholdingA very basic algorithm for separating low intensity regions from high intensity regions in the image is thresholding.We will now make a new image containing `True` and `False` as pixel values depending on if the original image had intensity lower or higher a given threshold. As this image has just two different pixel values, it is a binary image:<jupyter_code>threshold = 4

binary_image = image > threshold
binary_image
imshow(binary_image)<jupyter_output><empty_output><jupyter_text>[Matplotlib](https://matplotlib.org/) might be more flexible when visualizing images, e.g. for drawing outlines around regions of interest:<jupyter_code>import matplotlib.pyplot as plt

# create a new plot
fig, axes = plt.subplots(1,1)

# add two images
axes.imshow(image, cmap=plt.cm.gray)
axes.contour(binary_image, [0.5], linewidths=1.2, colors='r')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09855334538878843, 'fraction_numerical': 0.025768535262206148, 'mean_word_length': 5.233802816901409, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 3, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '277013', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 738, 'n_tokens_neox': 671, 'n_words': 275, 'path': 'docs/20_image_segmentation/06_Introduction_to_image_segmentation.ipynb', 'repo_name': 'zoccoler/BioImageAnalysisNotebooks', 'size': '2234'}