Imageprocessing - Docx 2
Imageprocessing - Docx 2
BACHELOR OF TECHNOLOGY
SEMESTER VII
Department of
Artificial Intelligence & Data Science
CERTIFICATE
Head of Department………….……
TABLE OF CONTENTS
Date of
Experiment Page No Performance
Date of
SR.NO Title Submission Marks Sign
From To
a) Image negative
b) Logarithmic transformation
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:
Description:
Resizes the image by a scale factor.
Example:
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:
Output:
16. edge()
Syntax:
Description:
Detects edges in the image using a specific method ('sobel', 'canny', etc.).
Example:
Output:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester
17. imfilter()
Syntax:
Description:
Applies a filter to an image.
Example:
h = fspecial('average', [5 5]);
smoothImg = imfilter(gray_img, h);
imshow(smoothImg);
Output:
18. imnoise()
Syntax:
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
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:
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
clear all clear all Removes all variables from clear all
workspace
close all close all Closes all figure windows close all
Conclusion:
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.
● 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;
[r, c] = size(gray);
[rb, cb] = size(binary_img);
% 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
% 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');
% 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:
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) :
Conclusion:
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
● Image Negative: Enhances white or bright regions by inverting the pixel intensities.
○ Formula: s = L - 1 - r
Code:
clc;
clear all;
close all;
% 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');
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:
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.
Image Histogram:
● 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:
Where:
Code:
clc;
clear all;
close all;
%
% 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:
- 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.
● 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:
● This technique is useful for data compression, watermarking, and visual analysis.
Code:
clc;
clear all;
close all;
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
○ Higher bits show clearer parts of the image, lower bits show noise or fine
textures
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:
PRACTICAL: 06
Aim: Write MATLAB code to demonstrate that the convolution in the spatial domain is
equivalent to multiplication in the frequency domain.
○ Where:
■ * is convolution
■ × is element-wise multiplication
■ F(u,v) and H(u,v) are the Fourier transforms of the image and
filter respectively.
Code:
clc;
clear;
close all;
%% 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
- 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
- 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).
- Appears as a bright symmetric pattern centered due to the square shape in the
spatial domain.
- The result of sliding the second binary image over the first and summing the
overlap at each position.
- Matches the convolution result, but small numerical differences may appear due
to rounding.
Conclusion:
○ 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:
● Combine both filters for improved noise reduction while preserving details.
● Image Restoration
○ Image restoration aims to recover an original image from a degraded (noisy) image while
preserving important details.
1. Median Filtering
○ Replaces each pixel’s value with the median of its surrounding neighborhood.
2. Wiener Filtering
○ Minimizes the mean square error between the restored and original image.
3. Combined Filtering
○ Step 2: Wiener filter reduces Gaussian noise from the median-filtered image.
Code:
clc;
clear all;
close all;
Output:
Output Observation
Output Observation
Output Observation
Conclusion:
Faculty of Engineering & Technology
Subject: Image Processing Laboratory
Subject Code: 303105382
B.Tech - AI&AIDS 4th Year 7th Semester
○ 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.
● Image Filtering
○ 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.
● For each position, pixel values under the mask are processed based on the filter type.
Code:
clc;
clear;
close all;
1 0 0 1 1;
1 1 1 1 0;
0 1 0 0 1;
10010
];
Output:
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.
● 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.
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;
% FFT of image
F = fft2(double(img));
Fshift = fftshift(F);
% Cutoff frequency
D0 = 100;
%% 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:
○ 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.
○ 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.
Code:
clc;
clear all;
close all;
% Fourier Transform
FT_img = fft2(double(i));
FT_img = fftshift(FT_img); % Center the spectrum
% 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:
○ Ideal High Pass – strong edges but ringing artifacts around objects.
Conclusion: