Anda di halaman 1dari 85

Python OpenCV Computer

Vision Training

Trainer: Dr. Ghazaleh Babanejad

Website:www.tertiarycourses.com.my
Email: enquiry@tertiaryinfotech.com
About the Trainer

Dr Ghazaleh Babanejad has received Phd from University Putra


Malaysia in Faculty of Computer Science and Information
Technology..She is working on recommender systems in the field
of skyline queries over Dynamic and Incomplete databases for
her PhD thesis. She is also working on Data Science field as a
trainer and Data Scientist. She worked on Machine Learning and
Process Mining projects. She also has several international
certificates in Practical Machine Learning (John Hopkins
University) Mining Massive Datasets (Stanford University),
Process Mining (Eindhoven University), Hadoop (University of
San Diego), MongoDB for DBAs (MongoDB Inc) and some
other certificates. She has more than 5 year experience as a
lecturer and data base administrator.
Agenda
Module 1 Get Ready

- What is OpenCV
- Install OpenCV
- Test OpenCV

Module 2: Basic Image Operations


- Read Image
- Display Image
- Output Image
- BGR and HSV Channels
- Filters
- Resize, Stretch and Rotate
- Video Capture
Agenda
Module 3: Object Detection
- What is Object Detection
- Image Segmentation
- Thresholding
- Composite Filtering
- Contouring
- Edge Detection

Module 4 Face and Feature Detection


- What is Feature Detection
- Template Matching
- Haar Cascade Face Detection
Prerequisites
• Basic Python programming
knowledge

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()

namedWindow: Creates a window


imshow: Displays an image in the specified window
waitKey: Waits for a pressed key
destroyAllWindows: Destroy all windows
Output Image
cv2.imwrite("output.jpg",img)
Image Attributes
img.shape
img[10, 5]
BGR vs RGB
OpenCV use BGR. Matplotlib use
RGB
To show image on Matplotlib, need
to flip BGR to RGB

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

• RGB and CMTK color models are


mainly for print
• HSV (hue, saturation, and value) is
similar to how humans perceive color
Split Into BGR Channels
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]

Alternatively, you can use split function in cv2


b,g,r = cv2.split(img)
HSV Color Model
• Hue is the color portion of the color
model, and is expressed as a number
from 0 to 360 degrees
• Saturation is the amount of gray in the
color, from 0 to 100 percent. A faded
effect
• Value describes the brightness or
intensity of the color, from 0-100
percent, where 0 is completely black
and 100 is the brightest and reveals the
most color
Split Into HSV Channels
• Convert color from BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
• Split to color channels
h,s,v = cv2.split(hsv)
Hue Saturation Value
Ex: BGR and HSV Channels
• Open tomatoes.jpg
• Split the image to BGR and HSV
channels

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))

Note that jpg does not support


transparency. So save as png file to see
the transparency effect.
Ex: Transparency
• Open tomatoes.jpg
• Add transparency to the image.

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)

Create a black image, a window and bind the


function to window:
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
Callback Event
def click(event, x, y, flags, param):
global point, pressed
if event == cv2.EVENT_LBUTTONDOWN:
print("Pressed",x,y)
point = (x,y)
Module 3
Object Detection
Object Detection
Object detection is to detect instances of
semantic objects and classify them to a
certain class
Object Detection Methods
● Image Segmentation
● Contouring
● Machine Learning
Image Segmentation
Image segmentation is the process of
partitioning a digital image into various
segments Eg 2 segments below

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

for row in range(0,height):


for col in range(0, width):
if img[row][col]>threshold:
binary[row][col]=255
CV2 Thresholding
Manual thresholding is slow. CV2 provides more
efficient thresholding algorithm eg
cv2.threshold(img,threshold,255,cv2.THRESH_BINARY)

• First arg is the source grayscale image


• Second arg is the threshold value
• Third arg is the maxVal
• Fourth arg decide the method
Other CV2 Thresholding
cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
cv.threshold(img,127,255,cv.THRESH_TRUNC)
cv.threshold(img,127,255,cv.THRESH_TOZERO)
cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
Ex: CV2 Thresholding
• Open opencv-logo.png
• Apply and compare BINARY,
BINARY_INV, TRUNC, TOZERO,
TOZERO_INV thresholding methods
• Apply and compare different threshold
of 80, 125 and 150

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.

Classifier 1 Classifier 1 ROI is 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

Anda mungkin juga menyukai