@DylanSeychell
Introduction to
Computer Vision
using OpenCV
Dylan Seychell
I am Dylan Seychell
Academic and Software Engineer
AI, UX and Computer Vision
@DylanSeychell
Hello!
2
@DylanSeychell
Presentation Overview
What is Computer Vision?
What is OpenCV?
Workshop:
Image Acquisition
Image Processing
Image Analysis/Understanding
3
Computer Vision
Making computers get a high-level
understanding from images and videos.
4
@DylanSeychell
Stages of Computer Vision
5
Acquisition UnderstandingProcessing
Covered in this session
@DylanSeychell
OpenCV - enabling computer vision
Open Source Computer Vision library
Cross-platform
Free for use under open source BSD license
Can be easily used with Java, Python, C and C++
Supports Machine Learning libraries such as
TensorFlow and Caffe.
https://opencv.org
6
@DylanSeychell
This Session:
We’ll be using OpenCV with Python
New to Python? Check these slides
https://www.slideshare.net/dylsey/introduction-to-python-80851217
7
@DylanSeychell
CodeLab Part 1: Acquisition of Image Data
8
@DylanSeychell
Test the library:
In terminal/CMD type python
>>> import cv2
>>>
9
@DylanSeychell
Importing an image
Create a Python module and write the following code:
import cv2
img = cv2.imread('duomo.jpg',1)
cv2.imshow("Output Window", img)
cv2.waitKey()
This code imports an image and outputs it to a window and waits for any user
keyboard input to terminate.
10
@DylanSeychell
cv2.imread() function
This function is used to load an image and store it into a variable
img = cv2.imread('duomo.jpg',1)
This function accepts 2 parameters:
1. The filename of the image
2. Colour Approach:
a. 1: Colour, neglecting transparency
b. 0: Greyscale
c. -1: Colour together with the alpha channel
11
@DylanSeychell
Different output for different imread() arguments
12
img = cv2.imread('duomo.jpg',1) img = cv2.imread('duomo.jpg',0)
@DylanSeychell
cv2.imshow() function
This function is used to display an image in a window.
cv2.imshow("Output Window", img)
This function accepts 2 parameters:
1. The name of the output window
2. The image to be displayed in the output window
NB 1: The window automatically fits the image size.
NB 2: Matplotlib can be used as an alternative
13
@DylanSeychell
cv2.waitKey() function
This is a keyboard binding function
cv2.waitKey()
A single argument value in milliseconds:
1. 0 or no argument: wait indefinitely for keyboard interrupt
2. Any other value: display the window for the duration of that value in ms
This function returns the ASCII value of the key pressed and if stored in a
variable, it can be used to perform subsequent logical operations.
14
@DylanSeychell
Using the webcam feed
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
15
@DylanSeychell
cv2.VideoCapture() Object
The video capture object allows us to manipulate captured frames from a
camera.
cap = cv2.VideoCapture(0)
The argument is either the video filename or camera index, 0 for webcam.
Allows the handling of each frame.
After being used, the capture has to be released:
cap.release()
16
@DylanSeychell
Importing a video
cap = cv2.VideoCapture('vtest.avi')
while(cap.isOpened()): #returns true when there is another frame to process
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
17
@DylanSeychell
CodeLab Part 2: Image Processing
18
@DylanSeychell
Create a new module and initialise it
import cv2
img = cv2.imread('duomo.jpg',0)
##Our Image Processing code goes here
cv2.imshow("Output Window", img)
cv2.waitKey()
19
@DylanSeychell
Image Type
Try printing these values:
print (type(img))
This will return <type 'numpy.ndarray'>
Therefore, we’d deal with a numpy array
20
@DylanSeychell
Image Shape
Try printing these values:
img = cv2.imread('duomo.jpg',0)
print (type(img))
print (img)
[[22 22 22 ..., 23 23 24]
[22 22 22 ..., 23 23 23]
[22 22 22 ..., 23 23 23]
...,
[13 13 13 ..., 5 6 6]
[13 13 13 ..., 11 11 10]
[13 13 13 ..., 12 12 10]]
21
Greyscale
@DylanSeychell
Try the same thing with a coloured image
22
@DylanSeychell
Slicing Image Channels (Colours)
Load a coloured image and set unwanted channels to zero
img = cv2.imread("duomo.jpg", 1)
img[:,:,2] = 0 #red
img[:,:,1] = 0 #green
img[:,:,0] #blue
cv2.imshow("Output", img) #returns the blue channel
23
@DylanSeychell
Slicing by colour channel.
24
img[:,:,2] = 0 #red
img[:,:,1] = 0 #green
img[:,:,0] #blue
img[:,:,2] #red
img[:,:,1] = 0 #green
img[:,:,0] = 0 #blue
img[:,:,2] = 0 #red
img[:,:,1] #green
img[:,:,0] = 0 #blue
@DylanSeychell
Blurring images in OpenCV
The blur function using average values
blur = cv2.blur(img,(5,5))
This method accepts 2 arguments:
1. The source image
2. A tuple with the size of the box filter
25
@DylanSeychell
Simple blurring using OpenCV.
26
blur = cv2.blur(img,(10,10)) blur = cv2.blur(img,(5,5))
@DylanSeychell
Detecting Edges
Using Canny edge detection:
● Removes the noise using a Gaussian Filter
● Finds intensity gradient of the image
● Non-maximum suppression (remove unwanted pixels)
● Hysteresis Thresholding (difference between min and max values)
27
@DylanSeychell
Canny Edge Detection in OpenCV
edges = cv2.Canny(img,100,200)
This method accepts 3 arguments:
1. The source image
2. Min value
3. Max value
28
@DylanSeychell
Different minVal and maxVal values
29
edges = cv2.Canny(img,50,60) edges = cv2.Canny(img,150,300)
@DylanSeychell
Choosing a region of interest
An inbuilt function to select a region of interest:
fromCenter = False
r = cv2.selectROI(img, fromCenter)
Arguments:
1. The source image
2. Flag to choose the origin of the bounding box
30
@DylanSeychell
Using the resultant RoI
Save the resultant RoI into another image
r = cv2.selectROI(img, fromCenter)
imCropT = img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
Cropping the image using Numpy array slicing in the form:
crop= img[yoffset:-yoffset, xoffset:-xoffset]
31
@DylanSeychell
Selecting a RoI and displaying it
32
r = cv2.selectROI(img, fromCenter)
imCropT = img[int(r[1]):int(r[1]+r[3]),
int(r[0]):int(r[0]+r[2])]
cv2.imshow("Cropped", imCropT)
@DylanSeychell
Part 3: Analysis
This is a specialised field also known as Artificial
Vision. More resources related to this field will follow.
33
@DylanSeychell
Merging Computer Vision and AI.
34
Image to TextObject Detection & Classification
@DylanSeychell
Thank you!
35

Introduction to Computer Vision using OpenCV

  • 1.
  • 2.
    I am DylanSeychell Academic and Software Engineer AI, UX and Computer Vision @DylanSeychell Hello! 2
  • 3.
    @DylanSeychell Presentation Overview What isComputer Vision? What is OpenCV? Workshop: Image Acquisition Image Processing Image Analysis/Understanding 3
  • 4.
    Computer Vision Making computersget a high-level understanding from images and videos. 4
  • 5.
    @DylanSeychell Stages of ComputerVision 5 Acquisition UnderstandingProcessing Covered in this session
  • 6.
    @DylanSeychell OpenCV - enablingcomputer vision Open Source Computer Vision library Cross-platform Free for use under open source BSD license Can be easily used with Java, Python, C and C++ Supports Machine Learning libraries such as TensorFlow and Caffe. https://opencv.org 6
  • 7.
    @DylanSeychell This Session: We’ll beusing OpenCV with Python New to Python? Check these slides https://www.slideshare.net/dylsey/introduction-to-python-80851217 7
  • 8.
    @DylanSeychell CodeLab Part 1:Acquisition of Image Data 8
  • 9.
    @DylanSeychell Test the library: Interminal/CMD type python >>> import cv2 >>> 9
  • 10.
    @DylanSeychell Importing an image Createa Python module and write the following code: import cv2 img = cv2.imread('duomo.jpg',1) cv2.imshow("Output Window", img) cv2.waitKey() This code imports an image and outputs it to a window and waits for any user keyboard input to terminate. 10
  • 11.
    @DylanSeychell cv2.imread() function This functionis used to load an image and store it into a variable img = cv2.imread('duomo.jpg',1) This function accepts 2 parameters: 1. The filename of the image 2. Colour Approach: a. 1: Colour, neglecting transparency b. 0: Greyscale c. -1: Colour together with the alpha channel 11
  • 12.
    @DylanSeychell Different output fordifferent imread() arguments 12 img = cv2.imread('duomo.jpg',1) img = cv2.imread('duomo.jpg',0)
  • 13.
    @DylanSeychell cv2.imshow() function This functionis used to display an image in a window. cv2.imshow("Output Window", img) This function accepts 2 parameters: 1. The name of the output window 2. The image to be displayed in the output window NB 1: The window automatically fits the image size. NB 2: Matplotlib can be used as an alternative 13
  • 14.
    @DylanSeychell cv2.waitKey() function This isa keyboard binding function cv2.waitKey() A single argument value in milliseconds: 1. 0 or no argument: wait indefinitely for keyboard interrupt 2. Any other value: display the window for the duration of that value in ms This function returns the ASCII value of the key pressed and if stored in a variable, it can be used to perform subsequent logical operations. 14
  • 15.
    @DylanSeychell Using the webcamfeed cap = cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() 15
  • 16.
    @DylanSeychell cv2.VideoCapture() Object The videocapture object allows us to manipulate captured frames from a camera. cap = cv2.VideoCapture(0) The argument is either the video filename or camera index, 0 for webcam. Allows the handling of each frame. After being used, the capture has to be released: cap.release() 16
  • 17.
    @DylanSeychell Importing a video cap= cv2.VideoCapture('vtest.avi') while(cap.isOpened()): #returns true when there is another frame to process ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() 17
  • 18.
    @DylanSeychell CodeLab Part 2:Image Processing 18
  • 19.
    @DylanSeychell Create a newmodule and initialise it import cv2 img = cv2.imread('duomo.jpg',0) ##Our Image Processing code goes here cv2.imshow("Output Window", img) cv2.waitKey() 19
  • 20.
    @DylanSeychell Image Type Try printingthese values: print (type(img)) This will return <type 'numpy.ndarray'> Therefore, we’d deal with a numpy array 20
  • 21.
    @DylanSeychell Image Shape Try printingthese values: img = cv2.imread('duomo.jpg',0) print (type(img)) print (img) [[22 22 22 ..., 23 23 24] [22 22 22 ..., 23 23 23] [22 22 22 ..., 23 23 23] ..., [13 13 13 ..., 5 6 6] [13 13 13 ..., 11 11 10] [13 13 13 ..., 12 12 10]] 21 Greyscale
  • 22.
    @DylanSeychell Try the samething with a coloured image 22
  • 23.
    @DylanSeychell Slicing Image Channels(Colours) Load a coloured image and set unwanted channels to zero img = cv2.imread("duomo.jpg", 1) img[:,:,2] = 0 #red img[:,:,1] = 0 #green img[:,:,0] #blue cv2.imshow("Output", img) #returns the blue channel 23
  • 24.
    @DylanSeychell Slicing by colourchannel. 24 img[:,:,2] = 0 #red img[:,:,1] = 0 #green img[:,:,0] #blue img[:,:,2] #red img[:,:,1] = 0 #green img[:,:,0] = 0 #blue img[:,:,2] = 0 #red img[:,:,1] #green img[:,:,0] = 0 #blue
  • 25.
    @DylanSeychell Blurring images inOpenCV The blur function using average values blur = cv2.blur(img,(5,5)) This method accepts 2 arguments: 1. The source image 2. A tuple with the size of the box filter 25
  • 26.
    @DylanSeychell Simple blurring usingOpenCV. 26 blur = cv2.blur(img,(10,10)) blur = cv2.blur(img,(5,5))
  • 27.
    @DylanSeychell Detecting Edges Using Cannyedge detection: ● Removes the noise using a Gaussian Filter ● Finds intensity gradient of the image ● Non-maximum suppression (remove unwanted pixels) ● Hysteresis Thresholding (difference between min and max values) 27
  • 28.
    @DylanSeychell Canny Edge Detectionin OpenCV edges = cv2.Canny(img,100,200) This method accepts 3 arguments: 1. The source image 2. Min value 3. Max value 28
  • 29.
    @DylanSeychell Different minVal andmaxVal values 29 edges = cv2.Canny(img,50,60) edges = cv2.Canny(img,150,300)
  • 30.
    @DylanSeychell Choosing a regionof interest An inbuilt function to select a region of interest: fromCenter = False r = cv2.selectROI(img, fromCenter) Arguments: 1. The source image 2. Flag to choose the origin of the bounding box 30
  • 31.
    @DylanSeychell Using the resultantRoI Save the resultant RoI into another image r = cv2.selectROI(img, fromCenter) imCropT = img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])] Cropping the image using Numpy array slicing in the form: crop= img[yoffset:-yoffset, xoffset:-xoffset] 31
  • 32.
    @DylanSeychell Selecting a RoIand displaying it 32 r = cv2.selectROI(img, fromCenter) imCropT = img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])] cv2.imshow("Cropped", imCropT)
  • 33.
    @DylanSeychell Part 3: Analysis Thisis a specialised field also known as Artificial Vision. More resources related to this field will follow. 33
  • 34.
    @DylanSeychell Merging Computer Visionand AI. 34 Image to TextObject Detection & Classification
  • 35.