SlideShare a Scribd company logo
1 of 28
ELC Activity Thapar Institute of Engineering and Technology 2024
ELC Activity
Image Processing
Introduction to Python for Image
Processing
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.
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
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.
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
ELC Activity Thapar Institute of Engineering and Technology 2024
Getting Started with Python
• To Run the code in notebook:
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
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
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)
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
ELC Activity Thapar Institute of Engineering and Technology 2024
Image Pre-Processing
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')
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)
ELC Activity Thapar Institute of Engineering and Technology 2024
Shape Detection
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))
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)
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)
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
ELC Activity Thapar Institute of Engineering and Technology 2024
Shape Identification
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)
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
ELC Activity Thapar Institute of Engineering and Technology 2024
Shape Recognition
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'])
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)
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
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
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
ELC Activity Thapar Institute of Engineering and Technology 2024
Thank You

More Related Content

Similar to Python image processing_Python image processing.pptx

On the necessity and inapplicability of python
On the necessity and inapplicability of pythonOn the necessity and inapplicability of python
On the necessity and inapplicability of pythonYung-Yu Chen
 
Presentation1.2.pptx
Presentation1.2.pptxPresentation1.2.pptx
Presentation1.2.pptxpranaykusuma
 
ANISH_and_DR.DANIEL_VRINCEANU_Presentation
ANISH_and_DR.DANIEL_VRINCEANU_PresentationANISH_and_DR.DANIEL_VRINCEANU_Presentation
ANISH_and_DR.DANIEL_VRINCEANU_PresentationAnish Patel
 
Autonomous Machines with Project Bonsai
Autonomous Machines with Project BonsaiAutonomous Machines with Project Bonsai
Autonomous Machines with Project BonsaiIvo Andreev
 
OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012Wingston
 
Continuous Evaluation of Deployed Models in Production Many high-tech industr...
Continuous Evaluation of Deployed Models in Production Many high-tech industr...Continuous Evaluation of Deployed Models in Production Many high-tech industr...
Continuous Evaluation of Deployed Models in Production Many high-tech industr...Databricks
 
Learning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method NamesLearning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method NamesDongsun Kim
 
19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx
19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx
19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptxSamridhGarg
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)Qiangning Hong
 
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherIntro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherBlue Elephant Consulting
 
Machine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedMachine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedOmid Vahdaty
 
Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015Christian Peel
 
Scilab: Computing Tool For Engineers
Scilab: Computing Tool For EngineersScilab: Computing Tool For Engineers
Scilab: Computing Tool For EngineersNaren P.R.
 
NVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflow
NVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflowNVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflow
NVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflowNVIDIA Taiwan
 
Artificial Intelligence, Machine Learning and Deep Learning
Artificial Intelligence, Machine Learning and Deep LearningArtificial Intelligence, Machine Learning and Deep Learning
Artificial Intelligence, Machine Learning and Deep LearningSujit Pal
 
alphablues - ML applied to text and image in chat bots
alphablues - ML applied to text and image in chat botsalphablues - ML applied to text and image in chat bots
alphablues - ML applied to text and image in chat botsAndré Karpištšenko
 

Similar to Python image processing_Python image processing.pptx (20)

On the necessity and inapplicability of python
On the necessity and inapplicability of pythonOn the necessity and inapplicability of python
On the necessity and inapplicability of python
 
Presentation1.2.pptx
Presentation1.2.pptxPresentation1.2.pptx
Presentation1.2.pptx
 
ANISH_and_DR.DANIEL_VRINCEANU_Presentation
ANISH_and_DR.DANIEL_VRINCEANU_PresentationANISH_and_DR.DANIEL_VRINCEANU_Presentation
ANISH_and_DR.DANIEL_VRINCEANU_Presentation
 
Autonomous Machines with Project Bonsai
Autonomous Machines with Project BonsaiAutonomous Machines with Project Bonsai
Autonomous Machines with Project Bonsai
 
OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012OpenCV @ Droidcon 2012
OpenCV @ Droidcon 2012
 
OpenCV+Android.pptx
OpenCV+Android.pptxOpenCV+Android.pptx
OpenCV+Android.pptx
 
Continuous Evaluation of Deployed Models in Production Many high-tech industr...
Continuous Evaluation of Deployed Models in Production Many high-tech industr...Continuous Evaluation of Deployed Models in Production Many high-tech industr...
Continuous Evaluation of Deployed Models in Production Many high-tech industr...
 
Learning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method NamesLearning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method Names
 
19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx
19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx
19BCS1815_PresentationAutomatic Number Plate Recognition(ANPR)P.pptx
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
 
Opencv
OpencvOpencv
Opencv
 
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each OtherIntro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
 
Machine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedMachine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data Demystified
 
Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015
 
R and C++
R and C++R and C++
R and C++
 
Tensorflow
TensorflowTensorflow
Tensorflow
 
Scilab: Computing Tool For Engineers
Scilab: Computing Tool For EngineersScilab: Computing Tool For Engineers
Scilab: Computing Tool For Engineers
 
NVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflow
NVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflowNVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflow
NVIDIA 深度學習教育機構 (DLI): Image segmentation with tensorflow
 
Artificial Intelligence, Machine Learning and Deep Learning
Artificial Intelligence, Machine Learning and Deep LearningArtificial Intelligence, Machine Learning and Deep Learning
Artificial Intelligence, Machine Learning and Deep Learning
 
alphablues - ML applied to text and image in chat bots
alphablues - ML applied to text and image in chat botsalphablues - ML applied to text and image in chat bots
alphablues - ML applied to text and image in chat bots
 

Recently uploaded

JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Python image processing_Python image processing.pptx

  • 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
  • 28. ELC Activity Thapar Institute of Engineering and Technology 2024 Thank You

Editor's Notes

  1. 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.