Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi 3 Cookbook for Python Programmers

You're reading from   Raspberry Pi 3 Cookbook for Python Programmers Unleash the potential of Raspberry Pi 3 with over 100 recipes

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher
ISBN-13 9781788629874
Length 552 pages
Edition 3rd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Steven Lawrence Fernandes Steven Lawrence Fernandes
Author Profile Icon Steven Lawrence Fernandes
Steven Lawrence Fernandes
Tim Cox Tim Cox
Author Profile Icon Tim Cox
Tim Cox
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started with a Raspberry Pi 3 Computer 2. Dividing Text Data and Building Text Classifiers FREE CHAPTER 3. Using Python for Automation and Productivity 4. Predicting Sentiments in Words 5. Creating Games and Graphics 6. Detecting Edges and Contours in Images 7. Creating 3D Graphics 8. Building Face Detector and Face Recognition Applications 9. Using Python to Drive Hardware 10. Sensing and Displaying Real-World Data 11. Building Neural Network Modules for Optical Character Recognition 12. Building Robots 13. Interfacing with Technology 14. Can I Recommend a Movie for You? 15. Hardware and Software List 16. Other Books You May Enjoy

Erosion and dilation


Erosion and dilation are morphological operations. Erosion removes pixels at the boundaries of objects in an image and dilation adds pixels to the boundaries of objects in an image.

How to do it...

  1. Import the Computer Vision package – cv2:
import cv2 
  1. Import the numerical Python package – numpy as np:
import numpy as np 
  1. Read the image using the built-in imread function:
image = cv2.imread('image_4.jpg')
  1. Display the original image using the built-in imshow function:
cv2.imshow("Original", image) 
  1. Wait until any key is pressed:
cv2.waitKey(0) 
  1. Given shape and type, fill it with ones:
# np.ones(shape, dtype) 
# 5 x 5 is the dimension of the kernel, uint8: is an unsigned integer (0 to 255) 
kernel = np.ones((5,5), dtype = "uint8") 
  1. cv2.erode is the built-in function used for erosion:
# cv2.erode(image, kernel, iterations) 
erosion = cv2.erode(image, kernel, iterations = 1) 
  1. Display the image after erosion using the built-in imshow function:
cv2.imshow("Erosion", erosion) 
  1. Wait until any...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime