Section 1: Introduction
to OpenCV
Open CV Beginner Level
Computer Vision Class Series
Welcome to Open CV
● pip install opencv-python
● import cv2
● You have officially stepped into
the world of computer vision
● Now How to proceed ?
● Lets see…………. Lets See how
computers Sees.
Basic Operations in
Computer Vision
● Reading an image
● Displaying an image
● Image window controls
● Reading images in grayscale
● Saving images
● Image shape and size
● Accessing pixel values
● Modifying pixel values
● Cropping images
● Resizing images
● Drawing lines
● Drawing rectangles
● Drawing circles
● Drawing polygons
● Adding text to images
Basic Operations in
Section
Computer Vision
● RGB vs BGR
● Converting color spaces
● Grayscale conversion
● HSV conversion
● Thresholding basics
● What is image filtering?
● Gaussian blur
● Median blur
● Bilateral filter
● Sharpening filters
● Edge detection with Canny
● Dilation
● Erosion
● Opening and Closing
Basic Operations in
Section
Computer Vision
● Rotation
● Translation
● Perspective transform
● OpenCV with deep learning, video
processing
Computer Vision Read/Display
⇒
# 1. Read and Display an Image
import cv2
img = cv2.imread('image.jpg') # Load image
cv2.imshow('Original Image', img) # Show image
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Convert to Grayscale
⇒
# 2. Convert to Grayscale
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Resize Image
⇒
# 3. Resize Image
import cv2
img = cv2.imread('image.jpg')
resized = cv2.resize(img, (300, 300))
cv2.imshow('Resized Image', resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Crop Image
⇒
# 4. Crop Image
import cv2
img = cv2.imread('image.jpg')
cropped = img[50:200, 100:300]
cv2.imshow('Cropped Image', cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Draw a Line
⇒
# 5. Draw a Line
import cv2
img = cv2.imread('image.jpg')
cv2.line(img, (0, 0), (150, 150), (255, 0, 0), 5)
cv2.imshow('Line', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Draw a Rectangle
⇒
# 6. Draw a Rectangle
import cv2
img = cv2.imread('image.jpg')
cv2.rectangle(img, (50, 50), (200, 200), (0, 255, 0), 3)
cv2.imshow('Rectangle', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Draw a Circle
⇒
# 7. Draw a Circle
import cv2
img = cv2.imread('image.jpg')
cv2.circle(img, (150, 150), 50, (0, 0, 255), -1)
cv2.imshow('Circle', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Add Text
⇒
# 8. Add Text
import cv2
img = cv2.imread('image.jpg')
cv2.putText(img, 'OpenCV', (10, 300),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)
cv2.imshow('Text', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Convert to HSV
⇒
# 8. Add Text
import cv2
img = cv2.imread('image.jpg')
cv2.putText(img, 'OpenCV', (10, 300),
cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)
cv2.imshow('Text', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Thresholding
⇒
# 10. Thresholding
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255,
cv2.THRESH_BINARY)
cv2.imshow('Thresholded Image', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Gaussian Blur
⇒
# 11. Gaussian Blur
import cv2
img = cv2.imread('image.jpg')
blur = cv2.GaussianBlur(img, (5, 5), 0)
cv2.imshow('Gaussian Blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Median blur
⇒
# 12. Median Blur
import cv2
img = cv2.imread('image.jpg')
blur = cv2.medianBlur(img, 5)
cv2.imshow('Median Blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Bilateral Filter
⇒
# 13. Bilateral Filter
import cv2
img = cv2.imread('image.jpg')
blur = cv2.bilateralFilter(img, 9, 75, 75)
cv2.imshow('Bilateral Filter', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Sharpening Filter
⇒
# 14. Sharpening Filter
import cv2
import numpy as np
img = cv2.imread('image.jpg')
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(img, -1, kernel)
cv2.imshow('Sharpened Image', sharpened)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Canny Edge Detection
⇒
# 15. Canny Edge Detection
import cv2
img = cv2.imread('image.jpg')
edges = cv2.Canny(img, 100, 200)
cv2.imshow('Canny Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Dilation
⇒
# 16. Dilation
import cv2
import numpy as np
img = cv2.imread('image.jpg')
edges = cv2.Canny(img, 100, 200)
kernel = np.ones((5, 5), np.uint8)
dilated = cv2.dilate(edges, kernel, iterations=1)
cv2.imshow('Dilated Edges', dilated)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Erosion
⇒
# 17. Erosion
import cv2
import numpy as np
img = cv2.imread('image.jpg')
edges = cv2.Canny(img, 100, 200)
kernel = np.ones((5, 5), np.uint8)
eroded = cv2.erode(edges, kernel, iterations=1)
cv2.imshow('Eroded Edges', eroded)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Opening
⇒
# 18. Opening
import cv2
import numpy as np
img = cv2.imread('image.jpg')
kernel = np.ones((5, 5), np.uint8)
opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('Opening', opened)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Closing
⇒
# 19. Closing
import cv2
import numpy as np
img = cv2.imread('image.jpg')
kernel = np.ones((5, 5), np.uint8)
closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE,
kernel)
cv2.imshow('Closing', closed)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Contour Detection
⇒
# 20. Contour Detection
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255,
cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Rotation
⇒
# 21. Rotation
import cv2
img = cv2.imread('image.jpg')
(h, w) = img.shape[:2]
M = cv2.getRotationMatrix2D((w/2, h/2), 45, 1)
rotated = cv2.warpAffine(img, M, (w, h))
cv2.imshow('Rotated Image', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Translation
⇒
# 22. Translation
import cv2
import numpy as np
img = cv2.imread('image.jpg')
M = np.float32([[1, 0, 100], [0, 1, 50]])
translated = cv2.warpAffine(img, M, (img.shape[1],
img.shape[0]))
cv2.imshow('Translated Image', translated)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Perspective
Transform
⇒
# 23. Perspective Transform
import cv2
import numpy as np
img = cv2.imread('image.jpg')
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (300, 300))
cv2.imshow('Perspective Transform', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Affine Transform
⇒
# 24. Affine Transform
import cv2
import numpy as np
img = cv2.imread('image.jpg')
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
cv2.imshow('Affine Transform', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Resize with
Interpolation
⇒
# 25. Resize with Interpolation
import cv2
img = cv2.imread('image.jpg')
resized = cv2.resize(img, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_AREA)
cv2.imshow('Interpolated Resize', resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Face Detection
⇒
# 26. Face Detection
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(‘.xml’)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision Video Capture
⇒
# 27. Video Capture
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Computer Vision Save Video Frame
⇒
# 28. Save Video Frame
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imwrite('frame.jpg', frame)
cap.release()
Computer Vision Drawing polygon
⇒
# 29. Draw Polygon
import cv2
import numpy as np
img = cv2.imread('image.jpg')
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
cv2.polylines(img, [pts], True, (255,255,0), 2)
cv2.imshow('Polygon', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Computer Vision cvlib
⇒
# Drawing bounding boxes using cvlib
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
# Load image
img = cv2.imread('image.jpg')
# Perform object detection
bbox, label, conf = cv.detect_common_objects(img)
# Draw bounding boxes
output = draw_bbox(img, bbox, label, conf)
# Display result
cv2.imshow("Object Detection", output)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
Custom Labelling using
# Load image CVlib
img = cv2.imread('image.jpg') Program
# Detect objects
bbox, label, conf = cv.detect_common_objects(img)
# Replace all labels with a custom one
custom_label = "Detected"
label = [custom_label for _ in label]
# Draw bounding boxes with custom labels
output = draw_bbox(img, bbox, label, conf)
cv2.imshow("Custom Label Detection", output)
cv2.waitKey(0)
cv2.destroyAllWindows()
Over To Machine Learning
Welcome Open CV to Machine Learning
Welcome my dear friend Open CV to the world
of Machine Learning and artificial intelligence,