SlideShare a Scribd company logo
1 of 45
Download to read offline
Make images come
alive with scikit-
image
IMAGE PROCESSING IN PYTHON
Rebeca Gonzalez
Data Engineer
IMAGE PROCESSING IN PYTHON
What is image processing?
Operations on images and videos to:
Enhance an image
Extract useful information
Analyze it and make decisions
IMAGE PROCESSING IN PYTHON
What is image processing?
Operations to on images and videos to:
Enhance an image
Extract useful information
Analyze it and make decisions
IMAGE PROCESSING IN PYTHON
Applications
Medical image analysis
Arti cial intelligence
Image restoration and enhancement
Geospatial computing
Surveillance
Robotic vision
Automotive safety
And many more...
IMAGE PROCESSING IN PYTHON
Purposes
1. Visualization:
Objects that are not visible
2. Image sharpening and restoration
A be er image
3. Image retrieval
Seek for the image of interest
4. Measurement of pa ern
Measures various objects
5. Image Recognition
Distinguish objects in an image
IMAGE PROCESSING IN PYTHON
Intro to scikit-image
Easy to use
Makes use of Machine Learning
Out of the box complex algorithms
IMAGE PROCESSING IN PYTHON
What is an image?
IMAGE PROCESSING IN PYTHON
What is an image?
IMAGE PROCESSING IN PYTHON
Images in scikit-image
from skimage import data
rocket_image = data.rocket()
IMAGE PROCESSING IN PYTHON
RGB channels
IMAGE PROCESSING IN PYTHON
Grayscaled images
IMAGE PROCESSING IN PYTHON
RGB vs Grayscale
from skimage import color
grayscale = color.rgb2gray(original)
rgb = color.gray2rgb(grayscale)
IMAGE PROCESSING IN PYTHON
Visualizing images in the course
Don't worry about Matplotlib!
def show_image(image, title='Image', cmap_type='gray'):
plt.imshow(image, cmap=cmap_type)
plt.title(title)
plt.axis('off')
plt.show()
IMAGE PROCESSING IN PYTHON
Visualizing images in the course
from skimage import color
grayscale = color.rgb2gray(original)
show_image(grayscale, "Grayscale")
Let's practice!
IMAGE PROCESSING IN PYTHON
NumPy for images
IMAGE PROCESSING IN PYTHON
Rebeca Gonzalez
Data Engineer
IMAGE PROCESSING IN PYTHON
NumPy for images
Fundamentals of image processing
techniques
Flipping
Extract and analyze features
IMAGE PROCESSING IN PYTHON
Images as NdArrays
# Loading the image using Matplotlib
madrid_image = plt.imread('/madrid.jpeg')
type(madrid_image)
<class 'numpy.ndarray'>
IMAGE PROCESSING IN PYTHON
Colors with NumPy
IMAGE PROCESSING IN PYTHON
Colors with NumPy
# Obtaining the red values of the image
red = image[:, :, 0]
# Obtaining the green values of the image
green = image[:, :, 1]
# Obtaining the blue values of the image
blue = image[:, :, 2]
IMAGE PROCESSING IN PYTHON
Colors with NumPy
plt.imshow(red, cmap="gray")
plt.title('Red')
plt.axis('off')
plt.show()
IMAGE PROCESSING IN PYTHON
Shapes
# Accessing the shape of the image
madrid_image.shape
(426, 640, 3)
IMAGE PROCESSING IN PYTHON
Sizes
# Accessing the shape of the image
madrid_image.size
817920
IMAGE PROCESSING IN PYTHON
Flipping images: vertically
# Flip the image in up direction
vertically_flipped = np.flipud(madrid_image)
show_image(vertically_flipped, 'Vertically flipped image')
IMAGE PROCESSING IN PYTHON
Flipping images: horizontally
# Flip the image in left direction
horizontally_flipped = np.fliplr(madrid_image)
show_image(horizontally_flipped, 'Horizontally flipped image')
IMAGE PROCESSING IN PYTHON
What is a histogram?
IMAGE PROCESSING IN PYTHON
Color histograms
IMAGE PROCESSING IN PYTHON
Applications of histograms
Analysis
Thresholding
Brightness and contrast
Equalize an image
IMAGE PROCESSING IN PYTHON
Histograms in Matplotlib
# Red color of the image
red = image[:, :, 0]
# Obtain the red histogram
plt.hist(red.ravel(), bins=256)
IMAGE PROCESSING IN PYTHON
Visualizing histograms with Matplotlib
blue = image[:, :, 2]
plt.hist(blue.ravel(), bins=256)
plt.title('Blue Histogram')
plt.show()
Let's practice!
IMAGE PROCESSING IN PYTHON
Getting started with
thresholding
IMAGE PROCESSING IN PYTHON
Rebeca Gonzalez
Data Engineer
IMAGE PROCESSING IN PYTHON
Thresholding
Partitioning an image into a foreground and
background
By making it black and white
We do so by se ing each pixel to:
255 (white) if pixel > thresh value
0 (black) if pixel < thresh value
IMAGE PROCESSING IN PYTHON
Thresholding
Simplest method of image segmentation
Isolate objects
Object detection
Face detection
Etc.
IMAGE PROCESSING IN PYTHON
Thresholding
Only from grayscale images
IMAGE PROCESSING IN PYTHON
Apply it
# Obtain the optimal threshold value
thresh = 127
# Apply thresholding to the image
binary = image > thresh
# Show the original and thresholded
show_image(image, 'Original')
show_image(binary, 'Thresholded')
IMAGE PROCESSING IN PYTHON
Inverted thresholding
# Obtain the optimal threshold value
thresh = 127
# Apply thresholding to the image
inverted_binary = image <= thresh
# Show the original and thresholded
show_image(image, 'Original')
show_image(inverted_binary,
'Inverted thresholded')
IMAGE PROCESSING IN PYTHON
Categories
Global or histogram based: good for uniform backgrounds
Local or adaptive: for uneven background illumination
IMAGE PROCESSING IN PYTHON
Try more thresholding algorithms
from skimage.filters import try_all_threshold
# Obtain all the resulting images
fig, ax = try_all_threshold(image, verbose=False)
# Showing resulting plots
show_plot(fig, ax)
IMAGE PROCESSING IN PYTHON
Try more thresholding algorithms
IMAGE PROCESSING IN PYTHON
Optimal thresh value
Global
Uniform background
# Import the otsu threshold function
from skimage.filters import threshold_otsu
# Obtain the optimal threshold value
thresh = threshold_otsu(image)
# Apply thresholding to the image
binary_global = image > thresh
IMAGE PROCESSING IN PYTHON
Optimal thresh value
Global
# Show the original and binarized image
show_image(image, 'Original')
show_image(binary_global, 'Global thresholding')
IMAGE PROCESSING IN PYTHON
Optimal thresh value
Local
Uneven background
# Import the local threshold function
from skimage.filters import threshold_local
# Set the block size to 35
block_size = 35
# Obtain the optimal local thresholding
local_thresh = threshold_local(text_image, block_size, offset=10)
# Apply local thresholding and obtain the binary image
binary_local = text_image > local_thresh
IMAGE PROCESSING IN PYTHON
Optimal thresh value
Local
# Show the original and binarized image
show_image(image, 'Original')
show_image(binary_local, 'Local thresholding')
Let's practice!
IMAGE PROCESSING IN PYTHON

More Related Content

Similar to Chapter1 8

Automated Face Detection System
Automated Face Detection SystemAutomated Face Detection System
Automated Face Detection SystemAbhiroop Ghatak
 
Image processing presentation
Image processing presentationImage processing presentation
Image processing presentationBibus Poudel
 
Matlab Image Enhancement Techniques
Matlab Image Enhancement TechniquesMatlab Image Enhancement Techniques
Matlab Image Enhancement Techniquesmatlab Content
 
Basics of image processing using MATLAB
Basics of image processing using MATLABBasics of image processing using MATLAB
Basics of image processing using MATLABMohsin Siddique
 
Digital Image Processing (Lab 07)
Digital Image Processing (Lab 07)Digital Image Processing (Lab 07)
Digital Image Processing (Lab 07)Moe Moe Myint
 
Final Report for project
Final Report for projectFinal Report for project
Final Report for projectRajarshi Roy
 
Image proccessing slide share
Image proccessing slide shareImage proccessing slide share
Image proccessing slide shareSyedShaiby
 
CE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdfCE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdfUmarMustafa13
 
Digital Image Processing using MatLAB with Arduino
Digital Image Processing using MatLAB with Arduino Digital Image Processing using MatLAB with Arduino
Digital Image Processing using MatLAB with Arduino Shivang Rana
 
Labcamp - working with image processing
Labcamp - working with image processingLabcamp - working with image processing
Labcamp - working with image processingRenato Souza
 
3 track kinect@Bicocca - sdk e camere
3   track kinect@Bicocca - sdk e camere3   track kinect@Bicocca - sdk e camere
3 track kinect@Bicocca - sdk e camereMatteo Valoriani
 
Digital image forgery detection
Digital image forgery detectionDigital image forgery detection
Digital image forgery detectionAB Rizvi
 
Palestra - Utilizando tensorflow mobile e seus desafios
Palestra - Utilizando tensorflow mobile e seus desafiosPalestra - Utilizando tensorflow mobile e seus desafios
Palestra - Utilizando tensorflow mobile e seus desafiosGustavo Monteiro
 
Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.
Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.
Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.Lviv Startup Club
 

Similar to Chapter1 8 (20)

Automated Face Detection System
Automated Face Detection SystemAutomated Face Detection System
Automated Face Detection System
 
Image processing presentation
Image processing presentationImage processing presentation
Image processing presentation
 
Matlab Image Enhancement Techniques
Matlab Image Enhancement TechniquesMatlab Image Enhancement Techniques
Matlab Image Enhancement Techniques
 
Image processing
Image processingImage processing
Image processing
 
Basics of image processing using MATLAB
Basics of image processing using MATLABBasics of image processing using MATLAB
Basics of image processing using MATLAB
 
Topic 1_PPT.pptx
Topic 1_PPT.pptxTopic 1_PPT.pptx
Topic 1_PPT.pptx
 
Digital Image Processing (Lab 07)
Digital Image Processing (Lab 07)Digital Image Processing (Lab 07)
Digital Image Processing (Lab 07)
 
Final Report for project
Final Report for projectFinal Report for project
Final Report for project
 
Image proccessing slide share
Image proccessing slide shareImage proccessing slide share
Image proccessing slide share
 
CE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdfCE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdf
 
Digital Image Processing using MatLAB with Arduino
Digital Image Processing using MatLAB with Arduino Digital Image Processing using MatLAB with Arduino
Digital Image Processing using MatLAB with Arduino
 
Image processing in MATLAB
Image processing in MATLABImage processing in MATLAB
Image processing in MATLAB
 
Image Processing Using MATLAB
Image Processing Using MATLABImage Processing Using MATLAB
Image Processing Using MATLAB
 
Vanmathy python
Vanmathy python Vanmathy python
Vanmathy python
 
Labcamp - working with image processing
Labcamp - working with image processingLabcamp - working with image processing
Labcamp - working with image processing
 
3 track kinect@Bicocca - sdk e camere
3   track kinect@Bicocca - sdk e camere3   track kinect@Bicocca - sdk e camere
3 track kinect@Bicocca - sdk e camere
 
Dip review
Dip reviewDip review
Dip review
 
Digital image forgery detection
Digital image forgery detectionDigital image forgery detection
Digital image forgery detection
 
Palestra - Utilizando tensorflow mobile e seus desafios
Palestra - Utilizando tensorflow mobile e seus desafiosPalestra - Utilizando tensorflow mobile e seus desafios
Palestra - Utilizando tensorflow mobile e seus desafios
 
Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.
Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.
Eugene Khvedchenya. State of the art Image Augmentations with Albumentations.
 

Recently uploaded

Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 

Recently uploaded (20)

Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 

Chapter1 8

  • 1. Make images come alive with scikit- image IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer
  • 2. IMAGE PROCESSING IN PYTHON What is image processing? Operations on images and videos to: Enhance an image Extract useful information Analyze it and make decisions
  • 3. IMAGE PROCESSING IN PYTHON What is image processing? Operations to on images and videos to: Enhance an image Extract useful information Analyze it and make decisions
  • 4. IMAGE PROCESSING IN PYTHON Applications Medical image analysis Arti cial intelligence Image restoration and enhancement Geospatial computing Surveillance Robotic vision Automotive safety And many more...
  • 5. IMAGE PROCESSING IN PYTHON Purposes 1. Visualization: Objects that are not visible 2. Image sharpening and restoration A be er image 3. Image retrieval Seek for the image of interest 4. Measurement of pa ern Measures various objects 5. Image Recognition Distinguish objects in an image
  • 6. IMAGE PROCESSING IN PYTHON Intro to scikit-image Easy to use Makes use of Machine Learning Out of the box complex algorithms
  • 7. IMAGE PROCESSING IN PYTHON What is an image?
  • 8. IMAGE PROCESSING IN PYTHON What is an image?
  • 9. IMAGE PROCESSING IN PYTHON Images in scikit-image from skimage import data rocket_image = data.rocket()
  • 10. IMAGE PROCESSING IN PYTHON RGB channels
  • 11. IMAGE PROCESSING IN PYTHON Grayscaled images
  • 12. IMAGE PROCESSING IN PYTHON RGB vs Grayscale from skimage import color grayscale = color.rgb2gray(original) rgb = color.gray2rgb(grayscale)
  • 13. IMAGE PROCESSING IN PYTHON Visualizing images in the course Don't worry about Matplotlib! def show_image(image, title='Image', cmap_type='gray'): plt.imshow(image, cmap=cmap_type) plt.title(title) plt.axis('off') plt.show()
  • 14. IMAGE PROCESSING IN PYTHON Visualizing images in the course from skimage import color grayscale = color.rgb2gray(original) show_image(grayscale, "Grayscale")
  • 16. NumPy for images IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer
  • 17. IMAGE PROCESSING IN PYTHON NumPy for images Fundamentals of image processing techniques Flipping Extract and analyze features
  • 18. IMAGE PROCESSING IN PYTHON Images as NdArrays # Loading the image using Matplotlib madrid_image = plt.imread('/madrid.jpeg') type(madrid_image) <class 'numpy.ndarray'>
  • 19. IMAGE PROCESSING IN PYTHON Colors with NumPy
  • 20. IMAGE PROCESSING IN PYTHON Colors with NumPy # Obtaining the red values of the image red = image[:, :, 0] # Obtaining the green values of the image green = image[:, :, 1] # Obtaining the blue values of the image blue = image[:, :, 2]
  • 21. IMAGE PROCESSING IN PYTHON Colors with NumPy plt.imshow(red, cmap="gray") plt.title('Red') plt.axis('off') plt.show()
  • 22. IMAGE PROCESSING IN PYTHON Shapes # Accessing the shape of the image madrid_image.shape (426, 640, 3)
  • 23. IMAGE PROCESSING IN PYTHON Sizes # Accessing the shape of the image madrid_image.size 817920
  • 24. IMAGE PROCESSING IN PYTHON Flipping images: vertically # Flip the image in up direction vertically_flipped = np.flipud(madrid_image) show_image(vertically_flipped, 'Vertically flipped image')
  • 25. IMAGE PROCESSING IN PYTHON Flipping images: horizontally # Flip the image in left direction horizontally_flipped = np.fliplr(madrid_image) show_image(horizontally_flipped, 'Horizontally flipped image')
  • 26. IMAGE PROCESSING IN PYTHON What is a histogram?
  • 27. IMAGE PROCESSING IN PYTHON Color histograms
  • 28. IMAGE PROCESSING IN PYTHON Applications of histograms Analysis Thresholding Brightness and contrast Equalize an image
  • 29. IMAGE PROCESSING IN PYTHON Histograms in Matplotlib # Red color of the image red = image[:, :, 0] # Obtain the red histogram plt.hist(red.ravel(), bins=256)
  • 30. IMAGE PROCESSING IN PYTHON Visualizing histograms with Matplotlib blue = image[:, :, 2] plt.hist(blue.ravel(), bins=256) plt.title('Blue Histogram') plt.show()
  • 32. Getting started with thresholding IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer
  • 33. IMAGE PROCESSING IN PYTHON Thresholding Partitioning an image into a foreground and background By making it black and white We do so by se ing each pixel to: 255 (white) if pixel > thresh value 0 (black) if pixel < thresh value
  • 34. IMAGE PROCESSING IN PYTHON Thresholding Simplest method of image segmentation Isolate objects Object detection Face detection Etc.
  • 35. IMAGE PROCESSING IN PYTHON Thresholding Only from grayscale images
  • 36. IMAGE PROCESSING IN PYTHON Apply it # Obtain the optimal threshold value thresh = 127 # Apply thresholding to the image binary = image > thresh # Show the original and thresholded show_image(image, 'Original') show_image(binary, 'Thresholded')
  • 37. IMAGE PROCESSING IN PYTHON Inverted thresholding # Obtain the optimal threshold value thresh = 127 # Apply thresholding to the image inverted_binary = image <= thresh # Show the original and thresholded show_image(image, 'Original') show_image(inverted_binary, 'Inverted thresholded')
  • 38. IMAGE PROCESSING IN PYTHON Categories Global or histogram based: good for uniform backgrounds Local or adaptive: for uneven background illumination
  • 39. IMAGE PROCESSING IN PYTHON Try more thresholding algorithms from skimage.filters import try_all_threshold # Obtain all the resulting images fig, ax = try_all_threshold(image, verbose=False) # Showing resulting plots show_plot(fig, ax)
  • 40. IMAGE PROCESSING IN PYTHON Try more thresholding algorithms
  • 41. IMAGE PROCESSING IN PYTHON Optimal thresh value Global Uniform background # Import the otsu threshold function from skimage.filters import threshold_otsu # Obtain the optimal threshold value thresh = threshold_otsu(image) # Apply thresholding to the image binary_global = image > thresh
  • 42. IMAGE PROCESSING IN PYTHON Optimal thresh value Global # Show the original and binarized image show_image(image, 'Original') show_image(binary_global, 'Global thresholding')
  • 43. IMAGE PROCESSING IN PYTHON Optimal thresh value Local Uneven background # Import the local threshold function from skimage.filters import threshold_local # Set the block size to 35 block_size = 35 # Obtain the optimal local thresholding local_thresh = threshold_local(text_image, block_size, offset=10) # Apply local thresholding and obtain the binary image binary_local = text_image > local_thresh
  • 44. IMAGE PROCESSING IN PYTHON Optimal thresh value Local # Show the original and binarized image show_image(image, 'Original') show_image(binary_local, 'Local thresholding')