1. ELC Activity Thapar Institute of Engineering and Technology 2024
ELC Activity
Image Processing
Introduction to Python for Image
Processing
2. ELC Activity Thapar Institute of Engineering and Technology 2024
Python
• What is Python?
• Python is a high-level, general-purpose programming
language.
• Python in Image Processing:
• Python is one of the widely used programming languages
for processing the Digital Images. Its amazing libraries
and tools help in achieving the task of image processing
very efficiently.
3. ELC Activity Thapar Institute of Engineering and Technology 2024
Working With Python
• To write a Python code, many IDEs are available.
• Working with Anaconda: Anaconda is a distribution of the
Python and R programming languages for scientific
computing, that aims to simplify package management and
deployment.
• Working with Google Colab: Colab, or "Colaboratory", allows
you to write and execute Python in your browser, with
– Zero configuration required
– Access to GPUs free of charge
– Easy sharing
4. ELC Activity Thapar Institute of Engineering and Technology 2024
Getting Started with Python
• Starting with Google Colab:
– Sign-in into your Gmail account and open the new
notebook in Google Colab.
• You will be redirected to the new
notebook.
5. ELC Activity Thapar Institute of Engineering and Technology 2024
Getting Started with Python
• In the new notebook:
Space to upload the data or images for
working within the session
Space to write your code
Output
6. ELC Activity Thapar Institute of Engineering and Technology 2024
Getting Started with Python
• To Run the code in notebook:
7. ELC Activity Thapar Institute of Engineering and Technology 2024
Importing Libraries
• OpenCV: It is a huge open-source library that includes several
hundreds of computer vision, machine learning, and image
processing algorithms. It provides a comprehensive set of functions
and tools that facilitate the development of applications dealing
with images and videos.
import cv2
• Pillow: The Python Imaging Library adds image processing
capabilities to your Python interpreter. This library provides
extensive file format support, an efficient internal representation,
and fairly powerful image processing capabilities.
import PIL
from PIL import Image
8. ELC Activity Thapar Institute of Engineering and Technology 2024
Importing Libraries
• NumPy: NumPy stands for Numerical Python. This library adds the
support for large, multi-dimensional arrays and matrices, along with
a large collection of high-level mathematical functions to operate
on these arrays.
import numpy as np
• Matplotlib: Matplotlib is an amazing visualization library in Python
for 2D plots of arrays. Matplotlib is a multi-platform data
visualization library built on NumPy arrays. Matplotlib consists of
several plots like line, bar, scatter, histogram, etc.
from matplotlib import pyplot as plt
9. ELC Activity Thapar Institute of Engineering and Technology 2024
Opening and Reading Image
• PIL.Image.open() Opens and identifies the given image file.
• The plt.imshow() function in pyplot module of matplotlib
library is used to display data as an image; i.e. on a 2D regular
raster.
img = Image.open("9.jpeg")
plt.imshow(img)
10. ELC Activity Thapar Institute of Engineering and Technology 2024
Opening and Reading Image
• Printing Image details
print(img.format)
print(img.size)
print(img.mode)
JPEG
(1238, 1280)
RGB
11. ELC Activity Thapar Institute of Engineering and Technology 2024
Image Pre-Processing
12. ELC Activity Thapar Institute of Engineering and Technology 2024
Converting into Gray scale
• First, convert the image into a numpy array:
img = np.array(img)
• Then convert the image into gray scale and show it:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray,cmap='gray')
13. ELC Activity Thapar Institute of Engineering and Technology 2024
Applying Thresholding
• Apply a thresholding value according to the image and then
convert it into an inverse binary image; where 1 means
contour lines and 0 means background.
_, threshold = cv2.threshold(gray, 110, 255,
cv2.THRESH_BINARY_INV)
14. ELC Activity Thapar Institute of Engineering and Technology 2024
Shape Detection
15. ELC Activity Thapar Institute of Engineering and Technology 2024
Applying Morphological Operators
• First defining the kernel shape and size to further use in
morphological operators.
• These kernels basically works as filter to either remove or add the
pixels in the contours as per the morphological operator applied.
# kernel for dilation operation
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(4,4))
# kernel for closing operation
kernelc = cv2.getStructuringElement(cv2.MORPH_RECT,(6,6))
# kernel for opening operation
kernelo = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
16. ELC Activity Thapar Institute of Engineering and Technology 2024
Applying Morphological Operators
• Apply different morphological operators, to extract the shape
boundary and discard the remaining background.
# for getting the complete shape
dilated = cv2.dilate(threshold, kernel, iterations=3)
# for connecting the disjoint edges of shapes
closing = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE,
kernelc, iterations = 3)
# for removing the blobs created because of the above two
operations
opening = cv2.morphologyEx(dilated, cv2.MORPH_OPEN,
kernelo, iterations = 8)
17. ELC Activity Thapar Institute of Engineering and Technology 2024
Applying Morphological Operators
• Apply different morphological operators, to extract the shape
boundary and discard the remaining background.
# for skeletonization
thinned = cv2.ximgproc.thinning(opening)
dilated_t = cv2.dilate(thinned, kernel, iterations=6)
18. ELC Activity Thapar Institute of Engineering and Technology 2024
Applying Morphological Operators
• Finally, the complete contours formed and detected.
• Hence, the first step of detecting the shape is completed.
Original Image Detected shapes
19. ELC Activity Thapar Institute of Engineering and Technology 2024
Shape Identification
20. ELC Activity Thapar Institute of Engineering and Technology 2024
Identifying and Drawing Contours
• Approximately identifying only the closed contour boundaries.
• cv2.approxPloyDP() function approximates the entire shape.
approx = cv2.approxPolyDP(contour, 0.02 *
cv2.arcLength(contour, True), True)
• Draw the identified contours by using drawContours() function
cv2.drawContours(img, [contour], 0, (0, 0, 255), 2))
Note: All the above function will be run within the counted contours of the shape
calculated by findContours() function.
contours,_ = cv2.findContours(dilated_t, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
21. ELC Activity Thapar Institute of Engineering and Technology 2024
Identifying and Drawing Contours
• The identified contours will look like the following:
Original Image Identified shapes
22. ELC Activity Thapar Institute of Engineering and Technology 2024
Shape Recognition
23. ELC Activity Thapar Institute of Engineering and Technology 2024
Recognizing and Putting name of shapes
• First find the center of the shape and then put name on it.
• To recognize the name of the shape, the length of the closed
contours is calculated, which is earlier calculated by
cv2.approxPloyDP() function.
• To detect the center of the point:
# finding center point of shape
M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
24. ELC Activity Thapar Institute of Engineering and Technology 2024
Recognizing and Putting name of shapes
• Recognize the shape by the number of contours in a closed shape and put
the name in the center of the each shape:
if len(approx) == 3:
cv2.putText(img, 'Triangle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 5)
elif len(approx) == 4:
cv2.putText(img, 'Rectangle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 5)
elif len(approx) == 5:
cv2.putText(img, 'Pentagon', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 5)
elif len(approx) == 6:
cv2.putText(img, 'Hexagon', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 2)
else:
cv2.putText(img, 'circle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
25. ELC Activity Thapar Institute of Engineering and Technology 2024
Recognizing and Putting name of shapes
• The final output will be like:
Original Image Final recognized shapes
26. ELC Activity Thapar Institute of Engineering and Technology 2024
Challenges
• Challenges:
– Identify the optimal Binary threshold value
– Irregular Shape Detection
– Shape detection with hazy and noisy background
– Smoothness and Continuity Improvement
27. ELC Activity Thapar Institute of Engineering and Technology 2024
Resources
• The following resources can be referred:
– Gonzalez, Rafael C. Digital image processing. Pearson education India, 3rd
edition, 2009.
– Jähne, Bernd. Digital image processing. Springer Science & Business Media,
2005.
– https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html
– https://docs.opencv.org/3.4/d2/d96/tutorial_py_table_of_contents_imgproc.
html
– https://www.bogotobogo.com/cplusplus/files/OReilly%20Learning%20OpenC
V.pdf
epsilon parameter in cv2.approxPolyDP: Parameter specifying the approximation accuracy. This is the maximum distance between the original curve and its approximation.
Cv2.RETR_TREE is RetrievalModes: retrieves all of the contours and reconstructs a full hierarchy of nested contours.
cv2.CHAIN_APPROX_SIMPLE: compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points.