Boundary Extraction of Image using MATLAB


In an image, a boundary is an element that separates an object from the background of the image or two different regions within an image. Boundary provides information about the shape and structure of objects present in the image.

Boundaries are mainly classified into the following two types:

Inner Boundary

It is the boundary of an object within an image that separates it from the image background. The inner boundary is basically the outline of the object and provides information about the shape of the object.

Therefore, by extracting the inner boundary of an object, we can identify and analyze the shape, size, and position of the object within the image.

In MATLAB, we can extract the inner boundary of an image by computing the difference between the original image and the eroded image, i.e.

Inner Boundary = Original Image – Eroded Image

Where, the eroded image is the image obtained by shrinking the original image through erosion.

Outer Boundary

The boundary that surrounds the outermost region of an image or a specific region of interest within an image is referred to as the outer boundary. The outer boundary specifies the outlines of multiple objects within the image or whole image itself.

Therefore, outer boundaries are mainly used to perform various image processing tasks like cropping, changing or removing background, etc.

In MATLAB, the outer boundary of an image can be obtained by computing the difference between dilated image and the original image, i.e.

Outer Boundary = Dilated Image – Original Image

Where, the dilated image is the expanded version of the original image obtained by dilation.

In this article, we will study how to extract boundaries of images using MATLAB codes. The following MATLAB programs demonstrate the extraction of inner and outer boundaries of image.

Example

% MATLAB program to extract inner boundary of an image 
% Read the input image
img = imread('https://www.tutorialspoint.com/electrical_machines/images/electrical_machines_logo.jpg');
% Convert the input image to grayscale
gray_img = rgb2gray(img);
% Specify the structuring element to perform erosion of image
se = strel('disk', 2);
% Perform erosion of the grayscale image
eroded_img = imerode(gray_img, se);
% Extract the inner boundary by subtracting the eroded image from gray image
inner_boundary = gray_img - eroded_img;
% Display the original image and the inner boundary
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(inner_boundary); title('Inner Boundary');

Output

Explanation

This MATLAB code demonstrates the inner boundary extraction of an image. In this MATLAB program, we read the input image by using the ‘imread’ function and then convert it to grayscale using the ‘rgb2gray’ function.

After that we create a structing element ‘se’ to perform erosion of the image, for this we use the ‘strel’ function. In this case, the structure element that we created has a disk shape of a radius of 2. Next, we perform erosion of the image by using the ‘imrode’ function. Due to erosion, the regions within the image are shrunk to eliminate the outer boundary.

Then, we extract the inner boundary by subtracting the eroded image ‘eroded_img’ from the original image ‘gray_img’ and store the result in the ‘inner_boundary’ variable.

Finally, we use the ‘imshow’ function to display the original image and inner boundary.

Example

% MATLAB program to extract outer boundary of an image
% Read the input image
img = imread('https://www.tutorialspoint.com/electrical_machines/images/electrical_machines_logo.jpg');
% Convert the RGB image to grayscale
gray_img = rgb2gray(img);
% Specify a structuring element to perform dilation of the image
se = strel('disk', 2);
% Perform dilation on the image to expand it
dilated_img = imdilate(gray_img, se);
% Extract the outer boundary by computing the difference between dilated image and original image
outer_boundary = dilated_img - gray_img;
% Display the original image and the outer boundary
subplot(1, 2, 1); imshow(img); title('Original Image');
subplot(1, 2, 2); imshow(outer_boundary); title('Outer Boundary');

Output

Explanation

In this MATLAB program, we have extracted outer boundary of the image. For this, we start by reading the image by using the ‘imread’ function. Then, we convert the input image to gray scale by using the ‘rgb2gray’ function. Next, we define a structing element to perform dilation on the image. After that we use the ‘imdilate’ function to perform dilation on the image.

Next, we extract the outer boundary by calculating the difference between the dilated image ‘dilated_img’ and the original image ‘gray_img’ and store the result in the ‘outer_boundary’ variable.

Finally, we call the ‘imshow’ function to display the original image and the outer boundary.

Conclusion

This is how, we can easily perform extraction of boundary of an image using MATLAB programming. MATLAB allows use to extract both inner as well as outer boundary of the image. To extract the inner boundary, the erosion of the image is performed. While to extract the outer boundary, we perform the dilation of the image.

Updated on: 07-Aug-2023

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements