0% found this document useful (0 votes)
3 views2 pages

Programme 4

The document outlines a MATLAB program to implement a smoothing or averaging filter on a grayscale image. It reads an image, applies a 3x3 averaging filter using convolution, and saves the resulting blurred image. The final output is displayed using the imshow function.

Uploaded by

Roshita Verma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Programme 4

The document outlines a MATLAB program to implement a smoothing or averaging filter on a grayscale image. It reads an image, applies a 3x3 averaging filter using convolution, and saves the resulting blurred image. The final output is displayed using the imshow function.

Uploaded by

Roshita Verma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAMME-4

To Implement smoothing or averaging filter in spatial domain

% Read the grayscale image

img = imread('[Link]');

% Convert to grayscale if image is RGB

if size(img, 3) == 3

img = rgb2gray(img);

end

% Convert image to double for calculation

img = double(img);

% Get image size

[m, n] = size(img);

% Create 3×3 averaging filter

mask = ones(3, 3) / 9;

% Initialize output image

img_new = zeros(m, n);

% Apply averaging filter using convolution

for i = 2:m-1

for j = 2:n-1

temp = ...

img(i-1, j-1) * mask(1,1) + ...

img(i-1, j) * mask(1,2) + ...


img(i-1, j+1) * mask(1,3) + ...

img(i, j-1) * mask(2,1) + ...

img(i, j) * mask(2,2) + ...

img(i, j+1) * mask(2,3) + ...

img(i+1, j-1) * mask(3,1) + ...

img(i+1, j) * mask(3,2) + ...

img(i+1, j+1) * mask(3,3);

img_new(i, j) = temp;

end

end

% Convert result to uint8

img_new = uint8(img_new);

% Save the output image

imwrite(img_new, '[Link]');

% Display result

figure, imshow(img_new), title('Smoothed Image');

You might also like