Matlab | Dilation of an Image Last Updated : 23 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Morphology is known as the broad set of image processing operations that process images based on shapes. It is also known as a tool used for extracting image components that are useful in the representation and description of region shape. The basic morphological operations are: Erosion 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 a binary image.Create a structuring element or you can use any predefined mask eg. special('sobel').Store the number of rows and columns in an array and loop through it.Create a zero matrix of the size same as 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 the neighborhood.If the value of the neighborhood pixel is 1, then change the value of that pixel to 1.Example: MATLAB % 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(:)); end end imshow(In); Output: Figure: Input imageFigure: Output imageLet's take another example for dilation. Syntax: imread() function is used to read the image.strel() function is used to define the structuring element. We have chosen a disk-shaped SE, of radius 5.imdialate() function is used to perform the dilation operation.imtool() function is used to display the image.Example: Matlab % MATLAB code for Dilation % read the image. k=imread("dilation.png"); % define the structuring element. SE=strel('disk',5); % apply the dilation operation. d=imdilate(k,SE); %display all the images. imtool(k,[]); imtool(d,[]); %see the effective expansion % in original image imtool(d-k,[]); Output: Figure: Left: Original image, Right: Dilated imageFigure: Expansion in the original imageCode Explanation: k=imread("dilation_exmp.png"); this line reads the image.SE=strel('disk',5); this line defines the structuring element.d=imdilate(k,SE); this line applies the dilation operation.imtool(k,[]); this line displays the original image.imtool(e,[]); this line displays the dilated image.imtool(d-k,[]); this line shows the effective expansion in original image.The last image shows the extent to which the original image got dilated. We have used the Structuring element of disk-shaped and the image we used is also circular in shape. This gives us the very desired output to understand erosion. Create Quiz Comment K kanugargng Follow 0 Improve K kanugargng Follow 0 Improve Article Tags : Computer Subject Machine Learning MATLAB Image-Processing MATLAB +1 More Explore Machine Learning BasicsIntroduction to Machine Learning8 min readTypes of Machine Learning7 min readWhat is Machine Learning Pipeline?6 min readApplications of Machine Learning3 min readPython for Machine LearningMachine Learning with Python Tutorial5 min readNumPy Tutorial - Python Library3 min readPandas Tutorial4 min readData Preprocessing in Python4 min readEDA - Exploratory Data Analysis in Python6 min readFeature EngineeringWhat is Feature Engineering?5 min readIntroduction to Dimensionality Reduction4 min readFeature Selection Techniques in Machine Learning4 min readSupervised LearningSupervised Machine Learning7 min readLinear Regression in Machine learning14 min readLogistic Regression in Machine Learning10 min readDecision Tree in Machine Learning8 min readRandom Forest Algorithm in Machine Learning5 min readK-Nearest Neighbor(KNN) Algorithm8 min readSupport Vector Machine (SVM) Algorithm9 min readNaive Bayes Classifiers6 min readUnsupervised LearningWhat is Unsupervised Learning5 min readK means Clustering â Introduction6 min readHierarchical Clustering in Machine Learning6 min readDBSCAN Clustering in ML - Density based clustering6 min readApriori Algorithm6 min readFrequent Pattern Growth Algorithm5 min readECLAT Algorithm - ML5 min readPrincipal Component Analysis (PCA)7 min readModel Evaluation and TuningEvaluation Metrics in Machine Learning9 min readRegularization in Machine Learning5 min readCross Validation in Machine Learning5 min readHyperparameter Tuning5 min readML | Underfitting and Overfitting5 min readBias and Variance in Machine Learning6 min readAdvanced TechniquesReinforcement Learning9 min readSemi-Supervised Learning in ML5 min readSelf-Supervised Learning (SSL)6 min readEnsemble Learning8 min readMachine Learning PracticeMachine Learning Interview Questions and Answers15+ min read100+ Machine Learning Projects with Source Code [2025]6 min read Like