0% found this document useful (0 votes)
13 views

Ecs-851 File

The document contains 5 programs for image processing tasks. Program 1 performs image morphology operations. Program 2 applies Laplacian transformation. Program 3 applies a low pass filter. Program 4 performs edge detection using the Prewitt operator. Program 5 displays a grayscale image histogram.

Uploaded by

jackpayne2021
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)
13 views

Ecs-851 File

The document contains 5 programs for image processing tasks. Program 1 performs image morphology operations. Program 2 applies Laplacian transformation. Program 3 applies a low pass filter. Program 4 performs edge detection using the Prewitt operator. Program 5 displays a grayscale image histogram.

Uploaded by

jackpayne2021
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
You are on page 1/ 16

BABSAHEB BHIMRAO AMBEDKAR

UNIVERSITY

Digital Image Processing Lab


Practical File (ECS-851)
2023-2024

Submitted by- Submitted to-


Abdulla Ansari Dr. P.D. Saxena
208705 Faculty of CSE
B.tech(CS) 8th Sem

Page | 1
S.no Program Date Sign
1 Write a program for image
morphology (boundary extraction,
interior filling).
2 To apply Laplacian Transformation on Image

3 To Apply Low Pass Filter on the image

4 Program to Implement Edge Detection on an


image (Edge detection)

5 To display the Gray scale images

INDEX

Page | 2
Program No. 1

Write a program for image morphology


(boundary extraction, interior filling).
Input:

Original Image

Code:
 MATLAB

clc;
close all;
clear all;

% Read Colour Image and convert it to a grey


level Image
% Display the original Image
I= imread('image10.png');

%I rgb2gray(RGB);
I1 = imtophat(I, strel('disk', 10));
figure, imshow(I1);
I2 = imadjust(I1);
figure,imshow(I2);

Page | 3
level = graythresh(I2);
BW = im2bw(I2,level);
figure,imshow(BW);
C=~BW;
figure,imshow(C);
D = ~bwdist(C);
D(C) = -Inf;
L = watershed(D);
Wi=label2rgb(L,'hot','w');
figure,imshow(Wi);
im=I;

Call the above function using the MATLAB command window.


Output:

Page | 4
Program No. 2

To apply Laplacian Transformation on Image


Input:

Original Image

Code:
 MATLAB

clc;
close all;
clear all;

% Read Colour Image and convert it to a grey


level Image
% Display the original Image
mycolourimage = imread('image1.jpg');
myimage = rgb2gray(mycolourimage);
subplot(3,3,1);
imshow(myimage); title('Original Image');

% Apply Laplacian Filter


f=fspecial('laplacian');
lapedg = imfilter(myimage,f,'symmetric');

Page | 5
subplot(3,3,7);
imshow(lapedg,[]); title('Laplacian Filter');

Call the above function using the MATLAB command window.


Output:

Page | 6
Program No. 3

To Apply Low Pass Filter on the image

Approach:
Step 1: Input – Read an image
Step 2: Saving the size of the input image in pixels
Step 3: Get the Fourier Transform of the input_image

Step 4: Assign the Cut-off Frequency


Step 5: Designing filter: Ideal Low Pass Filter
Step 6: Convolution between the Fourier Transformed input image and the filtering mask
Step 7: Take Inverse Fourier Transform of the convoluted image
Step 8: Display the resultant image as output
Implementation in MATLAB:

% MATLAB Code | Ideal Low Pass Filter

% Reading input image : input_image


input_image = imread('[name of input image
file].[file format]');

% Saving the size of the input_image in


pixels-
% M : no of rows (height of the image)

Page | 7
% N : no of columns (width of the image)
[M, N] = size(input_image);

% Getting Fourier Transform of the


input_image
% using MATLAB library function fft2 (2D
fast fourier transform)
FT_img = fft2(double(input_image));

% Assign Cut-off Frequency


D0 = 30; % one can change this value
accordingly

% Designing filter
u = 0:(M-1);
idx = find(u>M/2);
u(idx) = u(idx)-M;
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;

% MATLAB library function meshgrid(v, u)


returns
% 2D grid which contains the coordinates of
vectors
% v and u. Matrix V with each row is a copy
% of v, and matrix U with each column is a
copy of u
[V, U] = meshgrid(v, u);

Page | 8
% Calculating Euclidean Distance
D = sqrt(U.^2+V.^2);

% Comparing with the cut-off frequency and


% determining the filtering mask
H = double(D <= D0);

% Convolution between the Fourier


Transformed
% image and the mask
G = H.*FT_img;

% Getting the resultant image by Inverse


Fourier Transform
% of the convoluted image using MATLAB
library function
% ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));

% Displaying Input Image and Output Image


subplot(2, 1, 1), imshow(input_image),
subplot(2, 1, 2), imshow(output_image, [ ]);

Input Image – Output Image-

Page | 9
Program No. 4
Program to Implement Edge Detection on an image(Edge detection)
.Approach:

Step 1: Input – Read an image


Step 2: Convert the true-color RGB image to the grayscale image
Step 3: Convert the image to double
Step 4: Pre-allocate the filtered_image matrix with zeros
Step 5: Define Prewitt Operator Mask
Step 6: Edge Detection Process (Compute Gradient approximation and magnitude of vector)
Step 7: Display the filtered image
Step 8: Thresholding on the filtered image
Step 9: Display the edge-detected image
Implementation in MATLAB:
% MATLAB Code | Prewitt Operator from Scratch

% Read Input Image


input_image = imread('[name of input image file].[file format]');

% Displaying Input Image


input_image = uint8(input_image);
figure, imshow(input_image); title('Input Image');

% Convert the truecolor RGB image to the grayscale image


input_image = rgb2gray(input_image);

% Convert the image to double


input_image = double(input_image);

% Pre-allocate the filtered_image matrix with zeros


filtered_image = zeros(size(input_image));

% Prewitt Operator Mask


Mx = [-1 0 1; -1 0 1; -1 0 1];
My = [-1 -1 -1; 0 0 0; 1 1 1];

% Edge Detection Process


% When i = 1 and j = 1, then filtered_image pixel
% position will be filtered_image(2, 2)
% The mask is of 3x3, so we need to traverse
% to filtered_image(size(input_image, 1) - 2
%, size(input_image, 2) - 2)
Page | 10
% Thus we are not considering the borders.
for i = 1:size(input_image, 1) - 2
for j = 1:size(input_image, 2) - 2

% Gradient approximations
Gx = sum(sum(Mx.*input_image(i:i+2, j:j+2)));
Gy = sum(sum(My.*input_image(i:i+2, j:j+2)));

% Calculate magnitude of vector


filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);

end
end

% Displaying Filtered Image


filtered_image = uint8(filtered_image);
figure, imshow(filtered_image); title('Filtered Image');

% Define a threshold value


thresholdValue = 100; % varies between [0 255]
output_image = max(filtered_image, thresholdValue);
output_image(output_image == round(thresholdValue)) = 0;

% Displaying Output Image


output_image = im2bw(output_image);
figure, imshow(output_image); title('Edge Detected Image');

Input Image –

Page | 11
Filtered Image:

Edge Detected Image:

Advantages:
1. Good performance on detecting vertical and horizontal edges
2. Best operator to detect the orientation of an image
Limitations:
1. The magnitude of coefficient is fixed and cannot be changed
2. Diagonal direction points are not preserved always

Page | 12
Program No. 5
To display the Gray scale images
Approach :
 Read the source image file into image matrix
 Convert it to grayscale, if it is an RGB image
 Iterate over image matrix and count the frequency of every possible value of intensity
 plot the counted frequency

% Read source image file


img = imread('apple.jpg');

% Convert image to grayscale image


img=rgb2gray(img);

% get the dimension of the image


[x, y] = size(img);

% Create a frequency array of size 256


frequency = 1 : 256;

count = 0;

% Iterate over grayscale image matrix


% for every possible intensity value
% and count them

for i = 1 : 256
for j = 1 : x
for k = 1 : y

Page | 13
% if image pixel value at location (j, k)
is i-1
% then increment count
if img(j, k) == i-1
count = count + 1;
end
end
end
% update ith position of frequency array
with count
frequency(i) = count;

% reset count
count = 0;

end

n = 0 : 255;

% Display Histogram
stem(n, frequency);

grid on;
ylabel('Number of pixels with such intensity
levels -->');
xlabel('Intensity Levels -->');
title('HISTOGRAM OF THE IMAGE');

Page | 14
Input:

Output:

Page | 15
Page | 16

You might also like