0% found this document useful (0 votes)
7 views56 pages

Imageprocessing - Docx 2

Uploaded by

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

Imageprocessing - Docx 2

Uploaded by

venkynaidu9121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

FACULTY OF ENGINEERING AND TECHNOLOGY

BACHELOR OF TECHNOLOGY

Image Processing Laboratory


(303105382)

SEMESTER VII
Department of
Artificial Intelligence & Data Science
CERTIFICATE

This is to certify that

Mr. B.Manmath with Enrollment No. 210303124252 has successfully

completed his laboratory experiments in the Image Processing Lab

(303105308) from the department of Artificial Intelligence & Data

Science during the academic year 2025-2026.

Date of Submission ….…………… Staff In charge …..……………

Head of Department………….……
TABLE OF CONTENTS

Date of
Experiment Page No Performance
Date of
SR.NO Title Submission Marks Sign

From To

1 To study and implement basic commands of


MATLAB required for digital image
processing techniques, and various image file
formats

2 To study the effect of down sampling and


quantization techniques on the grayscale
image.

3 Write MATLAB code to enhance the visual


quality of the image using point processing
techniques, namely

a) Image negative

b) Logarithmic transformation

c) Power law transformation and gray level


slicing technique

4 Write MATLAB code to display the histogram


of the image and enhance the visual quality of
the image with the help of histogram
equalization technique

5 Write MATLAB code to perform the bit-plane


slicing method on grayscale images.

6 Write MATLAB code to demonstrate that the


convolution in the spatial domain is equivalent
to multiplication in the frequency domain.

7 Write MATLAB code to restore grayscale


images from noisy images with the help of
image restoration techniques.
8 Write MATLAB code to performMin, Median
& Max Filtering on grayscale image

9 Write MATLAB code to blur the image using


Ideal low pass, Butterworth low pass and
Gaussian Low pass filter.

10 Write MATLAB code to blur the image using


Ideal High pass, Butterworth High pass and
Gaussian High pass filter.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 01

Aim: To study and implement basic commands of MATLAB required for digital image
processing techniques, and various image file formats.

Objective: To study and implement basic MATLAB commands used in digital image
processing, including reading, displaying, transforming, and analyzing images, and to
understand different image file formats supported by MATLAB.

Procedure:

1. clc
Syntax:

clc

Description:
Clears the Command Window (output screen), but does not affect variables or figures.

Example:

clc

2. clear all
Syntax:

clear all

Description:
Removes all variables from the workspace, freeing up memory.

Example:

clear all
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

3. close all
Syntax:

close all

Description:
Closes all open figure windows.

Example:

close all

4. imread()
Syntax:

img = imread('filename.ext');

Description:
Reads an image file into MATLAB as a matrix.

Example:

img = imread(‘saturn.png');

5. imwrite()

Syntax:

imwrite(img, 'filename.jpg');

Description:
Saves the image to a file.

Example:

imwrite(img, 'output_gray.jpg');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

6. imshow()
Syntax:

imshow(img);

Description:
Displays an image in a figure window.

Example:

imshow(img);

Output:

7. title()

Syntax:

title('Image Title')

Description:
Adds a title above the current image or plot.

Example:

imshow(img);
title('Original Image');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:

8. subplot()

Syntax:

subplot(m, n, p)

Description:
Divides the figure into an m × n grid and activates the pth section for plotting.

Example:

subplot(1,2,1); imshow(img);
subplot(1,2,2); imshow(rgb2gray(img));

Output:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

9. figure

Syntax:

figure

Description:
Opens a new figure window for displaying plots/images.

Example:

figure; imshow(img);

Output:

10. imhist()

Syntax:

imhist(img);

Description:
Displays the histogram of a grayscale image.

Example:

imhist(img);
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:

11. addpath()

Syntax:

addpath('folder_path')

Description:
Adds a directory to MATLAB’s search path to access files from that folder.

Example:

addpath('C:\Users\kunj\Documents\Images');

12. imfinfo()

Syntax:

info = imfinfo('filename.jpg')

Description:
Returns metadata about the image file, such as resolution, color type, and size.

Example:

addpath('C:\Users\kunj\Documents\Images');

Output:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

13. imresize()

Syntax:

resizedImg = imresize(img, scale);

Description:
Resizes the image by a scale factor.

Example:

halfImg = imresize(img, 0.5); % 50% of original size


imshow(halfImg);

Output:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

14. rgb2gray()

Syntax:

grayImg = rgb2gray(img);

Description:
Converts a color image to grayscale.

Example:

grayImg = rgb2gray(img);
imshow(grayImg);

Output:

15. imadjust()

Syntax:

newImg = imadjust(img);

Description:
Enhances contrast of the image.

Example:

gray_img = rgb2gray(img); % Convert to grayscale


enhanced = imadjust(gray_img);
imshow(enhanced);
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:

16. edge()

Syntax:

edges = edge(grayImg, 'method');

Description:
Detects edges in the image using a specific method ('sobel', 'canny', etc.).

Example:

gray_img = rgb2gray(img); % Convert to grayscale


edges = edge(gray_img, 'sobel'); % Apply Sobel edge detection
imshow(edges);

Output:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

17. imfilter()

Syntax:

filteredImg = imfilter(img, h);

Description:
Applies a filter to an image.

Example:

h = fspecial('average', [5 5]);
smoothImg = imfilter(gray_img, h);
imshow(smoothImg);

Output:

18. imnoise()

Syntax:

noisyImg = imnoise(img, 'type');

Description:
Adds noise to the image ('salt & pepper', 'gaussian', etc.).

Example:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

noisy = imnoise(gray_img, 'salt & pepper');


imshow(noisy);

Output:

19. medfilt2()

Syntax:

filtered = medfilt2(grayImg);

Description:
Applies median filtering to remove noise.

Example:

noisy = rgb2gray(img);
filtered = medfilt2(noisy);
imshow(filtered);

Output:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

20. imbinarize()

Syntax:

bw = imbinarize(grayImg);

Description:
Converts a grayscale image to binary (black and white).

Example:

ngrayImg = rgb2gray(img);
bw = imbinarize(grayImg);
imshow(bw);

Output:

21. size()

Syntax:

[rows, cols] = size(img);

Description:
Returns the number of rows and columns in the image.

Example:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

[r, c, d] = size(img);
fprintf('Rows: %d, Columns: %d, Channels: %d\n', r, c, d);

Output:

Commands in brief

Command Syntax Description Example

clc clc Clears command window clc

clear all clear all Removes all variables from clear all
workspace

close all close all Closes all figure windows close all

imread imread('file.jpg') Reads an image from file img = imread('peppers.png');

title title('text') Adds title to current plot title('Original Image');

subplot subplot(m, n, p) Creates subplots in figure subplot(2,2,1);

figure figure Opens new figure window figure; imshow(img);

imshow imshow(img) Displays an image imshow(img);


Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Command Syntax Description Example

double double(var) Converts data to double precision A = double(img);

imagesc imagesc(data) Scales and displays matrix as image imagesc(grayImg);

addpath addpath('folder') Adds folder to MATLAB path addpath('C:\images')

clf clf Clears current figure window clf

imwrite imwrite(img, 'file.jpg') Saves image to file imwrite(img, 'output.jpg');

imfinfo imfinfo('file.jpg') Displays image file information info = imfinfo('peppers.png');

who who Lists variables in workspace who

whos whos Lists variables with details whos

Conclusion:

- Basic MATLAB commands for image processing were successfully implemented. We


learned how to read, display, convert, resize, and save images, and understood different
image formats. This forms the foundation for further image processing techniques.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 02

Aim: To study the effect of down sampling and quantization techniques on the grayscale
image.

Objective: To study and visualize the effect of downsampling and quantization on a grayscale
image using MATLAB.

Description (Theory in brief):

● Down Sampling: Reducing the resolution of the image by decreasing the number of
pixels (e.g., selecting every 2nd or 4th pixel).

● Quantization: Reducing the number of gray levels (e.g., 256 to 16 levels), which
compresses data but may reduce quality.

● Interpolation is the technique of estimating new pixel values when resizing images. It
helps maintain visual quality during enlargement or reduction.
○ Nearest Neighbor: Copies the value of the closest pixel (fast, but blocky).
○ Bilinear: Takes the average of surrounding 4 pixels (smoother).
○ Bicubic: Uses 16 neighboring pixels for smoother and more detailed results (best
quality, slower).
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Procedure :
Code (using external image):

clc;
clear all;
close all;

%% -------- Load and Prepare Images --------


% Load and convert to grayscale
i = imread('imgp2.png');
gray = rgb2gray(i);

% Manually create a binary 8x8 image


binary_img = [ ...
1 0 0 1 1 0 1 0;
1 1 0 1 0 0 1 1;
0 0 1 0 1 1 0 0;
1 1 1 0 0 0 1 0;
0 1 0 0 1 1 0 1;
1 0 1 1 0 0 1 1;
0 1 1 0 1 0 0 0;
1 0 0 1 0 1 1 0];
binary_img = logical(binary_img);

[r, c] = size(gray);
[rb, cb] = size(binary_img);

%% -------- 1. Downsampling (Both Images) --------


figure('Name', 'Downsampling - Grayscale & Binary');

% Grayscale
subplot(2,4,1); imshow(gray); title('Gray Original');
subplot(2,4,2); imshow(gray(1:2:r, 1:2:c)); title('Gray x2');
subplot(2,4,3); imshow(gray(1:3:r, 1:3:c)); title('Gray x3');
subplot(2,4,4); imshow(gray(1:4:r, 1:4:c)); title('Gray x4');

% Binary
subplot(2,4,5); imshow(binary_img); title('Binary Original');
subplot(2,4,6); imshow(binary_img(1:2:rb, 1:2:cb)); title('Binary x2');
subplot(2,4,7); imshow(binary_img(1:3:rb, 1:3:cb)); title('Binary x3');
subplot(2,4,8); imshow(binary_img(1:4:rb, 1:4:cb)); title('Binary x4');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

%% -------- 2. Interpolation (Both Images) --------


figure('Name', 'Interpolation - Grayscale & Binary');

% Grayscale
subplot(2,3,1); imshow(imresize(gray, 0.3, 'nearest')); title('Gray Nearest');
subplot(2,3,2); imshow(imresize(gray, 0.5, 'bilinear')); title('Gray Bilinear');
subplot(2,3,3); imshow(imresize(gray, 0.7, 'bicubic')); title('Gray Bicubic');

% Binary
subplot(2,3,4); imshow(imresize(binary_img, 3, 'nearest')); title('Binary Nearest');
subplot(2,3,5); imshow(imresize(binary_img, 3, 'bilinear')); title('Binary Bilinear');
subplot(2,3,6); imshow(imresize(binary_img, 3, 'bicubic')); title('Binary Bicubic');

%% -------- 3. Quantization (Both Images) --------


gray_binary = uint8(binary_img * 255); % convert binary to grayscale values

figure('Name', 'Quantization - Grayscale & Binary');

% Grayscale quantization
subplot(2,2,1); imshow(uint8(floor(double(gray)/16)*16)); title('Gray to 16 levels');
subplot(2,2,2); imshow(uint8(floor(double(gray)/32)*32)); title('Gray to 8 levels');

% Binary quantization
subplot(2,2,3); imshow(uint8(floor(double(gray_binary)/128)*128)); title('Binary to 2
levels');
subplot(2,2,4); imshow(uint8(floor(double(gray_binary)/255)*255)); title('Binary to 1
level');

Input:

Image Binary Image

1 0 0 1 1 0 1 0;
1 1 0 1 0 0 1 1;
0 0 1 0 1 1 0 0;
Paste Your Image here 1 1 1 0 0 0 1 0;
And change in outputs also 0 1 0 0 1 1 0 1;
1 0 1 1 0 0 1 1;
0 1 1 0 1 0 0 0;
10010110
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:
Down Sampling

Figure 1 :

Interpolation Methods

Figure 2 :
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Quantization

Figure 3 :

Output Windows You’ll See (for both binary and external image) :

1. Figure 1: Original and downsampled versions

2. Figure 2: Resized using nearest(Blocky), bilinear(smoothy),


bicubic(smoother)

3. Figure 3: Quantized to 16 and 8 levels.

Conclusion:

- The experiment successfully demonstrated the effects of down sampling,


interpolation, and quantization on a grayscale image and binary image. Down
sampling reduced spatial resolution, interpolation showed varying reconstruction
quality, and quantization reduced gray levels with visible intensity banding.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 03

Aim: Write MATLAB code to enhance the visual quality of the image using point
processing techniques, namely:

a. Image negative
b. Logarithmic transformation
c. Power law transformation and gray level slicing technique

Objective: To enhance the visual quality of a grayscale image using point processing
techniques, including:

- Image Negative

- Logarithmic Transformation

- Power Law (Gamma) Transformation

- Gray Level Slicing (Thresholding)

Description (Theory in brief):

● Image Negative: Enhances white or bright regions by inverting the pixel intensities.
○ Formula: s = L - 1 - r

● Logarithmic Transformation: Expands dark pixels and compresses bright regions.


○ Formula: s = c * log(1 + r)

● Power Law (Gamma) Transformation: Enhances contrast using a power function.


○ Formula: s = c * r^γ
○ (γ < 1 brightens; γ > 1 darkens)

● Gray Level Slicing (Thresholding): Highlights specific intensity ranges by assigning


pixels above or below a threshold to 0 or 255.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Code:
clc;
clear all;
close all;

% Load and convert to grayscale


a = imread(pu.jpeg');
i = rgb2gray(a);
i = double(i);

% 1. Negative Image
g = 255 - i;
subplot(4,2,1);
imshow(uint8(i));
title('Original Image');
subplot(4,2,2);
imshow(uint8(g));
title('Negative Image');

% 2. Logarithmic Transformation
c = 30;
s1 = c * log(1 + i);
subplot(4,2,3);
imshow(uint8(s1));
title('Log Transformation');

% 3. Antilog Transformation (Optional - Not commonly used)


s2 = (exp(i / c)) - 1;
subplot(4,2,4);
imshow(uint8(s2));
title('Antilog Transformation');

% 4. Power Law (Gamma) Transformation


gamma = 0.4;
s3 = c * (i .^ gamma);
subplot(4,2,5);
imshow(uint8(s3));
title('Power Law (Gamma = 0.4)');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

% 5. Gray Level Slicing (Thresholding)


[r, c] = size(i);
th = 100;
i_thresh = zeros(r, c);

for x = 1:r
for y = 1:c
if i(x,y) <= th
i_thresh(x, y) = 0;
else
i_thresh(x, y) = 255;
end
end
end

subplot(4,2,6);
imshow(uint8(i_thresh));
title('Thresholded Image');

Input Image:

(Pu.jpeg)
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:

Output Windows You’ll See:

Original Image: Grayscale car photo

- Negative Image: Inverted brightness — highlights car edges

- Log Transformation: Enhances dark regions — useful in shadows

- Antilog: Brightens image sharply (less common)

- Gamma (0.4): Enhances mid-tones — brings out body details

- Thresholded: Shows car shape in black & white — based on brightness

Conclusion:

- Image enhancement techniques like negative, log, gamma, and thresholding were
successfully applied, improving visual quality and highlighting important features
in the grayscale car image.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 04

Aim: Write MATLAB code to display the histogram of the image and enhance the visual
quality of the image with the help of histogram equalization technique.

Objective: Understand the concept of image histograms and their significance in image
processing.

● Display the histogram of an image using MATLAB.

● Enhance the visual quality of an image using histogram equalization.

● Apply both global and adaptive histogram equalization methods to observe


contrast improvement.

Description (Theory in brief):

Image Histogram:

● An image histogram is a graphical representation of the distribution of pixel


intensities in an image.

● The x-axis represents intensity values (0–255 for 8-bit images), and the y-axis
shows the frequency of each intensity.

● It helps to analyze the contrast, brightness, and intensity spread of the image.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Histogram Equalization:

● Histogram equalization improves image contrast by redistributing the pixel


intensities more evenly.

● It uses the cumulative distribution function (CDF) of the histogram to remap


pixel values.

● The transformation function is:

Where:

● L = number of intensity levels (usually 256)

● M×N = total number of pixels

● nj = number of pixels with intensity value j

Adaptive Histogram Equalization:

● This technique enhances contrast locally in small regions of the image.

● It avoids over-enhancement or artifacts in areas with uniform brightness.


Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Code:
clc;
clear all;
close all;

% Read and display the original color image


a = imread('Space.png');
figure;
imshow(a);
title('Original Color Image');

% Convert the image to grayscale


i = rgb2gray(a);

%
% 1. Basic Histogram Adjustment
%
J = imadjust(i);

figure;
subplot(3,3,1);
imshow(J);
title('Adjusted Image');

subplot(3,3,2);
imhist(J);
title('Histogram of Adjusted Image');

%
% 2. Contrast Adjustment using Specific Range
%
J1 = imadjust(i, [0.4 0.5], [0.4 0.5]);
subplot(3,3,3);
imshow(J1);
title('Contrast Adjusted');

subplot(3,3,4);
imhist(J1);
title('Histogram of Contrast Adjust');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

%
% 3. Stretching the Image Intensity
%
S = imadjust(i, stretchlim(i), [0.10 0.99]);
subplot(3,3,5);
imshow(S);
title('Intensity Stretch');

subplot(3,3,6);
imhist(S);
title('Histogram of Stretch');

%
% 4. Adaptive Histogram Equalization
%
AH = adapthisteq(i);
subplot(3,3,7);
imshow(i);
title('Original Grayscale Image');

subplot(3,3,8);
imshow(AH);
title('Adaptive Histogram Equalization');

subplot(3,3,9);
imhist(AH);
title('Histogram of Adaptive Equalized Image');

Input Image:

(Space.png)
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:

Conclusion:

- The experiment demonstrated how to visualize image histograms and apply


histogram equalization in MATLAB.

- Histogram equalization significantly improves image contrast by spreading


intensity values across the full dynamic range.

- Adaptive histogram equalization further enhanced local contrast, preserving


details in different regions of the image.

- These techniques are useful in enhancing medical images, satellite imagery, and
any low-contrast grayscale images.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 05

Aim: Write MATLAB code to perform the bit-plane slicing method on grayscale images.

Objective: To extract and visualize individual bit planes (from bit 1 to bit 8) of a grayscale
image to analyze how different bits contribute to the image's overall appearance and
information content.

Description (Theory in brief):

● In digital image processing, each pixel of an 8-bit grayscale image is represented using
8 bits (ranging from bit-plane 1 to bit-plane 8). Bit-plane slicing involves isolating each
of these 8 bits across all pixels to see their individual contributions:

○ Bit 1 (LSB) contains the least amount of visual information.

○ Bit 8 (MSB) contributes the most to the image’s structure.

● This technique is useful for data compression, watermarking, and visual analysis.

● Bit-plane slicing helps in:

○ Analyzing how different bits contribute to the image.


○ Performing compression, watermarking, or encryption
○ Reconstructing an image using only significant bit planes (e.g., 5 to 8).
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Code:
clc;
clear all;
close all;

% Step 1: Load your own colored image


i = imread(spaceship.png');
gray = rgb2gray(i); % Convert to grayscale

% Step 2: Bit slicing


a1 = bitget(gray, 1);
a2 = bitget(gray, 2);
a3 = bitget(gray, 3);
a4 = bitget(gray, 4);
a5 = bitget(gray, 5);
a6 = bitget(gray, 6);
a7 = bitget(gray, 7);
a8 = bitget(gray, 8);
a9 = im2bw(gray); % Binary conversion

% Step 3: Grouped Figures

%% --- Figure 1: Original and Grayscale


figure();
subplot(1,2,1), imshow(i), title('Original Color Image');
subplot(1,2,2), imshow(gray), title('Grayscale Image');

%% --- Figure 2: Bit Planes 1 to 8


figure();
subplot(2,4,1), imshow(a1, []), title('Bit Plane 1');
subplot(2,4,2), imshow(a2, []), title('Bit Plane 2');
subplot(2,4,3), imshow(a3, []), title('Bit Plane 3');
subplot(2,4,4), imshow(a4, []), title('Bit Plane 4');
subplot(2,4,5), imshow(a5, []), title('Bit Plane 5');
subplot(2,4,6), imshow(a6, []), title('Bit Plane 6');
subplot(2,4,7), imshow(a7, []), title('Bit Plane 7');
subplot(2,4,8), imshow(a8, []), title('Bit Plane 8');

%% --- Figure 3: Binary & Bitwise Operations


comb2 = imlincomb(1, a5, 1, a6, 1, a7, 1, a8); % Combine bit 5–8
bit1 = bitand(comb2, gray); % Bitwise AND
bit2 = bitset(comb2, 8); % Bitwise SET

figure();
subplot(2,2,1), imshow(a9), title('Binary (im2bw)');
subplot(2,2,2), imshow(bit1, []), title('bitand() Result');
subplot(2,2,3), imshow(bit2, []), title('bitset() Result');
subplot(2,2,4), imshow(comb2, []), title('Combined (Bits 5–8)');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output:

Figure 1:

Figure 2:

Figure 3:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output Windows You’ll See:

Figure: Bit-plane Slicing Results

1. Original Color Image – Your selected colored input image.

2. Grayscale Image – Converted version used for slicing.

3. Bit Plane 1 to Bit Plane 8 –

○ Bit 1 (Least Significant Bit) → very noisy, minor details

○ Bit 8 (Most Significant Bit) → major structure, most important features

○ Higher bits show clearer parts of the image, lower bits show noise or fine
textures

4. Binary Image – Thresholded version of the grayscale image using im2bw.

Bitwise Operations Output

5. bitand() Result – Highlights shared features between grayscale image and


combined bit layers.

6. bitset() Result – Alters the 8th bit of the combined image to emphasize its
contribution.

7. Combined (Bit 5–8) – Logical addition of higher-order bit planes. Shows the
reconstructed main structure of the image using only important bits.

Conclusion:

- Bit-plane slicing successfully isolates the contribution of each bit in an 8-bit


grayscale image. Higher-order bits (e.g., bits 7 and 8) contain most of the
significant image information, while lower-order bits contribute to finer details or
noise. This method is useful for analyzing image structure, compression, and
digital watermarking.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 06

Aim: Write MATLAB code to demonstrate that the convolution in the spatial domain is
equivalent to multiplication in the frequency domain.

Objective: To demonstrate that convolution in the spatial domain is equivalent to


multiplication in the frequency domain using MATLAB.

Description (Theory in brief):

● According to the Convolution Theorem in signal processing:

○ Convolution in the spatial domain (image space) is equivalent to


Point-wise multiplication in the frequency domain (Fourier space).

● This is mathematically represented as:

○ Where:
■ * is convolution

■ × is element-wise multiplication

■ F(u,v) and H(u,v) are the Fourier transforms of the image and
filter respectively.

● This property is used to speed up filtering operations using Fast Fourier


Transform (FFT).
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Code:
clc;
clear;
close all;

%% Generating the First Binary Image


A = zeros(256); % create black image
A(100:150, 100:150) = 1; % white square
figure;
imshow(A);
title('First Binary Image');

%% Generating the Second Binary Image


B = zeros(256); % create black image
B(120:170, 120:200) = 1; % white rectangle
figure;
imshow(B);
title('Second Binary Image');

%% Convolution in Spatial Domain


C = conv2(A, B, 'same');

%% Multiplication in Frequency Domain


A1 = fft2(A);
B1 = fft2(B);
C1 = A1 .* B1;
D = fftshift(ifft2(C1));

%% Display Results
figure;
subplot(3,2,1);
imshow(A);
title('First Binary Image');

subplot(3,2,2);
imshow(B);
title('Second Binary Image');

subplot(3,2,3);
imshow(log(1 + abs(fftshift(A1))), []);
title('DFT of First Image');

subplot(3,2,4);
imshow(log(1 + abs(fftshift(B1))), []);
title('DFT of Second Image');

subplot(3,2,5);
imshow(C, []);
title('Convolution (Spatial Domain)');

subplot(3,2,6);
imshow(abs(D), []);
title('Multiplication (Frequency Domain)');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

First Binary Image : Second Binary Image:

Result of First and Second Image:

Output Windows You’ll See:

- First Binary Image

- A black 256×256 image with a white square in the middle (pixels 100–150 in
both x and y directions).
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

- Second Binary Image

- A black 256×256 image with a white rectangle slightly shifted down and right
(pixels 120–170 in y-direction, 120–200 in x-direction).

- DFT of First Image

- The log magnitude spectrum of the first binary image.

- Appears as a bright symmetric pattern centered due to the square shape in the
spatial domain.

- DFT of Second Image

- The log magnitude spectrum of the second binary image.

- Shows a different bright frequency pattern because of the rectangle shape.

- Convolution (Spatial Domain)

- The result of sliding the second binary image over the first and summing the
overlap at each position.

- Appears as a larger bright region due to convolution broadening the shape.

- Multiplication (Frequency Domain)

- The result of multiplying the frequency spectra of both images, then


converting back to spatial domain.

- Matches the convolution result, but small numerical differences may appear due
to rounding.

Conclusion:

● This experiment validates that:

○ Spatial Convolution = Frequency Multiplication (under Fourier Transform)

○ This principle is widely used in image processing, computer vision, and digital
filtering to optimize large kernel operations using the Fast Fourier Transform
(FFT).
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 07

Aim: Write MATLAB code to restore grayscale images from noisy images with the help
of image restoration techniques.

Objective:

● Understand and implement image restoration techniques for noise removal.

● Apply median filtering to remove impulsive (salt-and-pepper) noise.

● Apply Wiener filtering to reduce Gaussian noise.

● Combine both filters for improved noise reduction while preserving details.

Description (Theory in brief):

● Image Restoration

○ Image restoration aims to recover an original image from a degraded (noisy) image while
preserving important details.

1. Median Filtering

○ A nonlinear filtering technique.

○ Replaces each pixel’s value with the median of its surrounding neighborhood.

○ Effective against salt-and-pepper noise.

○ Preserves edges better than simple averaging.


Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

2. Wiener Filtering

○ A linear statistical filter.

○ Minimizes the mean square error between the restored and original image.

○ Adapts to local image variance and mean.

○ Works well for Gaussian noise.

3. Combined Filtering

○ Step 1: Median filter removes impulsive noise.

○ Step 2: Wiener filter reduces Gaussian noise from the median-filtered image.

○ Results in improved restoration for mixed noise conditions.

Code:

clc;
clear all;
close all;

% 1. Generate a sample binary image (simple shapes)


binaryImage = zeros(256, 256); % create black image
binaryImage(50:100, 50:100) = 1; % white square
binaryImage(150:200, 100:180) = 1; % white rectangle
binaryImage = imnoise(binaryImage, 'salt & pepper', 0.05); % add noise

% 2. Display the noisy binary image


figure;
imshow(binaryImage);
title('Noisy Binary Image');

% 3. Apply Median Filtering


medianFiltered = medfilt2(binaryImage);
figure;
imshow(medianFiltered);
title('Median Filtered Binary Image');
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

% 4. Apply Wiener Filtering


wienerFiltered = wiener2(binaryImage, [5 5]);
figure;
imshow(wienerFiltered);
title('Wiener Filtered Binary Image');

% 5. Apply Combined Filtering (Median + Wiener)


combinedFiltered = wiener2(medianFiltered, [5 5]);
figure;
imshow(combinedFiltered);
title('Combined Median + Wiener Filtered Binary Image');

% 6. Convert final image back to binary using thresholding


restoredBinary = imbinarize(combinedFiltered);

% 7. Save the restored binary image


imwrite(restoredBinary, 'restored_binary_image.png');

% 8. Display the saved restored binary image


restoredImage = imread('restored_binary_image.png');
figure;
imshow(restoredImage);
title('Restored Binary Image');

Output:

Output Observation

A black 256×256 image containing:

- A white square (pixels 50–100,


50–100)

- A white rectangle (pixels 150–200,


100–180)

Both shapes are corrupted with random white


and black salt-and-pepper noise spread
across the image.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output Observation

Noise is greatly reduced, especially the


salt-and-pepper spots.

Shapes become cleaner, but some edges may


be slightly smoothed.

Image is smoother than the noisy version,


but not as effective as the median filter at
removing isolated salt-and-pepper noise.

More effective for Gaussian-like noise, so


some pepper noise may still be visible.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Output Observation

Very clean shapes with minimal noise.

The median filter removed impulsive noise


first, and the Wiener filter smoothed any
remaining variations.

A clean black-and-white image with perfectly


restored shapes.

No visible noise, only the two original shapes


remain.

Conclusion:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

○ Median filtering is highly effective for removing salt-and-pepper noise while


maintaining edge sharpness.

○ Wiener filtering works better for Gaussian noise but may cause slight blurring of fine
details.

○ The combined approach successfully reduces both impulsive and Gaussian noise
types, producing a cleaner and sharper result.

○ This experiment demonstrates that filter selection depends on the type of noise
present in an image.

○ Such restoration techniques are widely applicable in medical imaging, satellite image
enhancement, forensic investigations, and historical photograph restoration.

PRACTICAL: 08
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Aim: Write MATLAB code to perform Min, Median & Max Filtering on grayscale
images.

Objective: To write a MATLAB program to perform Minimum, Median, and Maximum


Filtering on a grayscale image and observe the effect of each filtering technique on image
noise and details.

Description (Theory in brief):

● Image Filtering

○ Image Filtering is a technique used to enhance or modify an image by removing noise or


emphasizing certain features.

1. Minimum Filter (Min Filter)

○ Replaces each pixel value with the minimum value in its neighborhood (defined
by a filter mask).

○ Effective in removing salt noise (bright spots) but may cause image darkening.

2. Median Filter

○ Replaces each pixel value with the median of the surrounding pixel values.
○ Best for removing salt-and-pepper noise while preserving edges.

○ Non-linear filtering method.

3. Maximum Filter (Max Filter)

○ Replaces each pixel with the maximum value in its neighborhood.

○ Removes pepper noise (dark spots) and enhances bright areas.

General Filtering Process:


Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

● A mask (kernel) slides over the image.

● For each position, pixel values under the mask are processed based on the filter type.

● Result is stored in the output image.

Code:

clc;
clear;
close all;

% -------- Grayscale Image --------


i = imread('shoe.png');
grayImg = rgb2gray(i);
figure;

subplot(2,4,1), imshow(grayImg), title('Original Grayscale');

minGray = ordfilt2(grayImg, 1, ones(3,3));


medianGray = medfilt2(grayImg, [3 3]);
maxGray = ordfilt2(grayImg, 9, ones(3,3));

subplot(2,4,2), imshow(minGray), title('Min Filter - Gray');


subplot(2,4,3), imshow(medianGray), title('Median Filter - Gray');
subplot(2,4,4), imshow(maxGray), title('Max Filter - Gray');

% -------- Binary Image (0s & 1s) --------


binaryImg = [
1 0 1 0 0;
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

1 0 0 1 1;
1 1 1 1 0;
0 1 0 0 1;
10010
];

binaryImg = logical(binaryImg); % Convert to logical for display


subplot(2,4,5), imshow(binaryImg), title('Original Binary');

minBinary = ordfilt2(double(binaryImg), 1, ones(3,3));


medianBinary = medfilt2(double(binaryImg), [3 3]);
maxBinary = ordfilt2(double(binaryImg), 9, ones(3,3));

subplot(2,4,6), imshow(minBinary), title('Min Filter - Binary');


subplot(2,4,7), imshow(medianBinary), title('Median Filter - Binary');
subplot(2,4,8), imshow(maxBinary), title('Max Filter - Binary');

Output:

Output Windows You’ll See


Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

● The first row will show:

1. Original grayscale image

2. Minimum filtered grayscale image

3. Median filtered grayscale image

4. Maximum filtered grayscale image

● The second row will show:

1. Original binary image (0s & 1s)

2. Minimum filtered binary image

3. Median filtered binary image

4. Maximum filtered binary image

Conclusion:

○ Min filtering is best for removing bright noise (salt) but may erode object boundaries.

○ Median filtering offers the best balance, removing salt-and-pepper noise while
preserving shapes and edges.

○ Max filtering is best for removing dark noise (pepper) but can cause objects to grow in
size.

○ Binary images(0s & 1s) also respond to these filters, but the result is purely logical
intensity changes rather than tonal variations.

○ On grayscale images, Median filtering provides the most visually pleasing results for
mixed noise removal.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 09

Aim: Write MATLAB code to blur the image using Ideal low pass, Butterworth low pass
and Gaussian Low pass filter.

Objective: To demonstrate the effect of frequency domain filtering by applying Ideal Low
Pass Filter (ILPF), Butterworth Low Pass Filter (BLPF), and Gaussian Low Pass Filter (GLPF)
on a grayscale image, and to analyze how each filter blurs the image by reducing
high-frequency components.

Description (Theory in brief):

● Image Blurring can be achieved in the frequency domain by applying Low Pass Filters
(LPF) that allow low frequencies (smooth regions) to pass and attenuate high
frequencies (edges & details).

● Types of LPFs:

○ Ideal LPF (ILPF): Sharp cutoff; removes all frequencies beyond cutoff. Causes
ringing effect.

○ Butterworth LPF (BLPF): Smooth transition; order n controls sharpness. Less


ringing than ILPF.

○ Gaussian LPF (GLPF): Smoothest filter; no ringing, Gaussian-shaped


frequency response.

Ideal Low Pass Filter Butterworth Low Pass Filter Gaussian Low Pass Filter
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Code:

clc;
clear all;
close all;

% Load grayscale image


img = imread('BMW.png');
if size(img,3)==3
img = rgb2gray(img);
end
figure; imshow(img); title('Original Image');

% FFT of image
F = fft2(double(img));
Fshift = fftshift(F);

% Get image size


[M, N] = size(img);
u = 0:(M-1);
v = 0:(N-1);
idx = find(u > M/2); u(idx) = u(idx) - M;
idy = find(v > N/2); v(idy) = v(idy) - N;
[V,U] = meshgrid(v,u);
D = sqrt(U.^2 + V.^2); % distance matrix

% Cutoff frequency
D0 = 100;

%% 1. Ideal Low Pass Filter


H_ideal = double(D <= D0);
G_ideal = H_ideal .* Fshift;
ideal_img = real(ifft2(ifftshift(G_ideal)));
ideal_img = mat2gray(ideal_img);

%% 2. Butterworth Low Pass Filter


n = 2; % order
H_butter = 1 ./ (1 + (D./D0).^(2*n));
G_butter = H_butter .* Fshift;
butter_img = real(ifft2(ifftshift(G_butter)));
butter_img = mat2gray(butter_img);

%% 3. Gaussian Low Pass Filter


H_gaussian = exp(-(D.^2) ./ (2*(D0^2)));
G_gaussian = H_gaussian .* Fshift;
gaussian_img = real(ifft2(ifftshift(G_gaussian)));
gaussian_img = mat2gray(gaussian_img);
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

%% Display results
figure;
subplot(2,2,1); imshow(img); title('Original Grayscaled Image');
subplot(2,2,2); imshow(ideal_img, []); title('Ideal LPF');
subplot(2,2,3); imshow(butter_img, []); title('Butterworth LPF');
subplot(2,2,4); imshow(gaussian_img, []); title('Gaussian LPF');

Output:

Output Windows You’ll See


○ Original Image – the normal grayscale image (before filtering).

○ Ideal Low Pass Filter Output – image blurred but with ringing/distortion at edges (due to
sharp cutoff in frequency domain).

○ Butterworth Low Pass Filter Output – smoother blur, less distortion compared to Ideal filter.

○ Gaussian Low Pass Filter Output – most natural blur, smooth and without ringing artifacts.

Conclusion:

○ Low Pass Filters are used to remove high-frequency components (edges, noise) and
keep low-frequency parts (smooth areas).

○ Ideal LPF produces strong blur but introduces ringing artifacts due to abrupt cutoff.

○ Butterworth LPF provides controlled blurring with smoother transition, reducing


ringing.

○ Gaussian LPF gives the best visual quality with natural blur and no ringing.
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

PRACTICAL: 10

Aim: Write MATLAB code to blur the image using Ideal High pass, Butterworth High
pass and Gaussian High pass filter.

Objective: To demonstrate the effect of frequency domain filtering by applying Ideal Low
Pass Filter (ILPF), Butterworth Low Pass Filter (BLPF), and Gaussian Low Pass Filter (GLPF)
on a grayscale image, and to analyze how each filter blurs the image by reducing
high-frequency components.

Description (Theory in brief):

High-Pass Filtering in Image Processing


High-pass filters are used to emphasize edges, fine details, and sharp transitions in an image by
removing low-frequency components (smooth regions).

1. Ideal High-Pass Filter (IHPF):

○ Sharp cutoff at frequency D0.

○ Passes all frequencies higher than D0 and blocks lower ones.

○ Produces ringing (Gibbs effect).

2. Butterworth High-Pass Filter (BHPF):

○ Smooth transition between low and high frequencies.

○ Controlled by order n. Higher n → sharper cutoff.

○ Reduces ringing compared to Ideal.

3. Gaussian High-Pass Filter (GHPF):

○ Smoothest filter with no ringing.

○ Based on Gaussian function.

○ Preserves edges while minimizing artifacts.


Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

Code:

clc;
clear all;
close all;

% Read and convert to grayscale


i = imread('shoe.png');
i = rgb2gray(i);
[M, N] = size(i);

% Fourier Transform
FT_img = fft2(double(i));
FT_img = fftshift(FT_img); % Center the spectrum

% Cut-off frequency & order


D0 = 20;
n = 2;

% Frequency grid
u = 0:(M-1);
v = 0:(N-1);
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
[V, U] = meshgrid(v, u);

% Distance matrix
D = sqrt(U.^2 + V.^2);

% High-pass filters
Hil = double(D > D0); % Ideal HPF
Hbh = 1 ./ (1 + (D0 ./ D).^(2*n)); % Butterworth HPF
Hgh = 1 - exp(-(D.^2) / (2*(D0^2))); % Gaussian HPF

% Apply filters
Gih = Hil .* FT_img;
Gbh = Hbh .* FT_img;
Ggh = Hgh .* FT_img;

% Inverse transform
output_image1 = real(ifft2(ifftshift(Gih)));
output_image2 = real(ifft2(ifftshift(Gbh)));
output_image3 = real(ifft2(ifftshift(Ggh)));
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester

% Display results
figure();
subplot(2,2,1); imshow(i); title('Original Image');
subplot(2,2,2); imshow(output_image1, []); title('Ideal High Pass');
subplot(2,2,3); imshow(output_image2, []); title('Butterworth High Pass');
subplot(2,2,4); imshow(output_image3, []); title('Gaussian High Pass');

Output:

Output Windows You’ll See


○ Original Image – unprocessed grayscale image.

○ Ideal High Pass – strong edges but ringing artifacts around objects.

○ Butterworth High Pass – smoother edges, less ringing than Ideal.

○ Gaussian High Pass – cleanest result, enhances edges naturally.

Conclusion:

○ High Pass Filters remove low-frequency components (smooth background) and


preserve high-frequency components (edges & fine details).

○ Ideal HPF highlights edges but introduces distortion (ringing).

○ Butterworth HPF balances edge sharpness with fewer artifacts.

○ Gaussian HPF produces the most natural edge enhancement.

You might also like