Digital Image Processing Lab Manual Part-1
Digital Image Processing Lab Manual Part-1
P age |2
Digital Image Processing Labo ratory
Table of Contents
List of The Experiments ..................................................................................................................... 2
INTRODUCTION ................................................................................................................................. 6
A. Background............................................................................................................................ 6
B. Required Knowledge .............................................................................................................. 6
C. Some Basic Concepts Need to Know Before Start ................................................................... 6
D. Environment Setup ................................................................................................................ 7
E. First Program to Test Environment......................................................................................... 8
EXPERIMENT NO. 01 .......................................................................................................................... 9
1.1. Some Key Terms Related to This Experiment ...................................................................... 9
1.2. Python Source Code ........................................................................................................... 9
1.3. Solution Procedure............................................................................................................. 9
1.4. Output Figure................................................................................................................... 10
EXPERIMENT NO. 02 ........................................................................................................................ 11
2.1. Some Key Terms Related to This Experiment .................................................................... 11
2.2. Python Source Code ......................................................................................................... 11
2.3. Solution Procedure........................................................................................................... 11
2.4. Output Figure................................................................................................................... 12
EXPERIMENT NO. 03 ........................................................................................................................ 13
3.1. Some Key Terms Related to This Experiment .................................................................... 13
3.2. Python Source Code ......................................................................................................... 13
3.3. Solution Procedure........................................................................................................... 13
3.4. Output Figure................................................................................................................... 14
EXPERIMENT NO. 04 ........................................................................................................................ 15
4.1. Some Key Terms Related to This Experiment .................................................................... 15
4.2. Python Source Code ......................................................................................................... 15
4.3. Solution Procedure........................................................................................................... 15
4.4. Output Figure................................................................................................................... 16
EXPERIMENT NO. 05 ........................................................................................................................ 17
5.1. Some Key Terms Related to This Experiment .................................................................... 17
5.2. Python Source Code ......................................................................................................... 17
5.3. Solution Procedure........................................................................................................... 17
5.4. Output Figure................................................................................................................... 18
EXPERIMENT NO. 06 ........................................................................................................................ 19
6.1. Some Key Terms Related to This Experiment .................................................................... 19
P age |3
Digital Image Processing Labo ratory
P age |4
Digital Image Processing Labo ratory
P age |5
Digital Image Processing Labo ratory
INTRODUCTION
A. Background
The study of digital image processing entails the analysis and alteration of digital images
through the use of computer algorithms. When computers first became strong enough to
process photos, in the 1960s, the history of digital image processing began. Digital image
processing first found use in the sciences of astronomy and medicine, where improved or
processed images were required for research. Digital image processing was increasingly used
in a range of industries, including photography, cinema, television, and video production, as
computers grew more powerful and accessible. With applications in industries like medicine,
remote sensing, and computer vision, digital image processing is now a significant academic
topic that is constantly developing and getting better.
B. Required Knowledge
We must have a fundamental understanding of picture attributes like resolution, color depth,
and image format in order to begin a basic Digital picture Processing Lab using OpenCV in
Python. Additionally, we should be familiar with the fundamentals of Python programming,
such as data types, control structures, and grammar.
Pip: Pip is the default package installer for Python. It is a command-line tool that allows
developers to easily install and manage third-party Python packages from the Python Package
Index (PyPI) and other package repositories.
OpenCV: OpenCV (Open Source Computer Vision) is an open-source library for computer
vision and image processing tasks, written in C++ and widely used in various industries and
research applications. OpenCV-Python is a Python wrapper for the OpenCV library, which
allows developers to use OpenCV in Python programs.
Matplotlib: Matplotlib is a plotting library for the Python programming language. It provides
an easy-to-use interface for creating high-quality static and interactive visualizations in
Python. Matplotlib was originally created to replicate the plotting capabilities of MATLAB, but
has since become a powerful tool for data visualization in its own right.
Numpy: NumPy (Numerical Python) is a Python library for scientific computing, particularly for
numerical operations on large arrays and matrices. It provides an array data structure that is
P age |6
Digital Image Processing Labo ratory
both more efficient and more convenient than Python's built-in data structures for numerical
operations.
VS Code: Visual Studio Code (VS Code) is a free and open-source code editor developed by
Microsoft for Windows, macOS, and Linux operating systems. It provides a modern and
powerful development environment that supports a wide range of programming languages
and frameworks.
RGB Image: The most popular color format for digital photos is RGB, which can produce a wide
range of colors by combining three color channels. Red, Green, and Blue are the three color
channels that make up RGB pictures. Each RGB image pixel contains a value for each of these
color channels, which establishes the pixel's overall color
Grayscale Image: Only shades of gray are present in grayscale photographs. In a grayscale
image, each pixel's brightness is determined by a single value. For simpler graphics that don't
need color, such black-and-white photos or straightforward line drawings, grayscale images
are frequently utilized. They can also be used to represent an RGB image more simply when
the color information is not required.
RGB Image Grayscale Image
D. Environment Setup
To set up a Digital Image Processing Lab using VS Code as the code editor and OpenCV,
Matplotlib, and Numpy libraries, should follow these steps for each operating system:
Windows:
1. Install Python: Download and install the latest version of Python from the official
website [1]. Make sure to add Python to the system PATH during installation.
2. Install pip: Open a command prompt and run the command python -m
ensurepip --default-pip.
3. Install OpenCV, Matplotlib, and Numpy: Open a command prompt and run the
following commands: pip install opencv-python, pip install
matplotlib, and pip install numpy.
4. Install VS Code: Download and install VS Code from the official website [4].
5. Configure VS Code: Open VS Code and install the Python extension. Set up your Python
interpreter by selecting it in the VS Code settings.
P age |7
Digital Image Processing Labo ratory
Linux (Ubuntu):
1. Install Python: Open a terminal and run the command sudo apt-get install python3
[2].
2. Install pip: Open a terminal and run the command sudo apt-get install python3-pip.
3. Install OpenCV, Matplotlib, and Numpy: Open a terminal and run the following
commands: pip install opencv-python, pip install matplotlib,
and pip install numpy.
4. Install VS Code: Download and install VS Code from the official website [5] or use the
following command: sudo snap install --classic code.
5. Configure VS Code: Open VS Code and install the Python extension. Set up your Python
interpreter by selecting it in the VS Code settings.
MacOS:
1. Install Python: Download and install the latest version of Python from the official
website [3]. Make sure to add Python to the system PATH during installation.
2. Install pip: Open a terminal and run the command sudo easy_install pip.
3. Install OpenCV, Matplotlib, and Numpy: Open a terminal and run the following
commands: pip install opencv-python, pip install matplotlib,
and pip install numpy.
4. Install VS Code: Download and install VS Code from the official website [6].
5. Configure VS Code: Open VS Code and install the Python extension. Set up your Python
interpreter by selecting it in the VS Code settings.
After completing these steps, we should be able to start using VS Code for Digital Image
Processing Lab. We can create Python files in VS Code and use the libraries to process images.
Make sure to import the necessary libraries at the beginning of our Python file.
P age |8
Digital Image Processing Labo ratory
1. EXPERIMENT NO. 01
P age |9
Digital Image Processing Labo ratory
Finally, the gradient image is saved to disk using the cv2.imwrite() function and displayed
using the cv2.imshow() function. The input image and the gradient image are displayed
side by side for comparison.
P a g e | 10
Digital Image Processing Labo ratory
2. EXPERIMENT NO. 02
P a g e | 11
Digital Image Processing Labo ratory
Once the intensity_counts list has been computed, the function uses the plt.bar()
function from the Matplotlib library to create a bar chart of the histogram. The x-axis is the
intensity level and the y-axis is the number of pixels with that intensity level.
Finally, the original input image, the grayscale image, and the histogram are displayed using
the cv2.imshow() and plt.show() functions.
P a g e | 12
Digital Image Processing Labo ratory
3. EXPERIMENT NO. 03
P a g e | 13
Digital Image Processing Labo ratory
Once the negative image has been computed, the function uses the cv2.imwrite()
function to save the image as a file named "negative.jpg".
Finally, the original input image and the negative image are displayed using the
cv2.imshow() function.
P a g e | 14
Digital Image Processing Labo ratory
4. EXPERIMENT NO. 04
P a g e | 15
Digital Image Processing Labo ratory
image is converted back to the BGR color space and saved as a new image file called
"enhanced_color.jpg". The code displays the original image and the color-enhanced
image using cv2.imshow().
P a g e | 16
Digital Image Processing Labo ratory
5. EXPERIMENT NO. 05
P a g e | 17
Digital Image Processing Labo ratory
the channels back together, converts the image back to BGR color space, and saves the
enhanced image.
The main program loads the image, displays the original image, applies the
enhance_brightness() function to the image, displays the enhanced image, and waits
for a key press before closing the windows.
P a g e | 18
Digital Image Processing Labo ratory
6. EXPERIMENT NO. 06
P a g e | 19
Digital Image Processing Labo ratory
Note that this approach is not the most efficient way to enhance contrast, since it involves a
lot of unnecessary looping and calculation. There are other methods, such as histogram
equalization or adaptive contrast stretching, that can achieve similar results with much less
computation.
P a g e | 20
Digital Image Processing Labo ratory
7. EXPERIMENT NO. 07
P a g e | 21
Digital Image Processing Labo ratory
P a g e | 22
Digital Image Processing Labo ratory
8. EXPERIMENT NO. 08
There are various contour detection algorithms used in digital image processing, including:
• Canny edge detection: This algorithm detects the edges in the image by analyzing the
intensity gradient of each pixel. The edges are then linked to form closed contours.
• Sobel edge detection: This algorithm applies a Sobel filter to the image to detect the
intensity gradient in both horizontal and vertical directions. The edges are then
detected based on the magnitude of the gradient.
In this experiment we will use Canny edge detection algorithm to detect contour of an image.
P a g e | 23
Digital Image Processing Labo ratory
P a g e | 24
Digital Image Processing Labo ratory
9. EXPERIMENT NO. 09
P a g e | 25
Digital Image Processing Labo ratory
should be run with different initial centroids. The criteria parameter defines the termination
criteria for the algorithm, which is set to stop when either the maximum number of iterations
(10) is reached or the minimum change in cluster centroids is less than 1.0.
The k-means algorithm is then run using the cv2.kmeans() function, which takes as input
the pixel array, the number of clusters (k), the initial centroids (None in this case), the
termination criteria (criteria), the number of times to run the algorithm (attempts), and the
method for selecting the initial centroids (cv2.KMEANS_PP_CENTERS).
The cv2.kmeans() function returns the final cluster centroids in the center variable, as well
as a label array that specifies which cluster each pixel belongs to. The center variable is
converted to an unsigned 8-bit integer array, and the label array is flattened and used to create
a new array ‘res’ of the same shape as the original pixel array, where each pixel is assigned
the RGB values of the cluster centroid it belongs to. Finally, the res array is reshaped into the
shape of the original image, and the resulting segmented image is saved to disk using the
cv2.imwrite() function.
P a g e | 26
Digital Image Processing Labo ratory
There are various image sharpening techniques used in digital image processing, including:
• Unsharp masking: This technique involves creating a blurred version of the original
image and subtracting it from the original image to enhance the edges. This is achieved
by convolving the image with a Gaussian filter and subtracting the resulting image from
the original image.
• High-pass filtering: This technique involves filtering the image with a high-pass filter,
which emphasizes the high-frequency components of the image (i.e., the edges) and
suppresses the low-frequency components (i.e., the smooth areas).
• Laplacian filtering: This technique involves convolving the image with a Laplacian filter,
which highlights the areas of the image where the intensity changes rapidly (i.e., the
edges).
• Contrast enhancement: This technique involves increasing the contrast of the image to
make the edges more visible. This can be achieved by applying a histogram equalization
or contrast stretching technique.
P a g e | 27
Digital Image Processing Labo ratory
18 cv2.waitKey(0)
P a g e | 28
Digital Image Processing Labo ratory
The process of skeletonization involves iteratively thinning the object until only its skeleton
remains. This is done by repeatedly eroding the object with a structuring element and
subtracting the result from the original object. The structuring element is a small binary matrix
that is used to scan the image and identify pixels that can be removed without changing the
topology of the object.
P a g e | 29
Digital Image Processing Labo ratory
P a g e | 30
Digital Image Processing Labo ratory
The 2D filter kernel is a small matrix that contains a set of coefficients that determine how
the neighboring pixels contribute to the new pixel value. The most commonly used 2D filter
kernels for image smoothing are Gaussian, median, and mean filters.
P a g e | 31
Digital Image Processing Labo ratory
P a g e | 32
Digital Image Processing Labo ratory
An average filter is a type of linear filter, where each output pixel value is calculated as the
average of the corresponding neighboring pixels within a defined kernel or window size. The
kernel size can vary depending on the level of smoothing desired.
The function takes an image as input, defines a 5x5 averaging kernel, applies the kernel
to the input image using the "cv2.blur()" function, and saves the output image as a file
named "smoothed_average_filter.jpg".
The code then displays the original image and the smoothed image using the average filter.
Finally, it waits for a key event to exit the program.
P a g e | 33
Digital Image Processing Labo ratory
P a g e | 34
Digital Image Processing Labo ratory
The Gaussian filter is a type of linear filter that is widely used for image smoothing because it
can effectively suppress high-frequency noise while preserving the edges and fine details in
the image. The amount of smoothing can be controlled by adjusting the standard deviation of
the Gaussian distribution, which determines the size of the filter kernel.
P a g e | 35
Digital Image Processing Labo ratory
The cv2.GaussianBlur() function takes three arguments: the input image, the size of
the kernel (specified as a tuple), and the standard deviation of the Gaussian distribution used
to generate the kernel. In this case, the kernel size is set to (5, 5) and the standard deviation is
set to 0, which means that it is automatically calculated based on the kernel size.
Finally, the original and smoothed images are displayed using cv2.imshow() and
cv2.waitKey().
P a g e | 36
Digital Image Processing Labo ratory
The Median filter is a non-linear filter that is widely used for image smoothing because it can
effectively suppress impulse noise, which is a type of noise that can randomly occur in the
image due to external factors. The amount of smoothing can be controlled by adjusting the
size of the filter kernel.
P a g e | 37
Digital Image Processing Labo ratory
(5,5) using the kernel variable. The resulting smoothed image is saved using the
cv2.imwrite() function.
Finally, the original image and the smoothed image are displayed using the cv2.imshow()
function and the cv2.waitKey() function is used to wait for a key event.
P a g e | 38
Digital Image Processing Labo ratory
The Bilateral filter is a non-linear filter that is widely used for image smoothing because it can
effectively suppress noise while preserving the edges and fine details in the image. The amount
of smoothing can be controlled by adjusting the standard deviation of the spatial Gaussian
distribution and the range Gaussian distribution, which determine the size of the filter kernel
and the weight distribution.
P a g e | 39
Digital Image Processing Labo ratory
the user. Finally, the program waits for the user to press a key before closing the windows
using cv2.waitKey().
P a g e | 40
Digital Image Processing Labo ratory
CONCLUSION
In conclusion, the basic Digital Image Processing lab covers fundamental concepts and
techniques that are essential for anyone interested in image processing. It provides an
understanding of image representation, transformation, enhancement, and analysis, and
introduces practical skills using popular image processing tools and libraries.
Part A of the lab introduced basic techniques such as image reading and writing, color space
conversion, image arithmetic, filtering, thresholding, and contour detection. These techniques
are fundamental to image processing and are the building blocks for more advanced
applications.
Part B of the lab will cover advanced topics such as image segmentation, feature extraction,
object recognition, and deep learning for image analysis. These techniques are essential for
advanced image processing applications and will further enhance the skills and knowledge of
participants.
Overall, the lab provides a comprehensive and hands-on approach to learning image
processing, and equips participants with practical skills that can be applied to a wide range of
image processing applications.
USEFULL LINKS
P a g e | 41