Image segmentation is the process of partitioning an image into multiple segments or regions, which makes it easier to analyze and interpret. By separating an image into distinct parts, we can isolate objects of interest, enhance image analysis, and improve recognition systems. It's a fundamental step in various applications, including object detection, image editing, and medical imaging.
There are various techniques for image segmentation, but in this blog, we will focus on the following three widely-used methods:
Thresholding is one of the simplest methods of image segmentation. It converts a grayscale image into a binary image by setting a threshold value. All pixel values above the threshold become white, and all pixel values below it become black.
import cv2 import numpy as np # Load image in grayscale image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Apply thresholding _, thresholded = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) # Display the results cv2.imshow('Original Image', image) cv2.imshow('Thresholded Image', thresholded) cv2.waitKey(0) cv2.destroyAllWindows()
In the above example, we use OpenCV to load an image, apply a simple threshold, and then display both the original and thresholded images.
K-means clustering is a popular unsupervised learning algorithm that can be used for image segmentation. It works by clustering pixel values into K different groups, which can highlight distinct segments in the image.
import cv2 # Load image image = cv2.imread('image.jpg') pixel_values = image.reshape((-1, 3)) pixel_values = np.float32(pixel_values) # Define K-means criteria and apply k-means k = 3 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2) _, labels, centers = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) # Convert back to uint8 centers = np.uint8(centers) segmented_image = centers[labels.flatten()] segmented_image = segmented_image.reshape(image.shape) # Display the results cv2.imshow('Original Image', image) cv2.imshow('Segmented Image', segmented_image) cv2.waitKey(0) cv2.destroyAllWindows()
In this example, we reshape the image into a 2D array where each pixel's color is represented as a 3D point (R, G, B). We then apply K-means to categorize pixels into three clusters, effectively segmenting the image.
The watershed algorithm is a powerful image segmentation technique that treats the grayscale image as a topographic surface. The algorithm identifies "catchment basins" based on gradients in pixel intensity and segments objects accordingly.
import cv2 import numpy as np # Load image in grayscale image = cv2.imread('image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply a binary threshold for segmentation _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) # Find sure foreground area dist_transform = cv2.distanceTransform(thresh, cv2.DIST_L2, 5) _, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0) # Find unknown region unknown = cv2.subtract(thresh, np.uint8(sure_fg)) # Marker labelling _, markers = cv2.connectedComponents(np.uint8(sure_fg)) # Add one to all the labels so that the sure regions are marked with 1 markers = markers + 1 markers[unknown == 255] = 0 # Mark the unknown regions with zero # Apply the watershed algorithm markers = cv2.watershed(image, markers) image[markers == -1] = [255, 0, 0] # Mark boundaries with red # Display results cv2.imshow('Original Image', image) cv2.imshow('Watershed Segmentation', markers) cv2.waitKey(0) cv2.destroyAllWindows()
In this example, we perform segmentation using the watershed algorithm by creating markers and highlighting the boundaries of different segments.
Image segmentation is a powerful technique that underpins many computer vision applications. With tools like OpenCV in Python, it's easier than ever to implement various segmentation methods to suit your needs. Each technique has its pros and cons, depending on the complexity of the images and the desired application. By experimenting with these methods, you can gain valuable insight into how computer vision works, paving the way for more advanced projects.
06/10/2024 | Python
15/11/2024 | Python
26/10/2024 | Python
06/10/2024 | Python
08/11/2024 | Python
22/11/2024 | Python
14/11/2024 | Python
14/11/2024 | Python
06/10/2024 | Python
21/09/2024 | Python
06/10/2024 | Python
08/12/2024 | Python