Scikit Image − Removing Small Objects from an Image



Removing small objects from an image refers to the process of identifying and eliminating relatively small objects (otherthan Zero’s) within an image that are often considered noise, or unwanted details. This is a common image-processing task used in various applications, such as image segmentation, object detection, and image analysis.

The scikit-image (skimage) library provides a remove_small_objects() function in the morphology module to identify and remove connected components or objects in a binary or labeled image based on the specified size.

Using the skimage.morphology.remove_small_objects() function.

The remove_small_objects() function is used to remove objects or connected components in a binary or labeled image that are smaller than a specified size.

Syntax

Following is the syntax of this function −

skimage.morphology.remove_small_objects(ar, min_size=64, connectivity=1, *, out=None)

Parameters

The function accepts the following parameters:

  • ar (ndarray): This parameter expects an array, which can be of arbitrary shape and should typically be of integer or boolean type. It is the input image containing the connected components of interest. If the array contains integers then ints must be non-negative.
  • min_size (int, optional, default: 64): It specifies the smallest allowable object size, in pixels.
  • connectivity (int, optional, default: 1): This parameter defines the connectivity of the neighborhood of a pixel and is used during the labeling process when ar is a boolean array.
  • out (ndarray, optional): It specifies an ndarray of the same shape as ar where the output will be placed. By default, a new ndarray is created to store the output.

Return value

The function returns an output array of the same shape and data type as the input ar. This output array will have small connected components (objects) removed if they are smaller than the specified min_size.

The function may raise a TypeError if the input array is of an invalid type (e.g., float or string) and a ValueError if the input array contains negative values.

Example 1

The following example removes objects smaller than 2 pixels using the morphology.remove_small_objects() function −

import numpy as np 
from skimage import morphology 
import matplotlib.pyplot as plt 
 
# Input array 
arr = np.array([
   [1, 1, 1, 0, 0],
   [1, 0, 1, 0, 0],
   [1, 1, 1, 0, 0],
   [0, 0, 0, 0, 2]])
# Print the input array
print("Input array:")
print(arr)
# Remove objects smaller than 2 pixels
result = morphology.remove_small_objects(arr, 2)

# Print the result
print("\nResult after removing small objects:")
print(result)

Output

Input array:
[[1 1 1 0 0]
[1 0 1 0 0]
[1 1 1 0 0]
[0 0 0 0 2]]

Result after removing small objects:
[[1 1 1 0 0]
[1 0 1 0 0]
[1 1 1 0 0]
[0 0 0 0 0]]

Example 2

import numpy as np 
from skimage import morphology, segmentation, io 
import matplotlib.pyplot as plt 
 
# Load an input image 
image = np.array([[1, 1, 1, 0, 0, 0], 
[1, 0, 1, 0, 1, 0], 
[1, 1, 1, 0, 0, 0], 
[0, 0, 0, 0, 1, 0], 
[0, 1, 0, 0, 1, 0]], bool) 
# Remove objects smaller than 2 pixels 
result = morphology.remove_small_objects(image, 2) 

# Visualize the input and result using matplotlib 
fig, axes = plt.subplots(1, 2, figsize=(10, 5)) 
ax = axes.ravel() 
# Display the input array 
ax[0].imshow(image) 
ax[0].set_title('Input image') 
ax[0].axis('off') 
# Display the result 
ax[1].imshow(result) 
ax[1].set_title('Result After Removing Small Objects') 
ax[1].axis('off') 
plt.tight_layout() 
plt.show() 

Output

Removing object from an image
Advertisements