Machine_Vision_Lab_Manual
Machine_Vision_Lab_Manual
LABORATORY MANUAL
Machine Vision
Subject Code: BE05041051
Page 1 of 31
Machine Vision (BE05041051) — Laboratory Manual
INDEX
Sr. No. Title of Practical CO Mapped Page No. Date Sign
1 Introduction to MATLAB/SCILAB: Basic Commands, Script CO-1
Files and Image Reading/Writing
2 Study of Different Digital Image Classes, Types and File CO-1
Formats
3 Arithmetic and Logical Operations on Images CO-1
Page 2 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency & Ability to set up the image processing environment and perform basic image I/O
Practical Skills operations.
CO-1: Explain the fundamentals of digital image formation, human visual perception,
Relevant CO
image sensing, sampling, quantization and pixel relationships.
Objectives
• To familiarize students with the MATLAB/SCILAB Image Processing environment.
• To learn basic commands, variables, and script file creation.
• To read, display and write digital images using built-in functions.
Theory
MATLAB (Matrix Laboratory) and SCILAB are numerical computing environments widely used for digital
image processing because images can be represented as 2-D (grayscale) or 3-D (RGB) matrices of pixel intensity
values.
A digital image f(x,y) is a discretized, quantized representation of a continuous scene captured through an
imaging sensor. Each element of the matrix, called a pixel, holds an intensity value (0-255 for an 8-bit image).
Basic functions used: imread() to load an image into a matrix, imshow()/imagesc() to display it, imwrite() to save
it, size() to get dimensions, and class() to check the data type of the image.
% Read an image
I = imread('[Link]');
Page 3 of 31
Machine Vision (BE05041051) — Laboratory Manual
Result
The image was successfully read, its size and class were displayed in the command window, and the image was
rendered in a figure window. A copy of the image was saved as output_image.png in the working directory.
Conclusion
Students should write the observed dimensions/class of the chosen image and summarize their understanding of
the basic MATLAB/SCILAB image I/O workflow.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 4 of 31
Machine Vision (BE05041051) — Laboratory Manual
Page 5 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to identify and convert between different image classes and file formats.
Practical Skills
CO-1: Explain the fundamentals of digital image formation, human visual perception,
Relevant CO
image sensing, sampling, quantization and pixel relationships.
Objectives
• To understand binary, indexed, grayscale and RGB image classes.
• To perform conversions between different image types.
• To study common image file formats and their properties.
Theory
Images can broadly be classified into four classes: Binary (1-bit, values 0 or 1), Indexed (uses a colormap/lookup
table), Grayscale/Intensity (8-bit or 16-bit, values 0-255 or 0-65535), and RGB/True-color (three 8-bit planes for
Red, Green, Blue).
Common file formats include BMP, TIFF, PNG, JPEG and GIF, which differ in compression scheme
(lossy/lossless), bit depth support and metadata handling.
MATLAB provides conversion functions such as rgb2gray(), im2bw()/imbinarize(), gray2ind(), and ind2rgb() to
move between these classes.
I_rgb = imread('[Link]');
figure, imshow(I_rgb), title('RGB Image');
% Convert to grayscale
I_gray = rgb2gray(I_rgb);
figure, imshow(I_gray), title('Grayscale Image');
% Convert to binary
I_bin = imbinarize(I_gray);
figure, imshow(I_bin), title('Binary Image');
Page 6 of 31
Machine Vision (BE05041051) — Laboratory Manual
Result
RGB, grayscale, binary and indexed versions of the same image were generated and displayed. Class types (uint8,
logical) were verified in the command window, and the image was saved in PNG, JPEG and BMP formats for
comparison.
Conclusion
Students should note the file-size differences among PNG, JPEG and BMP for the same image and relate this to
lossless vs lossy compression.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 7 of 31
Machine Vision (BE05041051) — Laboratory Manual
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 8 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to apply pixel-level arithmetic and logical operations for image manipulation.
Practical Skills
CO-1: Explain the fundamentals of digital image formation, human visual perception,
Relevant CO
image sensing, sampling, quantization and pixel relationships.
Objectives
• To perform arithmetic operations (addition, subtraction, multiplication) on images.
• To perform logical operations (AND, OR, XOR, NOT) on images.
• To observe the effect of these operations on image appearance.
Theory
Arithmetic operations are performed pixel-by-pixel between two images of the same size, or between an image
and a scalar. Image addition is used for noise averaging, subtraction for change/motion detection, and
multiplication/division for masking and shading correction.
Logical operations (AND, OR, XOR, NOT) operate on the binary representation of pixel values and are widely
used for masking, region extraction and combining binary images.
I1 = imread('[Link]');
I2 = imread('[Link]');
I2 = imresize(I2, size(I1));
% Arithmetic operations
I_add = imadd(I1, I2);
I_sub = imsubtract(I1, I2);
I_mul = immultiply(I1, 1.5);
I_div = imdivide(I1, 2);
figure;
subplot(2,2,1), imshow(I_add), title('Addition');
subplot(2,2,2), imshow(I_sub), title('Subtraction');
subplot(2,2,3), imshow(I_mul), title('Multiplication');
subplot(2,2,4), imshow(I_div), title('Division');
figure;
subplot(2,2,1), imshow(I_and), title('AND');
subplot(2,2,2), imshow(I_or), title('OR');
Page 9 of 31
Machine Vision (BE05041051) — Laboratory Manual
Result
Arithmetic operations produced brightened/darkened and blended image outputs, while logical operations
produced masked/combined binary images highlighting the overlapping and differing regions between the two
source images.
Conclusion
Students should describe which operation is most suitable for change detection and which for masking, based on
observed results.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 10 of 31
Machine Vision (BE05041051) — Laboratory Manual
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 11 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to perform spatial resolution modification and bit-level decomposition of images.
Practical Skills
CO-1: Explain the fundamentals of digital image formation, human visual perception,
Relevant CO
image sensing, sampling, quantization and pixel relationships.
Objectives
• To zoom in and shrink a digital image using interpolation techniques.
• To decompose a grayscale image into its constituent bit planes.
• To analyze the contribution of each bit plane to overall image quality.
Theory
Zooming (up-sampling) increases image size using interpolation methods such as nearest-neighbour, bilinear or
bicubic interpolation, whereas shrinking (down-sampling) reduces spatial resolution.
Bit-plane slicing decomposes an 8-bit grayscale image into 8 binary images, each representing one bit position.
Higher-order bit planes (e.g., the MSB) contain the majority of visually significant information.
I = imread('[Link]');
% Zooming (up-sampling)
I_zoom = imresize(I, 2, 'bicubic');
figure, imshow(I_zoom), title('Zoomed Image (2x)');
% Shrinking (down-sampling)
I_shrink = imresize(I, 0.5, 'nearest');
figure, imshow(I_shrink), title('Shrunk Image (0.5x)');
% Bit-plane slicing
I = double(I);
figure;
for bit = 0:7
plane = mod(floor(I / (2^bit)), 2);
subplot(2,4,bit+1);
imshow(plane);
title(['Bit Plane ', num2str(bit)]);
end
Result
Zoomed and shrunk versions of the image were generated using different interpolation techniques, and all eight
bit planes were extracted and displayed, showing that the MSB planes carry most of the structural information.
Conclusion
Students should comment on the visual quality loss/gain with each interpolation method and identify which bit
planes are most significant.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 13 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to apply basic gray-level transformation functions for image enhancement.
Practical Skills
CO-2: Apply spatial and frequency domain enhancement techniques to improve image
Relevant CO
quality.
Objectives
• To implement image negative, log and power-law (gamma) transformations.
• To perform contrast stretching on a low-contrast image.
• To compare the visual effect of each transformation.
Theory
Gray-level transformations map each pixel intensity r to a new value s = T(r). The image negative is given by s =
L - 1 - r; log transformation by s = c*log(1+r), which expands dark pixel values and compresses bright ones; and
power-law (gamma) transformation by s = c*r^gamma, used for gamma correction in displays and cameras.
Contrast stretching is a piecewise-linear transformation that expands the range of intensity values in a low-
contrast image to span the full dynamic range, thereby improving visual contrast.
I = imread('[Link]');
I = im2double(I);
% Negative transformation
I_neg = 1 - I;
% Log transformation
c = 1;
I_log = c * log(1 + I);
% Contrast stretching
r_min = min(I(:)); r_max = max(I(:));
I_stretch = (I - r_min) / (r_max - r_min);
figure;
subplot(2,3,1), imshow(I), title('Original');
subplot(2,3,2), imshow(I_neg), title('Negative');
subplot(2,3,3), imshow(I_log), title('Log Transform');
subplot(2,3,4), imshow(I_gamma), title('Power-Law (gamma=0.5)');
subplot(2,3,5), imshow(I_stretch), title('Contrast Stretched');
Page 14 of 31
Machine Vision (BE05041051) — Laboratory Manual
Result
All four transformations were applied to the input image. The negative transformation inverted the intensity, the
log transform enhanced dark regions, the power-law transform adjusted overall brightness non-linearly, and
contrast stretching visibly improved the dynamic range of the low-contrast input.
Conclusion
Students should relate the choice of gamma value to the type of correction (brightening vs darkening) required
and note which transformation best enhanced the given test image.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 15 of 31
Machine Vision (BE05041051) — Laboratory Manual
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 16 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to analyze and modify the intensity distribution of an image for enhancement.
Practical Skills
CO-2: Apply spatial and frequency domain enhancement techniques to improve image
Relevant CO
quality.
Objectives
• To compute and plot the histogram of a grayscale image.
• To perform histogram equalization to improve global contrast.
• To perform histogram matching against a specified reference histogram.
Theory
A histogram represents the frequency of occurrence of each intensity level in an image and provides a statistical
description of its tonal distribution.
Histogram equalization redistributes intensity values so that the resulting histogram is approximately uniform,
thereby improving overall image contrast. Histogram matching (specification) transforms the histogram of an
image to match a desired/reference histogram, useful when a specific tonal appearance is required.
I = imread('[Link]');
% Plot histogram
figure, imhist(I), title('Original Histogram');
% Histogram Equalization
I_eq = histeq(I);
figure, imshow(I_eq), title('Histogram Equalized Image');
figure, imhist(I_eq), title('Equalized Histogram');
% Histogram Matching
ref = imread('[Link]');
I_match = imhistmatch(I, ref);
figure, imshow(I_match), title('Histogram Matched Image');
figure, imhist(I_match), title('Matched Histogram');
Result
The original histogram showed a narrow intensity distribution (low contrast). After equalization, the histogram
spread across the full range, producing a visibly higher-contrast image. Histogram matching reshaped the image
histogram to resemble that of the reference image.
Page 17 of 31
Machine Vision (BE05041051) — Laboratory Manual
Conclusion
Students should compare the equalized and matched histograms with the original and discuss the visual
improvement/change achieved in each case.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 18 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to design and apply spatial filter masks for image smoothing and sharpening.
Practical Skills
CO-2: Apply spatial and frequency domain enhancement techniques to improve image
Relevant CO
quality.
Objectives
• To implement smoothing (low-pass) spatial filters using convolution masks.
• To implement sharpening (high-pass) spatial filters using the Laplacian mask.
• To compare filtered outputs with the original image.
Theory
Spatial filtering involves moving a mask (kernel) over the image and computing a weighted sum
(convolution/correlation) of pixel values under the mask. Smoothing filters (e.g., mean/averaging mask) reduce
noise and blur edges, while sharpening filters (e.g., Laplacian mask) enhance edges and fine detail by
emphasizing intensity discontinuities.
The Laplacian is a second-order derivative operator; adding the Laplacian response back to the original image
produces edge-enhanced (sharpened) output.
I = imread('[Link]');
I = im2double(I);
figure;
subplot(1,3,1), imshow(I), title('Original');
subplot(1,3,2), imshow(I_smooth), title('Smoothed (Mean Filter)');
subplot(1,3,3), imshow(I_sharp), title('Sharpened (Laplacian)');
Result
The mean filter mask produced a visibly blurred/smoothed image with reduced noise, while the Laplacian-based
sharpening mask enhanced edges and fine details, making structural boundaries more prominent.
Conclusion
Students should note the trade-off between noise reduction and edge preservation observed while applying
smoothing versus sharpening masks.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 20 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to transform and analyze images in the frequency domain.
Practical Skills
CO-2: Apply spatial and frequency domain enhancement techniques to improve image
Relevant CO
quality.
Objectives
• To compute the 1-D DFT of a signal/image row.
• To compute and visualize the 2-D DFT (frequency spectrum) of an image.
• To understand the significance of the frequency domain representation.
Theory
The Discrete Fourier Transform (DFT) converts a spatial-domain signal/image into its frequency-domain
representation, revealing the frequency components that make up the image. For images, the 2-D DFT is
computed using fft2(), and the zero-frequency (DC) component is shifted to the center of the spectrum using
fftshift() for visualization.
Low frequencies correspond to smooth/slowly varying regions of the image, whereas high frequencies correspond
to edges, noise and fine detail.
figure;
subplot(1,2,1), imshow(I), title('Original Image');
subplot(1,2,2), imshow(magnitude, []), title('2-D DFT Magnitude Spectrum');
Result
The 1-D DFT plot displayed the frequency content of a single image row, while the 2-D DFT magnitude spectrum
(log-scaled and centered) revealed the concentration of low-frequency energy near the center and high-frequency
components corresponding to edges spread outward.
Page 21 of 31
Machine Vision (BE05041051) — Laboratory Manual
Conclusion
Students should relate bright regions in the centered spectrum to smooth areas of the image and outer regions to
edges/fine detail, and note why fftshift() and log-scaling are needed for visualization.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 22 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency &
Ability to design and apply frequency-domain filters for image smoothing and sharpening.
Practical Skills
CO-2: Apply spatial and frequency domain enhancement techniques to improve image
Relevant CO
quality.
Objectives
• To design Ideal, Butterworth and Gaussian low-pass filters in the frequency domain.
• To design the corresponding high-pass filters.
• To observe smoothing and sharpening effects of frequency domain filtering.
Theory
Frequency domain filtering involves multiplying the DFT of an image by a filter transfer function H(u,v) and
taking the inverse DFT of the result. Ideal filters have a sharp cutoff and cause ringing artifacts; Butterworth
filters provide a smooth transition controlled by filter order; Gaussian filters provide the smoothest transition with
no ringing.
Low-pass filters attenuate high frequencies (smoothing/blurring), while high-pass filters attenuate low frequencies
(edge enhancement/sharpening).
I = im2double(imread('[Link]'));
[M, N] = size(I);
F = fftshift(fft2(I));
% Apply filters
I_ideal = real(ifft2(ifftshift(F .* H_ideal)));
I_butter = real(ifft2(ifftshift(F .* H_butter)));
I_gauss = real(ifft2(ifftshift(F .* H_gauss)));
Page 23 of 31
Machine Vision (BE05041051) — Laboratory Manual
figure;
subplot(2,3,1), imshow(I), title('Original');
subplot(2,3,2), imshow(I_ideal, []), title('Ideal LPF');
subplot(2,3,3), imshow(I_butter, []), title('Butterworth LPF');
subplot(2,3,4), imshow(I_gauss, []), title('Gaussian LPF');
subplot(2,3,5), imshow(I_ideal_hp, []), title('Ideal HPF');
subplot(2,3,6), imshow(I_gauss_hp, []), title('Gaussian HPF');
Result
Low-pass filtered images appeared progressively smoother from Ideal to Gaussian, with the Ideal filter showing
visible ringing artifacts near edges. High-pass filtered images highlighted edges and suppressed
smooth/background regions.
Conclusion
Students should compare ringing artifacts across the three low-pass filters and justify why the Gaussian filter is
generally preferred in practical applications.
Page 24 of 31
Machine Vision (BE05041051) — Laboratory Manual
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 25 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency & Ability to simulate image degradation and apply restoration techniques to recover image
Practical Skills quality.
CO-3: Analyze image degradation models and apply appropriate restoration filters to
Relevant CO
recover degraded images.
Objectives
• To add different types of noise (Gaussian, salt-and-pepper) to an image.
• To restore degraded images using arithmetic mean and adaptive filters.
• To compare restoration performance using PSNR/visual inspection.
Theory
Image degradation is commonly modeled as g(x,y) = h(x,y) * f(x,y) + n(x,y), where f is the original image, h is
the degradation function and n is additive noise. Noise models include Gaussian noise (arising from
sensor/electronic noise) and salt-and-pepper (impulse) noise.
Restoration filters attempt to recover the original image: the arithmetic mean filter averages pixel neighborhoods
(effective against Gaussian noise), the geometric mean filter smooths while preserving detail better, and adaptive
filters adjust filtering strength based on local statistics (variance), performing better than fixed filters in regions
with varying noise levels.
I = im2double(imread('[Link]'));
figure;
subplot(2,3,1), imshow(I), title('Original');
subplot(2,3,2), imshow(I_gauss_noise), title('Gaussian Noise');
subplot(2,3,3), imshow(I_restored_mean), title('Mean Filter Restored');
subplot(2,3,4), imshow(I), title('Original');
subplot(2,3,5), imshow(I_sp_noise), title('Salt & Pepper Noise');
subplot(2,3,6), imshow(I_restored_adaptive), title('Adaptive Filter Restored');
Page 26 of 31
Machine Vision (BE05041051) — Laboratory Manual
Result
Gaussian and salt-and-pepper noise were successfully added to the test image. The arithmetic mean filter reduced
Gaussian noise with some loss of sharpness, while the adaptive (Wiener) filter effectively suppressed impulse
noise while better preserving edge details.
Conclusion
Students should discuss which filter performed better for each noise type and why adaptive filtering outperforms
fixed averaging in noise-variable regions.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 27 of 31
Machine Vision (BE05041051) — Laboratory Manual
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 28 of 31
Machine Vision (BE05041051) — Laboratory Manual
Competency & Ability to apply morphological operations for shape analysis on binary and grayscale
Practical Skills images.
CO-5: Implement morphological image processing operations for binary and grayscale
Relevant CO
image analysis in machine vision applications.
Objectives
• To implement dilation and erosion on binary and grayscale images.
• To implement opening and closing operations and study their applications.
• To observe the effect of structuring element shape and size on the result.
Theory
Morphological image processing analyzes and manipulates the geometric structure of objects within an image
using a structuring element (SE). Dilation expands/grows the boundaries of foreground objects, while erosion
shrinks them.
Opening (erosion followed by dilation) removes small objects/noise while preserving the shape and size of larger
objects. Closing (dilation followed by erosion) fills small holes and gaps. The Hit-or-Miss transform is used for
shape/pattern detection within binary images.
I = imread('[Link]');
I = imbinarize(I);
se = strel('disk', 3);
figure;
subplot(2,3,1), imshow(I), title('Original Binary Image');
subplot(2,3,2), imshow(I_dilate), title('Dilation');
subplot(2,3,3), imshow(I_erode), title('Erosion');
subplot(2,3,4), imshow(I_open), title('Opening');
subplot(2,3,5), imshow(I_close), title('Closing');
% Hit-or-Miss Transform
se_hit = strel('disk', 2);
I_hitmiss = bwhitmiss(I, se_hit);
subplot(2,3,6), imshow(I_hitmiss), title('Hit-or-Miss Result');
Page 29 of 31
Machine Vision (BE05041051) — Laboratory Manual
Result
Dilation enlarged the foreground regions while erosion shrank them. Opening removed small isolated noise
particles, and closing filled small gaps/holes within the objects, confirming standard morphological behavior on
the test image.
Conclusion
Students should relate the size/shape of the structuring element used to the degree of change observed, and
identify one practical application each for dilation, erosion, opening and closing.
Evaluation Rubrics
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 30 of 31
Machine Vision (BE05041051) — Laboratory Manual
Below Average
Evaluation Criteria Excellent (5) Good (4) Average (3)
(1-2)
Page 31 of 31