MATLAB Source Code Implementation for Grayscale Image Dilation and Erosion Algorithms
- Login to Download
- 1 Credits
Resource Overview
MATLAB implementation of grayscale image dilation and erosion algorithm source code, featuring morphological operations with customizable structuring elements for image enhancement and feature extraction.
Detailed Documentation
This document provides MATLAB source code for implementing grayscale image dilation and erosion algorithms. These morphological operations help modify image shapes and structures through pixel-level transformations using structuring elements.
% Grayscale Image Dilation Algorithm
function dilatedImage = dilateImage(image, se)
% Implementation of dilation algorithm using maximum filtering
% Input: image - grayscale input matrix, se - structuring element
% Process: Applies maximum value filter within structuring element neighborhood
% Output: dilatedImage - expanded bright regions
[rows, cols] = size(image);
se_center = floor(size(se)/2);
padded_image = padarray(image, se_center, 'replicate');
dilatedImage = zeros(rows, cols);
for i = 1:rows
for j = 1:cols
neighborhood = padded_image(i:i+size(se,1)-1, j:j+size(se,2)-1);
dilatedImage(i,j) = max(neighborhood(se==1));
end
end
end
% Grayscale Image Erosion Algorithm
function erodedImage = erodeImage(image, se)
% Implementation of erosion algorithm using minimum filtering
% Input: image - grayscale input matrix, se - structuring element
% Process: Applies minimum value filter within structuring element neighborhood
% Output: erodedImage - reduced bright regions
[rows, cols] = size(image);
se_center = floor(size(se)/2);
padded_image = padarray(image, se_center, 'replicate');
erodedImage = zeros(rows, cols);
for i = 1:rows
for j = 1:cols
neighborhood = padded_image(i:i+size(se,1)-1, j:j+size(se,2)-1);
erodedImage(i,j) = min(neighborhood(se==1));
end
end
end
% Main Program Execution
% Read input grayscale image
originalImage = imread('input_image.jpg');
if size(originalImage,3)==3
originalImage = rgb2gray(originalImage);
end
% Define structuring element (3x3 square by default)
structuringElement = ones(3,3);
% Apply dilation algorithm
dilatedResult = dilateImage(originalImage, structuringElement);
% Apply erosion algorithm
erodedResult = erodeImage(originalImage, structuringElement);
% Display results
subplot(1,3,1); imshow(originalImage); title('Original Image');
subplot(1,3,2); imshow(dilatedResult); title('Dilated Image');
subplot(1,3,3); imshow(erodedResult); title('Eroded Image');
This code demonstrates fundamental morphological operations where dilation expands bright regions using maximum filtering, while erosion shrinks them through minimum filtering. The implementation uses neighborhood processing with customizable structuring elements, allowing adjustments for different morphological effects. Users can modify the structuring element shape and size to suit specific application requirements in image processing and computer vision tasks.
For further assistance or questions regarding the implementation details, please feel free to contact me.
- Login to Download
- 1 Credits