Open CV Features That You Must Know

In this tutorial, we will learn about exciting features of OpenCV thta every one must know. But before we start with, let’s see what is OpenCV?

What is OpenCV?

OpenCV (Open Source Computer Vision) is a library of programming functions that mostly aims at real-time computer vision. It is open-source software that is released under a BSD license, and it is free for both academic and commercial use.

OpenCV was originally developed by Intel and is now maintained by a non-profit foundation. It is written in C++ language and has interfaces for multiple programming languages, like C++, Python, Java,etc.

OpenCV is widely used in the fields of computer vision, image processing, and machine learning. It provides a wide range of functionality, including image and video processing, object detection, and face recognition. It also provides a number of pre-trained deep learning models that can be easily integrated into applications.

OpenCV is also widely used in various industries, such as self-driving cars, robotics, and security systems. Additionally, it is supported by many platforms, including Windows, Linux, MacOS, iOS, and Android.

Features of OpenCV

One of the main features of OpenCV is its ability to process images and videos. It includes over 2500+ algorithms which are specially designed for image and video analysis & can also be used to detect and recognize faces, identify objects, classify human actions in video and also to track camera movements, tracking the moving objects, extracting the 3D models of objects, producing 3D point clouds from stereo cameras, stitching images together to produce a high resolution image of an entire scene & much more

Another important feature of OpenCV is its ability to work with a wide variety of programming languages including C++, Python, Java and MATLAB. This makes it easy for developers to integrate OpenCV into their existing projects or to start a new project using their preferred programming language.

Here is the list of amazing openCV features:

1. Image and video processing:

OpenCV provides a wide range of functions for image and video processing, such as image filtering, image transformation, and feature detection. For example, the following code applies a Gaussian blur to an image:

import cv2

# Load image
img = cv2.imread("image.jpg")

# Apply Gaussian blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)

# Display original and blurred images
cv2.imshow("Original", img)
cv2.imshow("Blurred", blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. Object Detection:

OpenCV provides a vast number of pre-trained deep learning models for the purpose of object detection, such as the MobileNet-SSD, YOLOv3, and Faster R-CNN. For example, the following code uses the MobileNet-SSD model to detect objects in an image:

import cv2

# Load image
img = cv2.imread("image.jpg")

# Load the MobileNet-SSD model
net = cv2.dnn.readNetFromCaffe("MobileNetSSD_deploy.prototxt", "MobileNetSSD_deploy.caffemodel")

# Run object detection
blob = cv2.dnn.blobFromImage(img, 0.007843, (300, 300), (127.5, 127.5, 127.5))
net.setInput(blob)
detections = net.forward()

# Draw bounding boxes around detected objects
for i in range(detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    if confidence > 0.5:
        x1 = int(detections[0, 0, i, 3] * img.shape[1])
        y1 = int(detections[0, 0, i, 4] * img.shape[0])
        x2 = int(detections[0, 0, i, 5] * img.shape[1])
        y2 = int(detections[0, 0, i, 6] * img.shape[0])
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

# Display image
cv2.imshow("Object Detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. Image and video loading and saving:

OpenCV provides functions to load and save images and videos, such as cv2.imread() and cv2.imwrite(). For example, the following code loads the image and saves it with a name different thean the original file name.

import cv2

# Load image
img = cv2.imread("image.jpg")

# Save image with a different file name
cv2.imwrite("image_copy.jpg", img)

4. Image and video display:

OpenCV provides functions to display images and videos, such as cv2.imshow() and cv2.waitKey(). For example, the following code displays an image and waits for a key press:

import cv2

# Load image
img = cv2.imread("image.jpg")

# Display image
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Uses of openCV

OpenCV (Open Source Computer Vision Library) is a powerful library and is widely used in a variety of applications, including:

1. Computer vision and image processing:

OpenCV provides a wide range of functions for image and video processing, such as image filtering, image transformation, and feature detection. These functions can be used to build applications for image and video analysis, such as object detection, face recognition, and image stitching.

Example:

import cv2
import numpy as np

# Load the input image
img = cv2.imread("input_image.jpg")

# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect edges using Canny edge detection
edges = cv2.Canny(gray, 100, 200)

# Find lines in the image using Hough line transformation
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, maxLineGap=50)

# Draw the lines on the original image
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

# Display the result
cv2.imshow("Lines", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. Robotics and automation:

OpenCV can be used to build applications for robot vision, such as object tracking, machine learning, and 3D reconstruction.

Example:

import cv2
import numpy as np

# Load an image from disk
img = cv2.imread("image.jpg")

# Convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect edges using Canny Edge Detector
edges = cv2.Canny(gray, 50, 150)

# Find contours in the image
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours on the original image
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)

# Display the result
cv2.imshow("Result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, OpenCV is used to detect edges and contours in an image. The result is displayed on the screen, which can then be used by the robot to perform further actions.

3. Surveillance and security:

OpenCV can be used to build applications for surveillance and security, such as face recognition, object detection, and motion tracking.

4. Augmented Reality:

OpenCV can be used to build applications for augmented reality, such as image and video stitching, feature detection and matching, and 3D reconstruction.

Example:

import cv2
import numpy as np

# Load the reference image
reference_image = cv2.imread("reference.jpg")

# Load the camera
cap = cv2.VideoCapture(0)

while True:
    # Capture video frame-by-frame
    ret, frame = cap.read()

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Find the keypoints and descriptors using SIFT
    sift = cv2.xfeatures2d.SIFT_create()
    kp_frame, des_frame = sift.detectAndCompute(gray, None)
    kp_reference, des_reference = sift.detectAndCompute(reference_image, None)

    # Use BFMatcher to match the descriptors
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des_reference, des_frame, k=2)

    # Filter the matches using the ratio test
    good_matches = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good_matches.append(m)

    # Get the coordinates of the matched keypoints
    reference_points = np.float32([kp_reference[m.queryIdx].pt for m in good_matches])
    frame_points = np.float32([kp_frame[m.trainIdx].pt for m in good_matches])

    # Compute the homography matrix
    H, mask = cv2.findHomography(reference_points, frame_points, cv2.RANSAC)

    # Use the homography matrix to transform the reference image
    height, width, channels = reference_image.shape
    transformed_reference = cv2.warpPerspective(reference_image, H, (width, height))

    # Add the transformed reference image to the frame
    result = cv2.addWeighted(frame, 0.5, transformed_reference, 0.5, 0)

    # Display the result
    cv2.imshow("Result", result)

    # Break the loop if the "q" key is pressed
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

# Release the resources
cap.release()
cv2.destroyAllWindows()

5. Corner Detection:

Corner detection is a technique used in computer vision to identify the points in an image that are distinctive and can be used to track the image features. In OpenCV, corner detection is performed using the cv2.goodFeaturesToTrack function which implements the Harris corner detection algorithm.

This function takes an input grayscale image, and detects corners in the image by finding the points that have a large variation in intensity in all directions. The function returns the corners in the form of a list of points, represented as (x, y) coordinates.

In addition to Harris corner detection, OpenCV also provides the Shi-Tomasi corner detector, which is an improved version of the Harris corner detector. The Shi-Tomasi corner detector can be used by specifying the cv2.cornerMinEigenVal or cv2.cornerHarris as the cornerDetectionMethod argument in the cv2.goodFeaturesToTrack function.

import cv2
import numpy as np

# Load the image
img = cv2.imread("image.jpg", 0)

# Detect corners using the Harris corner detector
corners = cv2.goodFeaturesToTrack(img, 25, 0.01, 10, useHarrisDetector=True)

# Convert the corners to integers
corners = np.int0(corners)

# Draw the corners on the image
for i in corners:
    x, y = i.ravel()
    cv2.circle(img, (x, y), 3, (0, 0, 255), -1)

# Display the image with the corners drawn
cv2.imshow("Corners", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Medical image processing:

OpenCV can be used to build applications for medical image processing, such as image segmentation, image registration, and image analysis.

Example:

import cv2
import numpy as np

# Load the medical image
image = cv2.imread("medical_image.jpg", cv2.IMREAD_GRAYSCALE)

# Apply thresholding to segment the foreground from the background
_, thresholded = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY)

# Perform morphological operations to remove noise
kernel = np.ones((5, 5), np.uint8)
opened = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, kernel)

# Find contours in the image
contours, _ = cv2.findContours(opened, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on the original image
contoured_image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

# Display the result
cv2.imshow("Contoured Image", contoured_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

7. Computer graphics:

OpenCV can be used to build applications for computer graphics, such as image processing, image warping, and image morphing.

8. Industrial Automation:

OpenCV can be used to build applications for industrial automation, such as object tracking, machine learning, and 3D reconstruction.

9. Self-driving cars:

OpenCV can be used to build applications for self-driving cars, such as object detection, lane detection, and image stitching.

10. Automated Quality control:

OpenCV can be used to build applications for automated quality control, such as object detection, machine learning, and image analysis.

11. Video editing:

OpenCV can be used to build applications for video editing, such as image stabilization, video denoising, and video super-resolution.

Conclusion

In conclusion, OpenCV is a powerful library for computer vision and image processing. It provides a wide range of features that can be used to build a variety of applications, such as object detection, face recognition, image and video processing, object tracking, and machine learning. The library is open-source and highly optimized, making it easy to use and efficient in performance.

The features provided by OpenCV are not only limited to the above-mentioned list but also many more like 3D reconstruction, Image and video pyramids, Image and video histograms, Image and video gradient computation, Image and video morphology, Image and video filtering, Image and video transformation, Image and video feature detection, Image and video feature extraction, Image and video feature matching, Image and video feature description, Image and video feature matching, Image and video feature tracking, Image and video feature matching with FLANN and Image and video feature matching with KNN.

With its extensive documentation and active community, developers can easily learn and implement the library in their projects, making it a valuable tool for building computer vision and image processing applications.