OPENCV – PYTHON | Opencv and its MODULES | 97% people don’t know
OpenCV, short for Open Source Computer Vision Library, is a powerful open-source computer vision and machine learning software library. It offers a wide range of tools and modules that allow you to manipulate images and videos, perform object detection, and much more. In this blog, we will take a closer look at some of the core modules available in OpenCV’s Python library, and provide examples of how these modules can be applied in real-world scenarios.
Core Module
The core module is the heart of OpenCV, providing basic functions for image processing, data structures, and utilities. Let’s start with a simple example of how to load and display an image using this module:
import cv2
# Load an image
image = cv2.imread(‘example.jpg’)
# Display the image
cv2.imshow(‘Image’, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code loads an image and displays it. The core module is essential for almost every OpenCV project, as it forms the foundation for all other modules.
Imgproc Module
The imgproc module focuses on image processing techniques. One common use case is image smoothing, which helps reduce noise and improve image quality. Here’s an example of how to apply Gaussian blur to an image:
import cv2
# Load an image
image = cv2.imread(‘example.jpg’)
# Apply Gaussian blur
blurred_image = cv2.GaussianBlur(image, (5, 5), 0)
# Display the original and blurred images
cv2.imshow(‘Original’, image)
cv2.imshow(‘Blurred’, blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The imgproc module enables you to perform various image transformations, such as resizing, rotating, and cropping.
Imgcodecs Module
The imgcodecs module is responsible for image I/O operations. You can use it to save images to disk, convert between different image formats, and more. Here’s an example of how to save an image to a different format:
import cv2
# Load an image
image = cv2.imread(‘example.jpg’)
# Save the image as PNG
cv2.imwrite(‘example.png’, image)
This module is indispensable for working with images in different formats.
Video Module
The video module allows you to work with video streams and files. Let’s explore a simple video capture example:
import cv2
# Open a video capture device (e.g., webcam)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow(‘Video’, frame)
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
cap.release()
cv2.destroyAllWindows()
In this example, we capture video from a webcam and display it in a window. The video module can be used for tasks like object tracking and motion detection.
Calib3d Module
The calib3d module deals with camera calibration and 3D reconstruction. It is essential in computer vision applications like augmented reality and 3D modeling. One common use is estimating the camera’s pose using known 3D points and their 2D projections:
import cv2
import numpy as np
# Known 3D points and their 2D projections
object_points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0]], dtype=np.float32)
image_points = np.array([[0, 0], [1, 0], [0, 1]], dtype=np.float32)
# Estimate camera pose
retval, rvec, tvec = cv2.solvePnP(object_points, image_points, camera_matrix, dist_coeff)
# rvec and tvec now contain rotation and translation information
The calib3d module enables you to perform complex camera calibration and 3D reconstruction tasks.
Features2D Module
The features2d module is all about detecting and matching image features. An illustrative example is feature detection and matching between two images:
import cv2
# Load two images
img1 = cv2.imread(‘image1.jpg’, 0)
img2 = cv2.imread(‘image2.jpg’, 0)
# Initialize the ORB detector
orb = cv2.ORB_create()
# Find keypoints and descriptors in both images
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
# Match features
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
# Sort them in ascending order of distance
matches = sorted(matches, key=lambda x: x.distance)
# Draw the first 10 matches
match_img = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], outImg=None)
cv2.imshow(‘Matching’, match_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The features2d module is crucial for tasks like object recognition and image stitching.
Dnn Module
The dnn module allows you to work with deep learning models. You can load pre-trained models for tasks like image classification, object detection, and facial recognition. Let’s use a pre-trained YOLO (You Only Look Once) model for object detection:
import cv2
# Load YOLO model
net = cv2.dnn.readNet(‘yolov3.weights’, ‘yolov3.cfg’)
# Load classes and configuration
classes = []
with open(‘coco.names’, ‘r’) as f:
classes = f.read().strip().split(‘\n’)
# Load an image
image = cv2.imread(‘object_detection.jpg’)
# Detect objects
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward()
# Post-processing for object detection
# (drawing bounding boxes, class labels, and confidence scores)
# Display the result
cv2.imshow(‘Object Detection’, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The dnn module makes it easy to integrate deep learning into your computer vision projects.
Ml Module
The ml module provides machine learning tools for tasks such as image classification and regression. Here’s an example of using the K-Nearest Neighbors (KNN) algorithm for image classification:
import cv2
import numpy as np
# Load training data and labels
train_data = np.array([[0, 0], [1, 1], [2, 2], [3, 3]], dtype=np.float32)
labels = np.array([0, 1, 2, 3], dtype=np.int32)
# Create a K-Nearest Neighbors classifier
knn = cv2.ml.KNearest_create()
# Train the classifier
knn.train(train_data, cv2.ml.ROW_SAMPLE, labels)
# Test a new data point
test_data = np.array([[1.5, 1.5]], dtype=np.float32)
_, results, _, _ = knn.findNearest(test