Digital Image Processing Lab Manual
Digital Image Processing Lab Manual
Digital Image Processing Lab Manual
Lab Report On
Course No: ICT - 4206
Course Title: Digital Image Processing Laboratory
SUBMITTED TO,
SUBMITTED BY,
Introduction 3–6
2
Introduction:-
To introduce students with python programming language, & OpenCV :
2. Double click on the downloaded file and install Python for all users, and ensure that Python
is added to your path. Click on Install now to begin. Adding Python to the path will enable us to
use the Python interpreter from any part of the filesystem.
3
3. The Optional Features include common tools and resources for Python and you can install all of them, even
if you don’t plan to use them.
➢ Documentation: recommended
➢ pip: recommended if you want to install other Python packages, such as NumPy or pandas
➢ tcl/tk and IDLE: recommended if you plan to use IDLE or follow tutorials that use it
➢ Python test suite: recommended for testing and learning
➢ py launcher and for all users: recommended to enable users to launch Python from the
command line
4. Click next
5. The Advanced Options dialog displays.
4
6. Click Install to start the installation.
To start writing Python code, you can use a text editor or integrated development environment (IDE)
such as Visual Studio Code, PyCharm, or IDLE.
5
OpenCV:
Open-Source Computer Vision Library (OpenCV)is an open source software library written in
C++ for machine learning and computer vision applications.
To install packages in PyCharm Click, Files > Settings > Project: Name > Project Interpreter
Now click on ‘+’ icon to install new packages
Now search for following packages and install them by clicking ‘Install Package’
• opencv-python
• opencv-contrib-python
And finally, click apply and Ok. Then, run the python code
6
Experiment No: 01
Theory:
In computer vision, gradient images are often used to detect edges and other important features in an
image. In this lab report, we will learn how to use OpenCV-Python to create a gradient image from an
RGB or grayscale image. We will also discuss the theory behind gradient images and how they are
computed.
A gradient image is an image where each pixel represents the strength and direction of the change in
intensity of the original image. In other words, a gradient image contains information about the rate of
change of intensity in different directions.
The gradient of an image can be computed using mathematical operators like Sobel, Scharr, or
Laplacian. These operators calculate the difference between adjacent pixels in the x and y directions to
detect edges and other important features in an image.
Implementation:
In this code, we first load the input image using cv2.imread(). Then, we convert the image to grayscale
using cv2.cvtColor(). We then calculate the gradient using the Sobel operator in both the x and y
directions using cv2.Sobel(). Finally, we combine the gradients in both directions using
cv2.addWeighted() to obtain the final gradient image. We display both the original and gradient images
using cv2.imshow() and wait for a key press to close the windows using cv2.waitKey() and
cv2.destroyAllWindows().
7
Output:
Original Image:
Gradient Image
8
Here's the code for grayscale images:
import cv2
import numpy as np
The only difference in this code is that we load the image as grayscale using the
cv2.IMREAD_GRAYSCALE flag in cv2.imread(). The rest of the code is the same as before.
Grayscale Image
Gradient Image
9
Experiment No: 02
Experiment Name: To create an opencv-python program to show a histogram of an RGB/grayscale
image
Theory:
A histogram is a graphical representation of the distribution of values in a dataset. In the context of
digital images, a histogram is a plot that shows the frequency of occurrence of pixel intensity values in
an image. The x-axis of the histogram represents the possible pixel intensity values, while the y-axis
represents the frequency or number of pixels that have that intensity value.
RGB Image Histogram:
For an RGB image, the histogram is often split into three separate graphs, one for each color channel:
red, green, and blue. The red channel graph represents the distribution of red pixel intensities, the green
channel graph represents the distribution of green pixel intensities, and the blue channel graph
represents the distribution of blue pixel intensities.
In the RGB image, each pixel is represented by a combination of red, green, and blue intensities, with
each intensity value ranging from 0 to 255. The histogram for each channel shows the number of pixels
that have a specific intensity value. For example, if there are many pixels with a high red intensity
value, then the red channel graph will have a peak at that intensity value.
Histogram Equalization:
Histogram equalization is a technique used to improve the contrast of an image. It works by
redistributing the pixel intensities in the image to span a wider range of intensity values. This is
achieved by applying a transformation function to the image, which maps the original pixel intensities
to new, more evenly distributed intensities. The result is an image with improved contrast and visual
quality.
Implementation:
In this code, we first load the input image using cv2.imread(). Then, we calculate the histogram for
each channel using cv2.calcHist(). Finally, we plot the histograms for each channel using plt.plot(). We
set the x-limit to [0, 256] using plt.xlim(), set a title using plt.title(), and display the plot using
plt.show().
10
Python Code:
Here's the code for RGB images:
import cv2
import numpy as np
from matplotlib import pyplot as plt
Output:
Original Image:
11
Histogram (RGB):
12
The only difference in this code is that we load the image as grayscale using the
cv2.IMREAD_GRAYSCALE flag in cv2.imread(). The rest of the code is the same as before
Original Image:
Histogram (RGB):
13
Experiment No: 03
Experiment Name: To create an opencv-python program in order to convert a given
RGB/grayscale image to a negative image.
Theory:
Image negative:
An image negative is a version of an image in which the colors or grayscale values are inverted or
reversed. In other words, the negative of an image has the opposite colors or grayscale values of the
original image. For example, in a grayscale image, the darkest pixels become the brightest, and the
brightest pixels become the darkest, while all other grayscale values are reversed accordingly. Similarly,
in a color image, the red, green, and blue color channels are inverted, resulting in an image with the
opposite color scheme.
Image negatives are commonly used in photography and image processing to create artistic effects,
enhance image contrast, or correct color balance.
To convert an RGB or grayscale image to a negative image, we invert the pixel values. In other words,
we subtract the pixel value from the maximum value possible. For a grayscale image, the maximum
value is 255, so to get the negative image, we subtract each pixel value from 255. For an RGB image,
we do the same for each color channel.
14
Python Code:
import cv2
Output:
15
Experiment No: 04
Theory:
Image Enhancement: Image enhancement refers to the process of improving the visual quality of an
image. It involves techniques that can be used to improve the contrast, brightness, sharpness, color, and
other visual properties of an image. Image enhancement is often used to improve the quality of images
captured in low light conditions or to extract more information from images for analysis or
visualization.
There are various techniques used for image enhancement, some of which are:
• Color adjustment: Color adjustment involves changing the color balance of an image to improve
its visual appeal. This can be done by adjusting the brightness, saturation, or hue of an image.
• Sharpening: Sharpening is a technique used to enhance the edges in an image, making them
more distinct and clearer. This can be done by increasing the contrast of the edges or by using
filters to enhance the sharpness of the image.
Image enhancement can be performed manually or automatically, depending on the application and the
desired outcome. It is commonly used in fields such as medical imaging, forensic analysis, and digital
photography, among others.
16
Python Code:
from PIL import Image
from PIL import ImageEnhance
Output:
17
Experiment No: 05
Experiment Name: To create an opencv-python program to enhance brightness of a given
RGB/grayscale image.
Theory:
Brightness: Brightness is a visual perception of how much light an object emits or reflects, and it is
an important attribute in image enhancement. In image processing, brightness is often used to refer to
the overall level of lightness or darkness in an image. It is controlled by adjusting the intensity values
of the pixels in the image. Increasing the brightness of an image makes it appear lighter and more
washed out, while decreasing the brightness makes it appear darker and more saturated.
Brightness adjustments are commonly used in image enhancement to improve the visibility of details,
enhance contrast, or correct exposure problems. They can be performed by changing the overall
intensity of the image, or by selectively adjusting the brightness of specific regions or objects within
the image. Brightness adjustments can be made manually using software tools like image editors or
automated using algorithms that analyze the image and make adjustments based on predefined rules or
criteria.
Python Code:
from PIL import Image
from PIL import ImageEnhance
# Enhance Brightness
curr_bri = ImageEnhance.Brightness(image)
new_bri = 2.5
18
Output:
19
Experiment No: 06
Theory:
Contrast: In image enhancement, contrast refers to the difference in brightness and color intensity
between different parts of an image. Increasing contrast can make an image appear more vivid and
improve its visual appeal.
An image with high contrast has a large difference between the lightest and darkest parts of the image,
while an image with low contrast has a smaller difference. Increasing contrast can help bring out details
in the image, making it appear sharper and more defined. Conversely, decreasing contrast can make an
image appear softer and more subdued.
Contrast can be adjusted using various image editing tools such as brightness and contrast adjustments,
curves adjustments, and levels adjustments. These tools allow users to manipulate the brightness and
darkness of different areas of an image, and adjust the overall contrast to achieve the desired effect.
Python Code:
from PIL import Image
from PIL import ImageEnhance
# Enhance Contrast
curr_con = ImageEnhance.Contrast(image)
#new_con = 0.3
new_con = 8.3
20
Output:
21
Experiment No: 07
Theory:
Image Segmentation: Image segmentation is the process of dividing an image into multiple
regions or segments, each of which corresponds to a distinct object or region of interest within the
image. The goal of image segmentation is to simplify and/or change the representation of an image into
something more meaningful and easier to analyze.
Image segmentation is an important technique in computer vision and image processing, and is used in
a variety of applications such as medical image analysis, object recognition and tracking, and scene
understanding in autonomous vehicles.
There are several methods for performing image segmentation, including thresholding, edge detection,
region growing, and clustering. Each method has its own strengths and weaknesses and the choice of
method depends on the specific application and the characteristics of the image being segmented.
Thresholding: Thresholding is a simple yet powerful technique in image segmentation that is used
to separate an image into different regions based on the pixel intensity values. The basic idea behind
thresholding is to convert a grayscale or color image into a binary image by assigning a pixel value of
1 (white) to pixels whose intensity values are above a certain threshold, and a pixel value of 0 (black)
to pixels whose intensity values are below the threshold.
Implementation:
In this code, we first load the input image using cv2.imread(). Then, we convert it to grayscale using
cv2.cvtColor(). After that, we apply thresholding using cv2.threshold(). We pass the grayscale image,
the threshold value, the maximum value (which is 255 for an 8-bit image), and the thresholding type
(which is cv2.THRESH_BINARY in this case). Finally, we display the thresholded image using
cv2.imshow(), wait for a key press using cv2.waitKey(), and destroy all windows using
cv2.destroyAllWindows().
22
Python Code:
Here's the code for RGB images:
import cv2
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply thresholding
threshold_value = 127 # Set your threshold value here
ret, thresh = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)
Output:
Thresholded Image
23
Experiment No: 08
Theory:
There are several algorithms that can be used for contour detection, including Canny edge detection,
Sobel edge detection, and Laplacian edge detection. These algorithms work by identifying points in the
image where there is a sudden change in brightness or color, which typically occur at the edges of
objects or regions.
The Canny edge detection algorithm is one of the most widely used algorithms for contour detection.
It involves several steps, including:
Python Code:
import cv2
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply thresholding
threshold_value = 127 # Set your threshold value here
ret, thresh = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)
# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
24
# Display the result
cv2.imshow('Contour Image', contour_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Original Image
25
Experiment No: 09
Theory:
K-means algorithm:
K-means algorithm is a clustering technique that can be used for image segmentation. In image
segmentation, the goal is to divide an image into different regions or segments such that each segment
contains pixels with similar properties, such as color or texture. K-means algorithm can be used to
group pixels with similar properties into clusters, which can then be used to define the different
segments of the image.
The K-means algorithm works by first selecting a number of clusters (K) and initializing them with
random pixel values. Each pixel in the image is then assigned to the nearest cluster based on its color
or texture properties. The mean of each cluster is then calculated, and the pixel assignments are updated
based on the new cluster means. This process is repeated until convergence, which is typically achieved
when the cluster means no longer change significantly.
• Initialize the K cluster centers randomly. This can be done by selecting K pixels at random from
the image or by generating K random color or texture values.
• Assign each pixel in the image to the nearest cluster center. This can be done by computing the
distance between each pixel and each cluster center and assigning the pixel to the cluster with
the closest center.
• Recalculate the cluster centers by taking the mean color or texture value of all pixels assigned
to each cluster.
• Repeat steps 3 and 4 until the cluster centers no longer change significantly, or until a maximum
number of iterations is reached.
• Assign each pixel in the image to the final cluster that it belongs to.
26
Python Code:
import cv2
import numpy as np
for i in range(k):
mask[labels == i] = centers[i]
27
Output:
Original Image
28
Experiment No: 10
Theory:
Image Sharpening: Image sharpening is a technique in image processing that enhances the edges and
details of an image, making it appear clearer and more defined. The goal of image sharpening is to
improve the visual quality of an image by emphasizing the edges and edges details, which may have
been lost due to factors such as blurring, noise, or low resolution.
Image sharpening is widely used in various applications, including photography, medical imaging, and
computer vision.
Sharpen Filter: A sharpen filter is a type of image filter used in image processing to enhance the edges
and details of an image, making it appear clearer and more defined. There are several types of sharpen
filters, but one of the most common is the Laplacian filter. The Laplacian filter works by computing
the second derivative of the image intensity with respect to position, which highlights regions of the
image where the intensity changes rapidly, such as edges and corners.
To implementing image sharpening using the Laplacian filter:
• Load the image that you want to sharpen using an image processing library such as OpenCV or
Pillow.
• Convert the image to grayscale, as sharpening is often applied to the luminance (brightness)
component of an image rather than the color components.
• Apply a Gaussian blur to the image to remove noise and other high-frequency components that
may interfere with the sharpening process.
• Apply the Laplacian filter to the image using convolution. The Laplacian filter can be
implemented as a kernel, which is a small matrix of numbers that is used to modify the values
of neighboring pixels.
• Add the Laplacian-filtered image back to the original image using a suitable weighting factor,
such as 0.5 or 0.7. This will enhance the edges and details in the image while preserving the
overall appearance.
29
Python Code:
img = cv2.imread('shrp.jpg')
Output:
Original Image:
Sharpned Image:
30
Experiment No: 11
Theory:
Skeletonize: Skeletonization, also known as thinning, is a morphological operation used to extract the
essential features of an object in an image while preserving its shape and topology. It involves
iteratively removing pixels from the boundary of the object until only a one-pixel-wide skeletonized
version of the object remains. The result is a simplified representation of the object that can be used for
further analysis or processing. Skeletonization is commonly used in image processing for applications
such as object recognition, shape analysis, and pattern matching.
Algorithm:
1. Import necessary libraries: PIL, cv2, matplotlib.pyplot, and numpy.
2. Define a function named "skeletonize" that takes an input image as an argument.
3. Create a structuring element using cv2.getStructuringElement() function.
4. Create an empty array with the same size and data type as the input image using np.zeros()
function.
5. Set a flag variable "flag" to zero.
6. Create a while loop with condition "while (not flag)".
7. Inside the while loop, erode the input image using cv2.erode() function and the structuring
element.
8. Dilate the eroded image using cv2.dilate() function and the structuring element.
9. Subtract the dilated image from the input image using cv2.subtract() function.
10. Use cv2.bitwise_or() function to apply the opened image to the output image.
11. Copy the eroded image to the input image.
12. Calculate the number of zeros in the input image using cv2.countNonZero() function.
13. If the number of zeros is equal to the size of the image, set the flag variable to 1, else set it to 0.
14. Create a kernel using np.ones() function.
15. Dilate the output image using cv2.dilate() function and the kernel.
16. Apply median blur to the dilated image using cv2.medianBlur() function and kernel size 5.
17. Threshold the blurred image using cv2.threshold() function with threshold value 127 and max
value 255.
18. Save the resulting image as 'skeleton.png' using cv2.imwrite() function.
19. Display the original input image and the skeletonized image using cv2.imshow() function.
20. Wait for a key press using cv2.waitKey() function.
31
Python Code:
import numpy as np
import cv2
img = cv2.imread('Thumb.png',0)
ret,img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
flag = 0
while(not flag):
eroded = cv2.erode(im, element)
opened = cv2.dilate(eroded, element)
opened = cv2.subtract(im,opened)
out = cv2.bitwise_or(out,opened)
im = eroded.copy()
zeros = img.size - cv2.countNonZero(im)
flag = 1 if (zeros == img.size) else 0
return out
output = findSkeleton(img)
kernel = np.ones((3,3),np.uint8)
output = cv2.dilate(output,kernel)
output = cv2.medianBlur(output, 5)
ret,thresh = cv2.threshold(output,127,255,cv2.THRESH_BINARY_INV)
Output:
32
Experiment No: 12
Theory:
Algorithm:
1. Load the input image using cv2.imread() and store it in a variable (e.g., img).
2. Define the kernel using np.ones() and specify the kernel size (e.g., 3x3).
3. Divide the kernel by the sum of its elements to ensure that the output image has the same
brightness as the input image.
4. Apply the filter to the input image using cv2.filter2D() function and store the result in a variable
(e.g., dst).
5. Save the smoothed image using cv2.imwrite() function and specify the filename.
6. Display the original image using cv2.imshow() function.
7. Call the function to smooth the image by the 2D filter.
8. Display the smoothed image using cv2.imshow() function.
9. Wait for a key event using cv2.waitKey(0) function.
33
Python Code:
import cv2
import numpy as np
Output:
34
Experiment No: 13
Theory:
Average filter: An average filter, also known as a mean filter, is a type of linear filter commonly used
in digital image processing. It operates by replacing the value of each pixel with the average value of
its neighboring pixels. The size of the neighborhood is typically defined by a square or rectangular
window, and the values in the window are all given equal weight. The result is a smoothing effect on
the image, which can help to reduce noise and blur sharp edges. However, it can also result in loss of
detail and blurring of important features, so it must be used judiciously.
Algorithm:
35
Python Code:
import cv2
import numpy as np
Output:
36
Experiment No: 14
Theory:
Gaussian filter: Gaussian filter is a type of linear filter used in image processing to smooth or blur an
image. It works by convolving the image with a Gaussian function to produce a smoothed image with
reduced noise and details. The Gaussian filter is designed to give more weight to the central pixels and
less weight to the surrounding pixels, creating a smoothing effect that is more natural and less likely to
create artifacts than other filters.
Algorithm:
37
Python Code:
import cv2
Output:
38
Experiment No: 15
Theory:
Median Filter: A median filter is a digital signal processing filter that replaces each pixel's value with
the median value of its neighboring pixels. It is commonly used for image denoising to remove salt-
and-pepper noise, which can occur during image acquisition or transmission. The median filter is
effective in preserving edges and small details in an image while reducing noise.
Algorithm:
1. Import the required libraries: cv2, matplotlib, and numpy
2. Load the input image using the cv2.imread() function
3. Define a function named "smooth_image_median_filter" that takes the input image as an
argument.
4. In the function, define a kernel of size 5x5 using the numpy.ones() function and divide it by 25
to get a normalized kernel.
5. Apply the median filter using the cv2.medianBlur() function with a kernel size of 5x5 on the
input image.
6. Save the smoothed image using the cv2.imwrite() function with a filename
'smoothed_median_filter.png'
7. Display the original and smoothed images using the cv2.imshow() and cv2.imread() functions
respectively.
8. Wait for a key event using the cv2.waitKey() function
39
Python Code:
import cv2
Output:
40
Experiment No: 16
Theory:
Bilateral filter: A bilateral filter is a non-linear, edge-preserving smoothing filter that is used to reduce
noise and smooth images while preserving edges. It is a type of filter that takes into account both the
spatial and range distances between the pixels in an image. The bilateral filter uses a Gaussian filter in
the spatial domain and a range filter in the intensity domain to smooth the image. The range filter
weights the contribution of each pixel to the filtered output based on how similar its intensity value is
to the central pixel's intensity value. The bilateral filter is effective in smoothing images while
preserving edges, and it is commonly used in image processing and computer vision applications.
Algorithm:
1. Load the image 'abc.jpg' using cv2.imread() function and store it in a variable 'img'.
2. Define a function smooth_image_bilateral_filter(img) that takes an image as an argument.
3. Define a kernel of size 5x5 with all elements as 1 and divided by 25 using np.ones() and
np.float32() functions.
4. Apply the bilateral filter using cv2.bilateralFilter() function with 3 arguments: image, diameter
of each pixel neighborhood, color sigma, and space sigma. Store the result in a variable 'dst'.
5. Save the smoothed image using cv2.imwrite() function with the name
'smoothed_bilateral_filter.png'.
6. Display the original image using cv2.imshow() function with the title 'Original Image' and image
'img'.
7. Call the function smooth_image_bilateral_filter(img).
8. Display the smoothed image using cv2.imshow() function with the title 'Smoothed Image' and
image read from the saved file 'smoothed_bilateral_filter.png'.
9. Wait for the user to press any key using cv2.waitKey(0) function.
41
Python Code:
import cv2
Output:
42