Developing Image Processing and Computer
Vision applications
Artificial Intelligence
Dr. Usman Ghani
INDEX
▰ Introduction to OpenCV
▰ Image Basics in OpenCV
▰ Basic functions of OpenCV
▰ Constructing Basic Shapes in OpenCV
▰ Image filtering
▰ Morphological transformations
▰ Processing Images with OpenCV
▰ Working with videos
2
Introduction to
OpenCV
3
How does computer vision works?
4
Applications of OpenCV
Human Pose Detection Detection and Tracking
• e.g. Tracking Cars or Objects
• e.g. AR clothes fitting
Medical Imaging Camera Calibration
• e.g. camera calibrations toolkits
• e.g. AI medical diagnosis
Manufacturing Production
Autonomous vehicles Line
• e.g. Self Driving Cars • Labeling and tracing products
5
Computer Vision Toolkits
Endrov
VTK VXL
PIL BootCV
Computer
PCL Vision VLFeat
toolkits
6
Introduction to the OpenCV library
▰ Open source Computer Vision Library
▰ Officially launched in 1999 at Intel Research
▰ Available for C, C++, and Python
▰ free for both academic and commercial
▰ Cross Platform
▻ Supports Windows, Linux, Mac OS , iOS, Android
▰ http://opencv.org/
7
Installing OpenCV, Python, and other packages
▰ Installation of required ▰ Installation Commands:
libraries: pip install opencv-python
Install opencv-python pip install matplotlib
Install matplotlib pip install scikit-image
Install scikit-image pip install numpy
Install numpy pip install pandas
Install pandas
8
Image Basics in
OpenCV
9
Concepts of pixels, colors, channels, images,
and color spaces
Pixels Colors Images Color
channels
spaces
10
Accessing and manipulating pixels in OpenCV
Access pixel values
Access only blue pixel
Modify the pixel values
11
Get image shape
• type of image data; number of pixels; etc.
• print( img.shape )
12
Basic functions of
OpenCV
13
Read an Image
Syntax: cv2.imread(image, flag)
14
https://miro.medium.com/max/1400/1*IHHLvhkhx2xHloOb3-wXwA.jpeg
Display an image
Syntax: cv2.imshow(window_name, image)
Output
15
Save image to disk
Syntax: cv2.imwrite(filename, image)
Image saved in drive
16
Access Image properties
Syntax:
• img.shape
• img.dtype
• img.size
17
Change Color Spaces
RGB → Gray
cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
18
Resize image
Syntax:cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation ]]]])
Original Image Resized Image
19
Constructing Basic
Shapes in OpenCV
20
Draw a rectangle on image
Syntax: cv2.rectangle(image, p0, p1, color,
thickness)
Output
21
Draw a circle on image
Syntax: cv2.circle(image,
center_coordinates, radius, color,
thickness)
Output
22
Draw a line on image
Syntax: cv2. line(image, p0, p1, color,
thickness)
Output
23
Draw an ellipse on image
Syntax: cv2.ellipse(img, center, axes, angle,
a0, a1, color, thickness)
Output
24
Put text on image
Syntax: cv2.putText(image, text, org, font,
fontScale, color[, thickness[, lineType[,
bottomLeftOrigin]]])
Output
25
Image filtering
26
Sharpen the image
Syntax: cv2.filter2D(src, depth, k_sharped,
kernel)
Output
27
Blur an image
Syntax: cv2.blur(image, ksize)
Output
28
Apply Gaussian Filter on an image
Syntax: cv2.GaussianBlur(src, ksize, sigmaX[,
dst[, sigmaY[, borderType =
BORDER_DEFAULT]]] )
Output
29
Apply Median Filter on an image
Syntax: cv2.medianBlur(src, ksize)
Output
30
Apply an identity Filter on an image
k_filter = np.array([[1,0,0], [1,1,0], [0,0,1]])
Syntax: cv2.filter2D(img, -1, k_filter)
Output
31
Processing Image
Operations with OpenCV
32
Perform Thresholding on an image
Syntax: cv2.threshold(image, thresh, maxval,
type, dst)
Output
33
Detect Edges of objects in an image
Syntax: cv2.Canny('/path/to/img', minVal,
maxVal, apertureSize, L2gradient)
Output
34
Detect Contours in an image
#find Contours
cv2.FindContours(image,mode=
CV_RETR_LIST,method=CV_CHAIN_APPROX_
SIMPLE)
#draw contours
cv2.DrawContours(img, contours,
contourIdx, colour, thickness)
Output
35
Setting Region of Image
Syntax: cv2.selectROI(windowName, img,
showCrosshair, fromCenter)
Output
36
Blending Images
Blend Pictures
Syntax: cv2.addWeighted(img1, alpha, img2, beta, gamma)
Pic 1 Pic 2
37
Crop an image
Syntax: crop_img = img[y:y+h, x:x+w]
Original Image Cropped Image
38
Splitting and merging images
Syntax: Merged an image
• cv2.split()
• cv2.merge()
Split an image
39
Draw Histogram of an image
Syntax: cv2.calcHist(images, channels, mask,
histSize, ranges)
Original Grayscale Image
40
Apply Log Transformation on an image
# Apply log transform.
c = 255/(np.log(1 + np.max(img)))
log_transformed = c * np.log(1 + img)
abc= np.array(log_transformed, dtype = np.uint8)
Input Image Apply Log Transformation
41
Apply Power Law Transformation on an image
Original Image Gamma = 2.2 Gamma = 0.1
Pow_law_trans = np.array(255*(img / 255) ** gamma, dtype = 'uint8')
42
Morphological
Transformations
43
Dilation Operation
Syntax: cv2. dilate(img, kernel, iterations=1)
Input Image Dilation of Image
44
Erosion Operation
Syntax: cv2.erode(img, kernel, iterations=1)
Input Image Erosion of Image
45
Opening Operation
Syntax: cv2.morphologyEx(img,
cv2.MORPH_OPEN, kernel)
Input Image Opening operation on Image
46
Closing Operation
Syntax: cv2.morphologyEx(img,
cv2.MORPH_CLOSE, kernel)
Input Image Closing operation on Image
47
Top Hat Operation
Syntax: cv2.morphologyEx(img,
cv2.MORPH_TOPHAT, kernel)
Input Image Top Hat Operation
48
Black Hat Operation
Syntax: cv2.morphologyEx(img,
cv2.MORPH_BLACKHAT, kernel)
Input Image Black Hat Operation
49
Morphological gradient operation
Syntax: cv2.morphologyEx(img,
cv2.MORPH_GRADIENT, kernel))
Input Image Morphological gradient operation
50
Perform Negative of an Image
# Subtract the img from max value
(calculated from dtype)
img_neg = 255 - img
Input Image Perform Negative Operation
51
THANKS!
Prof Dr. Muhammad Usman Ghani Khan
Director
NCAI UET Lahore
usman.ghani@kics.edu.pk
52