Image Segmentation using Thresholding in MATLAB

NeuralNinja
3 min readMar 4, 2023

--

Introduction:

An important step in image processing is image segmentation, which seeks to separate an image into segments, each of which represents a different object or area of interest. With a threshold value, foreground (object) pixels are separated from background pixels in the technique known as thresholding, which converts an image into a binary picture. In this tutorial, we’ll go through how to segment images in MATLAB using thresholding.

Code Explanation:

The provided code uses the “imread” function to read an image and initializes the empty segmented img matrix with the same dimensions as the original image. The following thresholding conditions are then applied as it loops through each pixel of the image using two nested “for” loops:

· The segmented image gives the pixel value between 0 and 90, which is deemed to be part of the background, a value of 0.

· A region of interest is considered and given a value of 50 in the segmented image if the pixel value is between 91 and 180.

· The segmented image assigns the pixel a value of 255 and considers it to be in the foreground if its value is larger than 180.

A segmented image with three separate regions is the result of this code: the background (value 0), the region of interest (value 50), and the foreground (value 255).

Conclusion:

Thresholding is a straightforward yet powerful method for segmenting images that may be applied to a variety of tasks, including object detection, identification, and tracking. As demonstrated in the code below, thresholding in MATLAB can be readily accomplished using straightforward conditional statements. To get precise segmentation results, it is crucial to select a suitable threshold value based on the properties of the input image.

Following code can be used to achieve segmentation. It is worth to note that regions should be defined based on image:

% This code performs image segmentation using thresholding

% Input: original_img — input image file name

% Output: segmented_img — segmented image with three regions (background, region of interest, foreground)

% Read the input image

original_img = imread(‘input_image.png’);

% Initialize an empty matrix for the segmented image with the same dimensions as the original image

[r, c] = size(original_img);

segmented_img = zeros(r, c);

% Loop through each pixel of the image and apply thresholding conditions

for i = 1:r

for j = 1:c

% If the pixel value is between 0 and 90, it is considered as part of the background and assigned a value of 0 in the segmented image.

if (original_img(i, j) >= 0) && (original_img(i, j) <= 90)

segmented_img(i, j) = 0;

% If the pixel value is between 91 and 180, it is considered as a region of interest and assigned a value of 50 in the segmented image.

elseif (original_img(i, j) > 90) && (original_img(i, j) <= 180)

segmented_img(i, j) = 50;

% If the pixel value is greater than 180, it is considered as part of the foreground and assigned a value of 255 in the segmented image.

else

segmented_img(i, j) = 255;

end

end

end

% Display the original and segmented images side by side for comparison

figure;

subplot(1, 2, 1); imshow(original_img); title(‘Original Image’);

subplot(1, 2, 2); imshow(segmented_img); title(‘Segmented Image’);

--

--

No responses yet