You are on page 1of 35

Introduction to

Computer Vision
using OpenCV
Dylan Seychell

@DylanSeychell
Hello!
I am Dylan Seychell
Academic and Software Engineer
AI, UX and Computer Vision
@DylanSeychell

2
Presentation Overview
What is Computer Vision?

What is OpenCV?

Workshop:

Image Acquisition

Image Processing

Image Analysis/Understanding

3
@DylanSeychell
Computer Vision
Making computers get a high-level
understanding from images and videos.

4
Stages of Computer Vision

Acquisition Processing Understanding

Covered in this session

5
@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
img = cv2.imread('duomo.jpg',1) img = cv2.imread('duomo.jpg',0)

Different output for different imread() arguments


12
@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]
Greyscale
[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
@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
img[:,:,2] = 0 #red img[:,:,2] #red img[:,:,2] = 0 #red
img[:,:,1] = 0 #green img[:,:,1] = 0 #green img[:,:,1] #green
img[:,:,0] #blue img[:,:,0] = 0 #blue img[:,:,0] = 0 #blue

Slicing by colour channel.


24
@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
blur = cv2.blur(img,(10,10)) blur = cv2.blur(img,(5,5))

Simple blurring using OpenCV.


26
@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
edges = cv2.Canny(img,50,60) edges = cv2.Canny(img,150,300)

Different minVal and maxVal values


29
@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
r = cv2.selectROI(img, fromCenter)
cv2.imshow("Cropped", imCropT)
imCropT = img[int(r[1]):int(r[1]+r[3]),
int(r[0]):int(r[0]+r[2])]

Selecting a RoI and displaying it


32
@DylanSeychell
Part 3: Analysis

This is a specialised field also known as Artificial


Vision. More resources related to this field will follow.

33
@DylanSeychell
Object Detection & Classification Image to Text

Merging Computer Vision and AI.


34
@DylanSeychell
Thank you!

35
@DylanSeychell

You might also like