Thresholding-Based Image Segmentation in MATLAB


In computer−based image analysis and processing, the image segmentation is an essential task. The image segmentation allows us to extract specific objects or regions from an image. In digital image processing, the most widely used image segmentation technique is the thresholding−based image segmentation. This image segmentation technique works depending on the pixel intensity values to separate different regions within an image.

In this article, we will discuss learn the implementation of thresholding−based image segmentation using MATLAB programming. But before that let’s get into the basics of thresholding and its types.

What is Thresholding?

In digital image processing, the thresholding is a method of separating the foreground and background regions of an image based on their pixel intensity. For this, we have to specify a threshold value of the pixel intensity. Then, the image processing tool, in our case MATLAB, separates the regions of pixel intensities above and below the specified threshold value into two different regions.

Types of Thresholding

Based on the different requirements of images and image segmentation, there are several different types of thresholding techniques have been developed. These thresholding methods are described below:

  • Global Thresholding − In the case of global thresholding, a constant or fixed thresholding value is specified for segmentation of the entire image. This thresholding technique is mainly used on images with even lighting conditions.

  • Adaptive Thresholding − The adaptive thresholding technique is applied for segmentation of images with uneven lighting conditions and having intensity variations. This thresholding technique first divide the image into smaller regions and then apply a specific thresholding value locally to each region for segmentation.

  • Otsu’s Thresholding − The Otsu’s thresholding technique is capable to automatically determine an optimal threshold value based on the image for segmentation. For this, it maximizes the interclass variance between the foreground and background pixel values of the image.

After getting an overview of thresholding and its types, let us now learn the implementation of thresholding−based image segmentation using MATLAB programming.

Example

% MATLAB program for thresholding-based image segmentation using global thresholding
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Convert the RGB image to grayscale
gray_img = rgb2gray(img);
% Compute the global threshold value
GT_value = graythresh(gray_img);
% Perform global thresholding based image segmentation
binary_img = imbinarize(gray_img, GT_value);
% Display the original and globally segmented images
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(binary_img); title('Global Segmented Image');

Output

Explanation

In this MATLAB program, we have demonstrated the implementation of thresholding−based image segmentation using the global thresholding technique.

In this code, we start by reading the input image, then convert it into a grayscale image if it is not. Next, we use the ‘graythresh’ function to determine the global threshold value for the given image. After that we perform the global thresholding on the gray scale image by using the global threshold value and the ‘imbinarize’ function. Finally, we use the ‘imshow’ function to display the original and global segmented images.

Example

% MATLAB program for thresholding-based image segmentation using adaptive thresholding
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Convert the RGB image to grayscale
gray_img = rgb2gray(img);
% Calculate the adaptive threshold value
AT_value = adaptthresh(gray_img, 0.5); % Set the sensitivity parameter if required
% Perform adaptive thresholding-based image segmentation
adaptive_img = imbinarize(gray_img, AT_value);
% Display the original and adaptive segmented images
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(adaptive_img); title('Adaptive Segmented Image');

Output

Explanation

This MATLAB program demonstrates the implementation of thresholding−based image segmentation using the adaptive thresholding method.

In this MATLAB code, first we read the input image using the ‘imread’ function, then convert it into a grayscale image if it is not. Next, we use the ‘adaptthresh’ function to determine the adaptive threshold value for the given image. After that we perform the adaptive thresholding segmentation of the gray scale image by using the adaptive threshold value and the ‘imbinarize’ function. Finally, we use the ‘imshow’ function to display the original and adaptive segmented images with a suitable title.

Example

% MATLAB program for thresholding-based image segmentation using Otsu’s thresholding
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
% Convert the image to grayscale if required
if size(img, 3) > 1
    gray_img = rgb2gray(img);
end
% Calculate the histogram for the image
[counts, x] = imhist(gray_img, 16);
% Calculate the Otsu's thresholding value 
Otsu_value = otsuthresh(counts);
% Perform the Otsu's thresholding-based image segmentation
Otsu_img = imbinarize(gray_img, Otsu_value);
% Display the original and Otsu’s segmented image
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(Otsu_img); title('Otsu Segmented Image');

Output

Explanation

This MATLAB program performs thresholding−based image segmentation using Otsu’s thresholding method.

In this code, we first read the input image by using the ‘imread’ function, then convert it to grayscale image if required. Next, we use the ‘imhist’ function to compute the histogram of the gray image to get the information of pixel intensities in the image. After that we compute the Otsu’s thresholding value by using the ‘otsuthresh’ function.

Next, we use the ‘imbinarize’ function to perform Otsu;s thresholding−based image segmentation of the input image. Finally, we call the ‘imshow’ function to display the original and Otsu segmented images.

Conclusion

Hence, this is how we can perform thresholding−based image segmentation using MATLAB programming. MATLAB provides three types of thresholding namely, global, adaptive, and Otsu’s thresholding to perform image segmentation. The thresholding−based image segmentation is a crucial task in computer−based analysis of images. The thresholding−based image segmentation is most commonly used, because it provides a simple and effective approach to extract the objects/regions from an image. Also, MATLAB provides various built−in functions and simple programming environment to perform the thresholding−based image segmentation.

Updated on: 08-Aug-2023

461 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements