Matlab | Dilation of an Image
Morphology is known as the broad set of image processing operations that process images based on the shapes. It is also known as a tool used for extracting image components that are useful in representation and description of region shape.
The basic morphological operations are:
1. Erosion
2. Dilation
Dilation –
- Dilation expands the image pixels i.e. it is used for expanding an element A by using structuring element B.
- Dilation adds pixels to object boundaries.
- The value of the output pixel is the maximum value of all the pixels in the neighborhood. A pixel is set to 1 if any of the neighboring pixels have the value 1.
Approach:
- Read the RGB image.
- Using function im2bw(), convert the RGB image to binary image.
- Create a structuring element or you can use any predefined mask eg. fspecial(‘sobel’).
- Store the number of rows and columns in and array and loop through it.
- Create a zero matrix of the size same as of the size of our image.
- Leaving the boundary pixels start moving the structuring element on the image and start comparing the pixel with the pixels present in neighborhood.
- If the value of neighborhood pixel is 1, then change the value of that pixel to 1.
Below is the Matlab code for Dilation :
% read image I=imread('lenna.png'); % convert to binary I=im2bw(I); % create structuring element se=ones(5, 5); % store number of rows in P and number of columns in Q. [P, Q]=size(se); % create a zero matrix of size I. In=zeros(size(I, 1), size(I, 2)); for i=ceil(P/2):size(I, 1)-floor(P/2) for j=ceil(Q/2):size(I, 2)-floor(Q/2) % take all the neighbourhoods. on=I(i-floor(P/2):i+floor(P/2), j-floor(Q/2):j+floor(Q/2)); % take logical se nh=on(logical(se)); % compare and take minimum value of the neighbor % and set the pixel value to that minimum value. In(i, j)=max(nh(:)); endend imshow(In); |
chevron_right
filter_none
Input Image:

Output Image:

Recommended Posts:
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- MATLAB | RGB image to grayscale image conversion
- Matlab | Erosion of an Image
- Image Resizing in Matlab
- Image Complement in Matlab
- MATLAB | RGB image representation
- MATLAB | Complement colors in RGB Image
- LSB based Image steganography using MATLAB
- Extract bit planes from an Image in Matlab
- MATLAB | Display histogram of a grayscale Image
- MATLAB | Complement colors in a Grayscale Image
- Flip image across Secondary Diagonal in MATLAB
- Image Processing in MATLAB | Fundamental Operations
- MATLAB | Complement colors in a Binary image
- Matlab | Edge Detection of an image without using in-built function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



