OpenCV (Open Source Computer Vision Library) is an open-source computer vision library that contains over 2500 optimized algorithms for tasks such as object detection, facial recognition, image classification, and video processing.
Installing OpenCV
To install OpenCV in Python, use pip
:
pip install opencv-python
pip install opencv-python-headless # Without GUI
Reading and Writing Images
Read an image
You can load images using the cv2.imread()
function:
import cv2
image = cv2.imread('path/to/image.jpg')
Display an image
To display an image in a window:
cv2.imshow('Window Title', image)
cv2.waitKey(0) # Waits until a key is pressed
cv2.destroyAllWindows()
Save an image
To save a modified image:
cv2.imwrite('path/to/new_image.jpg', image)
Image Processing
Convert to grayscale
To convert an image to grayscale:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Convert to other color spaces
You can convert images to different color spaces, such as HSV:
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
Resize an image
To resize an image:
resized_image = cv2.resize(image, (width, height))
Rotate an image
To rotate an image:
height, width = image.shape[:2]
center = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
Crop an image
To crop an image to a specific region:
crop = image[y:y+h, x:x+w]
Change brightness and contrast
bright_image = cv2.convertScaleAbs(image, alpha=1.2, beta=30) # alpha: contrast, beta: brightness
Apply a Gaussian filter
To smooth an image and reduce noise:
smooth_image = cv2.GaussianBlur(image, (5, 5), 0)
Median filter
To remove noise while preserving edges:
median_image = cv2.medianBlur(image, 5)
Video Processing
Capture video from a camera
To capture real-time video:
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
cv2.imshow('Live Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
Read video from a file
capture = cv2.VideoCapture('path/to/video.mp4')
while True:
ret, frame = capture.read()
if not ret:
break
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
Algorithms
Edge detection with Canny
To detect edges in an image:
edges = cv2.Canny(gray_image, threshold1, threshold2)
Find contours
To detect contours in a binary image:
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Draw contours
To draw detected contours on an image:
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
Corner detection
To detect corners (Harris Corner Detection) in an image:
corners = cv2.cornerHarris(gray_image, 2, 3, 0.04)
Feature detection with ORB
Using ORB for feature detection
orb = cv2.ORB_create()
keypoints, descriptors = orb.detectAndCompute(image, None)
image_with_keypoints = cv2.drawKeypoints(image, keypoints, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
Object Recognition
Using Haar Cascades
To detect objects, such as faces, you can use cascade classifiers.
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_classifier.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
Draw rectangles around detected faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
Segmentation
Segmentation with k-means
Z = image.reshape((-1, 3))
Z = np.float32(Z)
k = 8 # Number of colors
_, labels, centers = cv2.kmeans(Z, k, None, (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1.0), 10, cv2.KMEANS_RANDOM_CENTERS)
centers = np.uint8(centers)
segmented_image = centers[labels.flatten()].reshape(image.shape)
Using SIFT
Advanced feature detection
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(image, None)
Machine Learning
Using SVM for image classification
from sklearn import svm
# Assuming you have training data X and labels y
clf = svm.SVC()
clf.fit(X, y)
# Prediction
predictions = clf.predict(new_data)