Unsupervised Segmentation
Mark Boundaries
This technique produces an image with highlighted borders between labeled areas, where the pictures were segmented using the SLIC method.
skimage.segmentation.mark_boundaries() function is to return image with boundaries between labeled regions.
Syntax : skimage.segmentation.mark_boundaries(image)
Parameters :
- image : An image
- label_img : Label array with marked regions
- color : RGB color of boundaries
- outline_color : RGB color of surrounding boundaries
Return : marked : An image with boundaries are marked
Code:
Python3
# Importing required boundaries from skimage.segmentation import slic, mark_boundaries from skimage.data import astronaut # Setting the plot figure as 15, 15 plt.figure(figsize = ( 15 , 15 )) # Sample Image of scikit-image package astronaut = astronaut() # Applying SLIC segmentation # for the edges to be drawn over astronaut_segments = slic(astronaut, n_segments = 100 , compactness = 1 ) plt.subplot( 1 , 2 , 1 ) # Plotting the original image plt.imshow(astronaut) # Detecting boundaries for labels plt.subplot( 1 , 2 , 2 ) # Plotting the output of marked_boundaries # function i.e. the image with segmented boundaries plt.imshow(mark_boundaries(astronaut, astronaut_segments)) |
Output:
Explanation: We cluster the image into 100 segments with compactness = 1 and this segmented image will act as a labeled array for the mark_boundaries() function. Each segment of the clustered image is differentiated by an integer value and the result of mark_boundaries is the superimposed boundaries between the labels.
Simple Linear Iterative Clustering
By combining pixels in the image plane based on their color similarity and proximity, this method generates superpixels. Simple Linear Iterative Clustering is the most up-to-date approach for segmenting superpixels, and it takes very little computing power. In a nutshell, the technique clusters pixels in a five-dimensional color and picture plane space to create small, nearly uniform superpixels.
skimage.segmentation.slic() function is used to segment image using k-means clustering.
Syntax : skimage.segmentation.slic(image)
Parameters :
- image : An image
- n_segments : Number of labels
- compactness : Balances color and space proximity.
- max_num_iter : Maximum number of iterations
Return : labels: Integer mask indicating segment labels.
Code:
Python3
# Importing required libraries from skimage.segmentation import slic from skimage.data import astronaut from skimage.color import label2rgb # Setting the plot size as 15, 15 plt.figure(figsize = ( 15 , 15 )) # Sample Image of scikit-image package astronaut = astronaut() # Applying Simple Linear Iterative # Clustering on the image # - 50 segments & compactness = 10 astronaut_segments = slic(astronaut, n_segments = 50 , compactness = 10 ) plt.subplot( 1 , 2 , 1 ) # Plotting the original image plt.imshow(astronaut) plt.subplot( 1 , 2 , 2 ) # Converts a label image into # an RGB color image for visualizing # the labeled regions. plt.imshow(label2rgb(astronaut_segments, astronaut, kind = 'avg' )) |
Output:
Explanation: This technique creates superpixels by grouping pixels in the picture plane based on their color similarity and closeness. This is done in 5-D space, where XY is the pixel location. Because the greatest possible distance between two colors in CIELAB space is restricted, but the spatial distance on the XY plane is dependent on the picture size, we must normalize the spatial distances in order to apply the Euclidean distance in this 5D space. As a result, a new distance measure that takes superpixel size into account was created to cluster pixels in this 5D space.
Felzenszwalb’s Segmentation
Felsenszwalb’s efficient graph-based picture segmentation is computed. It produces an over-segmentation of an RGB picture on the image grid using a quick, minimal spanning tree-based clustering. This may be used to isolate features and identify edges. This algorithm uses the Euclidean distance between pixels. skimage.segmentation.felzenszwalb() function is used to compute Felsenszwalb’s efficient graph-based image segmentation.
Syntax : skimage.segmentation.felzenszwalb(image)
Parameters :
- image : An input image
- scale : Higher value – larger clusters
- sigma : Width of Gaussian kernel
- min_size : Minimum component size
Return : segment_mask : Integer mask indicating segment labels.
Code:
Python3
# Importing the required libraries from skimage.segmentation import felzenszwalb from skimage.color import label2rgb from skimage.data import astronaut # Setting the figure size as 15, 15 plt.figure(figsize = ( 15 , 15 )) # Sample Image of scikit-image package astronaut = astronaut() # computing the Felzenszwalb's # Segmentation with sigma = 5 and minimum # size = 100 astronaut_segments = felzenszwalb(astronaut, scale = 2 , sigma = 5 , min_size = 100 ) # Plotting the original image plt.subplot( 1 , 2 , 1 ) plt.imshow(astronaut) # Marking the boundaries of # Felzenszwalb's segmentations plt.subplot( 1 , 2 , 2 ) plt.imshow(mark_boundaries(astronaut, astronaut_segments)) |
Output:
Explanation: Using a rapid, minimal tree structure-based clustering on the picture grid, creates an over-segmentation of a multichannel image. The parameter scale determines the level of observation. Less and larger parts are associated with a greater scale. The diameter of a Gaussian kernel is sigma, which is used to smooth the picture before segmentation. Scale is the sole way to control the quantity of generated segments as well as their size. The size of individual segments within a picture might change dramatically depending on local contrast.
There are many other supervised and unsupervised image segmentation techniques. This can be useful in confining individual features, foreground isolation, noise reduction, and can be useful to analyze an image more intuitively. It is a good practice for images to be segmented before building a neural network model in order to yield effective results.
Image Segmentation using Python’s scikit-image module
The process of splitting images into multiple layers, represented by a smart, pixel-wise mask is known as Image Segmentation. It involves merging, blocking, and separating an image from its integration level. Splitting a picture into a collection of Image Objects with comparable properties is the first stage in image processing. Scikit-Image is the most popular tool/module for image processing in Python.