The Intel Image Processing Library (IPL) was a library developed by Intel that provided functions for image processing and computer vision. OpenCV was initially built on top of IPL, and later versions of OpenCV absorbed many of its functionalities.
used in computer vision tasks such as object detection, face detection, face recognition, image segmentation, etc. but also contains a lot of useful functions that you may need in ML.
It provides pre-trained models for object detection using methods like Haar cascades and HOG (Histogram of Oriented Gradients).
You can use OpenCV's DNN (Deep Neural Network) module for working with more advanced object detection models.
Features:
Functions:
Haar Cascades: cv2.CascadeClassifier.detectMultiScale()
CascadeClassifier is trained to recognize specific patterns, and detectMultiScale() is used to find those patterns in an image.It works by sliding a window over the input image at different scales and applying the trained classifier to identify regions containing the target object.
# Example Usage
import cv2
# Load the pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Detect faces in an image
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5)
# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
HOG (Histogram of Oriented Gradients): cv2.HOGDescriptor.detectMultiScale()
HOG is a feature descriptor technique that captures the distribution of intensity gradients in an image. The HOGDescriptor is configured with parameters, and detectMultiScale() is used to find objects in an image.
Similar to Haar cascades, it slides a window over the image at different scales and applies the HOG descriptor to detect objects. The function returns a list of rectangles representing the detected objects' bounding boxes.
# Example Usage
import cv2
# Create a HOGDescriptor object
hog = cv2.HOGDescriptor()
# Detect objects using HOG
rects, weights = hog.detectMultiScale(image, winStride=(8, 8), padding=(4, 4))
# Draw rectangles around the detected objects
for (x, y, w, h) in rects:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
Deep Neural Network (DNN) Module: cv2.dnn.readNet(), net.forward()
readNet() is used to load a pre-trained deep learning model from a file (e.g., Caffe, TensorFlow, YOLO formats).forward() is used to perform forward-pass inference on an input image.# Example Usage
import cv2
# Load pre-trained deep learning model (e.g., Caffe)
net = cv2.dnn.readNet('deploy.prototxt', 'model.caffemodel')
# Preprocess the input image and perform forward-pass inference
blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(300, 300), mean=(104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
# Interpret the detections and draw bounding boxes
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
box = detections[0, 0, i, 3:7] * np.array([width, height, width, height])
(startX, startY, endX, endY) = box.astype("int")
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)
Pros:**
Cons:**