OpenCV ¶
OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel). OpenCV currently supports wide variety of languages like, C++, Python, Java, etc.
Installation ¶
Please refer to these articles for installation of OpenCV on your computer.
- Windows Installation Instructions: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows
- Mac Installation Instructions (High Sierra): https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a
- Linux Installation Instructions (Ubuntu 18.04): https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv
Here we will be focusing on Python implementation of OpenCV ¶
1# Reading image in OpenCV
2import cv2
3img = cv2.imread('cat.jpg')
4
5# Displaying the image
6# imshow() function is used to display the image
7cv2.imshow('Image', img)
8# Your first argument is the title of the window and second parameter is image
9# If you are getting an error, Object Type None, your image path may be wrong. Please recheck the path to the image
10cv2.waitKey(0)
11# waitKey() is a keyboard binding function and takes an argument in milliseconds. For GUI events you MUST use waitKey() function.
12
13# Writing an image
14cv2.imwrite('catgray.png', img)
15# The first argument is the file name and second is the image
16
17# Convert image to grayscale
18gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19
20# Capturing Video from Webcam
21cap = cv2.VideoCapture(0)
22# 0 is your camera, if you have multiple cameras, you need to enter their id
23while True:
24 # Capturing frame-by-frame
25 _, frame = cap.read()
26 cv2.imshow('Frame', frame)
27 # When user presses q -> quit
28 if cv2.waitKey(1) & 0xFF == ord('q'):
29 break
30# Camera must be released
31cap.release()
32
33# Playing Video from file
34cap = cv2.VideoCapture('movie.mp4')
35while cap.isOpened():
36 _, frame = cap.read()
37 # Play the video in grayscale
38 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
39 cv2.imshow('frame', gray)
40 if cv2.waitKey(1) & 0xFF == ord('q'):
41 break
42cap.release()
43
44# Drawing The Line in OpenCV
45# cv2.line(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
46cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
47
48# Drawing Rectangle
49# cv2.rectangle(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
50# thickness = -1 used for filling the rectangle
51cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
52
53# Drawing Circle
54# cv2.circle(img, (xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness)
55cv2.circle(img, (200, 90), 100, (0, 0, 255), -1)
56
57# Drawing Ellipse
58cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
59
60# Adding Text On Images
61cv2.putText(img, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
62
63# Blending Images
64img1 = cv2.imread('cat.png')
65img2 = cv2.imread('openCV.jpg')
66dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
67
68# Thresholding image
69# Binary Thresholding
70_, thresImg = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
71# Adaptive Thresholding
72adapThres = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
73
74# Blur Image
75# Gaussian Blur
76blur = cv2.GaussianBlur(img, (5, 5), 0)
77# Median Blur
78medianBlur = cv2.medianBlur(img, 5)
79
80# Canny Edge Detection
81img = cv2.imread('cat.jpg', 0)
82edges = cv2.Canny(img, 100, 200)
83
84# Face Detection using Haar Cascades
85# Download Haar Cascades from https://github.com/opencv/opencv/blob/master/data/haarcascades/
86import cv2
87import numpy as np
88
89face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
90eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
91
92img = cv2.imread('human.jpg')
93gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
94
95faces = face_cascade.detectMultiScale(gray, 1.3, 5)
96for x, y, w, h in faces:
97 # Draw a rectangle around detected face
98 cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
99 roi_gray = gray[y : y + h, x : x + w]
100 roi_color = img[y : y + h, x : x + w]
101 eyes = eye_cascade.detectMultiScale(roi_gray)
102 for ex, ey, ew, eh in eyes:
103 # Draw a rectangle around detected eyes
104 cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
105
106cv2.imshow('img', img)
107cv2.waitKey(0)
108
109cv2.destroyAllWindows()
110# destroyAllWindows() destroys all windows.
111# If you wish to destroy specific window pass the exact name of window you created.
Further Reading: ¶
- Download Cascade from https://github.com/opencv/opencv/blob/master/data/haarcascades
- OpenCV drawing Functions https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
- An up-to-date language reference can be found at https://opencv.org
- Additional resources may be found at https://en.wikipedia.org/wiki/OpenCV
- Good OpenCV Tutorials