Introduction to OpenCV
OpenCV – Introduction
} OpenCV stands for the Open Source Computer Vision Library.
} It was founded at Intel in 1999, went through some lean years a@er the .bust but is now u
acAve development, now receiving ongoing support from Willow Garage.
} OpenCV is free for commercial and research use.
} It has a BSD license. The library runs across many plaHorms and acAvely supports Linux,
Windows and Mac OS.
} OpenCV was founded to advance the field of computer vision.
} It gives everyone a reliable, real Ame infrastructure to build on. It collects and makes avai
the most useful algorithms.
2
OpenCV History
● OpenCV is an open library with programmation
fonctions for implementing computer vision tasks:
● Main interface in C++
● Also interfaces in Java, Matlab and Python:
https://opencv-python-tutroals.readthedocs.io
● History :
● 1999 : Original version
by Intel Research
● 2006 : Version 1.0
● 2008 : Support by
Willow Garage
● 2009 : Version 2.0 (OpenCV 2)
● 2012 : Support by OpenCV.org
● 2015 : Version 3.0 (OpenCV 3)
Source: http://fr.slideshare.net/embeddedvision/e04-open-cvbradsk
● 2018 : Version 4.0 (OpenCV 4)
OpenCV - Features
} Cross PlaHorm
} Windows, Linux, Mac OS
} Portable
} iPhone
} Android.
} Language Support
} C/C++
} Python
4
OpenCV-Python
● Why using Python ? :
● Programming simplicity
● Support of library Numpy :
● Optimised for numerical operations
● Similar syntax to Matlab
● Conversion between OpenCV et Numpy data
Slide Courtesy OpenCV Tutorial Gary Bradski
OpenCV Overview: > 500 func)ons
General Image Processing Functions Image Pyramids
Geometric
Descriptors
Segmentation Camera
Calibration,
Stereo, 3D
Features
Transforms Utilities and
Data Structures
Tracking
Machine
Learning: Fitting
• Detection,
• Recognition
Matrix Math
6
OpenCV First Steps - Reading
Now that OpenCV is installed, the first thing to do is to import the module and opening an
image which is fairly simple.
import cv2
# LOAD AN IMAGE USING 'IMREAD’ : Lena is a well know image used by the vision community
img = cv2.imread("Resources/lena.png") # the image will be stored in img
# DISPLAY
cv2.imshow("Lena Soderberg",img) #two arguments the first one is the name of the window and the second the name
of the image we want to show
cv2.waitKey(0) #we add a delay - 0 means infinite delay unAl a key is pressed - an other number will be interpreted in
milliseconds
OpenCV First Steps – Read Video
We can also import a video from a file.
import cv2 #import OpenCV
frameWidth = 640 #To set the image width
frameHeight = 480 #To set the image height
cap = cv2.VideoCapture("Resources/test_ video.mp4") #capture a video in your computer
while True:
success, img = cap.read() #read an image from the video and put it in img – success is a boolean
img = cv2.resize(img, (frameWidth, frameHeight)) #resize images
cv2.imshow("Result", img) #show the image
if cv2.waitKey(1) & 0xFF == ord('q'): #if we go out a certain Ame or if q is pressed break the loop
break
OpenCV First Steps – Read a webcam
Or you can read a video from your webcam
import cv2 #import OpenCV
frameWidth = 640 #To set the image width
frameHeight = 480 #To set the image height
cap = cv2.VideoCapture(0) #capture a video from your default webcam 0, you can use 1 or 2 if you have more cameras
cap.set(3, frameWidth) # set the width defined as the id number 3
cap.set(4, frameHeight) # set the width defined as the id number 4
cap.set(10,100) # set the brightness defined as the id number 10
while True:
success, img = cap.read() #read an image from the video and put it in img – success is a boolean
img = cv2.resize(img, (frameWidth, frameHeight)) #resize images
cv2.imshow("Result", img) #show the image
if cv2.waitKey(1) & 0xFF == ord('q'): #if we go out a certain Ame or if q is pressed break the loop
break
OpenCV First Steps – Read a webcam
Now that OpenCV is installed, the first thing to do is to import the module and opening an
image which is fairly simple.
import cv2
import numpy as np
img = cv2.imread("Resources/lena.png")
kernel = np.ones((5,5),np.uint8)
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray,(7,7),0)
imgCanny = cv2.Canny(img,150,200)
imgDialaAon = cv2.dilate(imgCanny,kernel,iteraAons=1)
imgEroded = cv2.erode(imgDialaAon,kernel,iteraAons=1)
cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
OpenCV First Steps – Basic functions
Here are some basic function you can try --> we will explain, the idea behind later
import cv2
import numpy as np # you should add numpy to your project (as you added OpenCV)
img = cv2.imread("Resources/lena.png")
kernel = np.ones((5,5),np.uint8) #This will creat a 5x5 matrix with ones inside
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #convert your image from BGR into grayscale
imgBlur = cv2.GaussianBlur(imgGray,(7,7),0) #add a gaussian blur to your image with variance sigam =0
imgCanny = cv2.Canny(img,150,200) #extract the contours of your image
imgDialaAon = cv2.dilate(imgCanny,kernel,iteraAons=1) #this one possible operaAon on your image
imgEroded = cv2.erode(imgDialaAon,kernel,iteraAons=1) #this is an other psossible operaAon on your image
cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
cv2.imshow("DialaAon Image",imgDialaAon)
cv2.imshow("Eroded Image",imgEroded)
cv2.waitKey(0)
# You can have a look on the difference between the different images
OpenCV First Steps – Resizing and Cropping
Have a look on this and explain it !!!!!!!!!
import cv2
import numpy as np
img = cv2.imread("Resources/lambo.png")
print(img.shape)
imgResize = cv2.resize(img,(1000,500))
print(imgResize.shape)
imgCropped = img[0:200,200:500]
cv2.imshow("Image",img) OpenCV Frame convenAons
cv2.imshow("ImageResize",imgResize)
#cv2.imshow("Image Resize",imgResize)
cv2.imshow("Image Cropped",imgCropped)
cv2.waitKey(0)
OpenCV First Steps – Shapes and Texts
Have a look on this and explain it !!!!!!!!!
mport cv2
import numpy as np
img = np.zeros((512,512,3),np.uint8) #we create here a matrix with thre dimensions
#print(img)
img[:]= 255,0,0 #we can change the values between 0 and 255 to see what happens
cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(0,255,0),3)
cv2.rectangle(img,(0,0),(250,350),(0,0,255),cv2.FILLED)
cv2.circle(img,(400,50),30,(255,255,0),5)
cv2.putText(img," OPENCV ",(300,200),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,150,0),3)
cv2.imshow("Image",img)
cv2.waitKey(0)