This is an introductory deck to computer vision using OpenCV and Python, through examples. This presentation is a step by step codelab through the basic functions of OpenCV.
6. @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
7. @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
10. @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
11. @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
13. @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
14. @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
15. @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
16. @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
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
19. @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
20. @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
25. @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
27. @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
28. @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
30. @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
31. @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
32. @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)
33. @DylanSeychell
Part 3: Analysis
This is a specialised field also known as Artificial
Vision. More resources related to this field will follow.
33