Python OpenCV Computer Vision Training PDF
Python OpenCV Computer Vision Training PDF
Vision Training
Website:www.tertiarycourses.com.my
Email: enquiry@tertiaryinfotech.com
About the Trainer
- What is OpenCV
- Install OpenCV
- Test OpenCV
5
Exercise Files
Download the exercise file from
https://github.com/tertiarycourses/OpenCVTraining
6
IDE for Python
● Pycharm
https://www.jetbrains.com/pycharm/download/
● Jupyter Notebook
http://jupyter.org/
● Sublime Text 3
http://www.sublimetext.com/3
● Atom
https://ide.atom.io/
● Wing 101
https://wingware.com/downloads/wingide-101
● Sypder
https://pythonhosted.org/spyder/installation.htm
l
7
Useful Sublime Text Keys
CMD/CTRL S : Save the changes/file
CMD/CTRL N : Open a new tab
CMD/CTRL Z : Undo the changes
CMD/CTRL B : Build and Run the program
CMD/CTRL / : Comment and uncomment
CMD/CTRL + : Increase the font size
CMD/CTRL - : Decrease the font size
8
Module 1
Get Ready
What is OpenCV
• OpenCV (Open Source
Computer Vision Library) is free
open source software
• Is supports C++, Python and
Java
• It can be run on Windows, Linux,
Mac OS, iOS and Android.
• OpenCV was designed for
computational efficiency and
with a strong focus on real-time
applications.
Install OpenCV on Mac
1. ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/i
nstall)"
2. brew update
3. brew tap homebrew/science
4. brew install opencv3 --with-contrib --with-python3 --HEAD
5. Add Path for openCV to .bash_profil
PATH="/usr/local/opt/opencv3/bin:$PATH"
export PATH
Install OpenCV on Mac
1. Install Anaconda
2. conda install opencv-contrib-python
3. conda install pip install dlib
Ref: http://www.learnopencv.com/install-opencv-3-and-dlib-on-
windows-python-only/
Test OpenCV
import cv2
print(cv2.__version__)
Module 2
Basic Image
Operations
Read Image
img = cv2.imread("./images/detect_blob.png",flag)
flag
1 : Loads color image, ignore alpha
0 : Loads image in grayscale mode
-1: Loads color image with alpha
Display Image
cv2.namedWindow("Image",cv2.WINDOW_NORMAL)
cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = img[:,:,::-1]
plt.imshow(img)
plt.xticks([]), plt.yticks([])
plt.show()
Ex: Display and Save Image
• Download an image from internet
• Display the image using CV2
• Try out flag = 1, 0, -1
• Save the image
Time: 5 mins
Draw a Line
To draw a line, you need to pass starting
and ending coordinates of line:
img = np.zeros([512,512,3],np.uint8)
cv2.line(img,(0,0),(512,512),(255,0,0),5)
Draw a Rectangle
To draw a rectangle, you need top-left
corner and bottom-right corner of
rectangle.
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3
)
Draw a Circle
To draw a circle, you need its center
coordinates and radiu
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3
)
Draw a Polygon
To draw a polygon, first you need
coordinates of vertices
pts = np.array([[10,5],[20,30],[70,20],[50,10]],
np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
Add Text to Image
To put texts in images, you need specify
following things.
• Text
• Position coordinates
• Font type
• Font Scale
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font,
4,(255,255,255),2,cv2.LINE_AA)
Ex: Create Images
Create a 200x200 white and blue images
with numpy
Hint:
white = np.ones([200,200,3],'uint8')
Time: 5 mins
Color Models
There are 3 color models
• RGB
• CMTK
• HSV
Time: 10 mins
Convert to Gray Scale Image
You can convert a color image to gray
scale image using the command below
cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
Add Transparency
You can use merge command to add
transparency to the image
rgba = cv2.merge((b,g,r,g))
Time: 2 mins
Filters
• Images can be filtered with various
low-pass filters (LPF), high-pass
filters(HPF).
• LPF helps in removing noises, blurring
the images etc. HPF filters helps in
finding edges in the images.
• OpenCV provides a function
cv2.filter2D() to convolve a kernel with
an image.
Gaussian Blur Filter
blur = cv2.GaussianBlur(img, (5,55),0)
cv2.imshow("Blur",blur)
Original Blurred
Erosion and Dilation
• Erosion filter- the basic idea of erosion
is just like soil erosion only, it erodes
away the boundaries of foreground
object
• Dilation filter - opposite of erosion.
Here, a pixel element is '1' if atleast one
pixel under the kernel is '1'.
• For noise removal, erosion is followed
by dilation
Dilate Filter
Create a (5x5) filter:
kernel = np.ones((5,5),'uint8')
dilate = cv2.dilate(img,kernel,iterations=1)
iterations – number of times dilation is applied.
Original Dilated
Erode Filter
kernel = np.ones((5,5),'uint8')
erode = cv2.erode(img,kernel,iterations=1)
Original Eroded
Resize
img_half = cv2.resize(img, (0,0), fx=0.1, fy=0.1)
Original Resize
Stretch
Stretch also use resize function
Without interpolation:
img_stretch = cv2.resize(img, (600,600))
With interpolation:
img_stretch_near = cv2.resize(img, (1000,1000),
interpolation=cv2.INTER_NEAREST)
Rotate
M = cv2.getRotationMatrix2D((0,0), -30, 1)
rotated = cv2.warpAffine(img, M, (img.shape[1],
img.shape[0]))
Ex: Resize and Rotate
• Open tomatoes.jpg
• Resize the image to 50% and rotate by
60 degree
Time: 2 mins
Video Capture
To capture a video, you need to create a
VideoCapture object
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
Play Video from File
cap =
cv2.VideoCapture('./images/video.mp4')
Bind Callbacks Function
Mouse callback function:
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
Binary
Segmentation
Segmentation Methods
• Simple Value-based Thresholding
• Adaptive Thresholding
• Histogram-based Methods
• Color Space Transformation
• A-Priori Based Methods
• Machine Learning
Value-Based Thresholding
If pixel value is greater than a threshold
value, it is assigned one value (may be
white), else it is assigned another value
(may be black).
Manual Binary Thresholding
threshold = 85
Time: 15 mins
Adaptive Thresholding
• Adaptive thresholding calculates the
threshold for a small regions of the
image.
• We get different thresholds for
different regions of the same image
and it gives us better results for
images with varying illumination.
• Adaptive thresholding can only apply
to gray scale image
Adaptive Thresholding
thres_adapt = cv2.adaptiveThreshold(img, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 115, 1)
Original Basic Adaptive
Ex: Adaptive Thresholding
• Open opencv-logo.png
• Use adaptive thresholding
Time: 5 mins
Composite Filtering
Composite filtering can
be used to improve the
result if thresholding
segmentation does not
work
Composite Filtering Steps
• Split the image into hsv channels
• Perform thresholding on saturation and hue
channels
• Combine the two thresholded images
Composite Filterining
ret, min_sat = cv2.threshold(s,40,255,
cv2.THRESH_BINARY)
ret, max_hue = cv2.threshold(h,15, 255,
cv2.THRESH_BINARY_INV)
final = cv2.bitwise_and(min_sat,max_hue)
Ex: Composite Filtering
• Open tomatoes.jpg
• Apply composite filtering to segment
out the tomatoes
• Try different threshold for sat and hue
Time: 10 mins
Contouring
• Once you have segmented out the key
areas of an image, the next step is
typically to identify the individual
objects.
• One method of image identification is
through contouring .
Contouring Steps
• Step 1: Thresholding
• Step 2: Find Contours - findContours
function
• Step 3: Draw Contours -
cv2.drawContours function
Find Contours
_, contours, hierarchy = cv2.findContours(thresh,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contouring
Ex: Contouring
• Open opencv-logo.png
• Perform adaptive thresholding
• Find the contours of the elements in
the image
Time: 15 mins
Area and Perimeter
Area and perimeter of a contour can be
computed using the following
commands:
area = cv2.contourArea(c)
perimeter = cv2.arcLength(c, True)
Edge Detection
• Edge detection algorithms look at the
rate or speed at which color changes
across the image.
• Canny Edges is one type of edge
detection algorithm that works quite
well to help create better separation of
objects within the image.
Canny Edge Detection
Canny Edge Detection is a popular edge
detection algorithm. It was developed by
John F. Canny
Canny Edge Detection
edges = cv2.Canny(img, 100, 70). Second
and third arguments are minVal and
maxVal respectively
Module 4
Face and Feature
Detection
Feature Detection
• Features are salient attributes of an
object in an image
• Ideally, features are transformation
invariant
• Features can be used in machine
learning classifier
• Detection Recognition
Template Matching
Template Matching is a method for
searching and finding the location of a
template image in a larger image.
OpenCV comes with a function
cv2.matchTemplate() for this purpose.
Template Matching
cv2.matchTemplate(frame, template,
cv2.TM_CCOEFF_NORMED)
Template
Matching
Haar Features
• Object Detection using
Haar feature-based
cascade classifiers is an
effective object
detection method
• The Haar features for
face detection are
stored at
haarcascade_frontalfac
e_default
Haar Cascade Face Detection
• The Haar cascade use the classifier in a
cascaded manner
• It applies the fastest and most general checks
first in order to quickly rule out region of interest
(ROI) that are definitely not matching a face.
• Then it goes through more classifiers to more
sure that ROI is actually a face.
ROI is not a
Classifier 1 Classifier 1
face
Face Detection
path = "haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(path)
faces = face_cascade.detectMultiScale(gray,
scaleFactor=1.10, minNeighbors=5,
minSize=(40,40))
print(len(faces))
Ex: Eye Detection
• Open faces.jpeg
• Use the eye haar features
"haarcascade_eye.xml" to detect the
eyes
Time: 10 mins
Ex: Haar Cascade Detection
• Open children.jpeg
• Identify the face and eye of the children
Time: 10 mins
Resources
https://docs.opencv.org/master/index.html
Summary
Parting
Message
83
Q&A
Feedback
https://www.tertiarycourses.com.my/cours
e-feedback.html
Ghazaleh Babanejad
ghazaleh.babanejad@gmail.com
01123005257