The technological evolution and emergence of AI and ML have made digital images both a critical asset as well as a challenge. Organizations are continually adapting to new technologies every day to manage digital image data and train computer vision models.  

Since it is difficult to manually sort and filter such massive data for AI/ML projects and data pipelines, it is crucial to use automated tools for quick results. OpenCV (Open Source Computer Vision Library), developed by Intel in 1999, is an effective tool for automated and intelligent image management. 

This blog will take you through the ways to use OpenCV for advanced image sorting and filtering tasks. We will cover the basics in brief briefly before delving into the advanced techniques used to sort and filter images. 

OpenCV is a widely used, open-source library for computer vision and machine learning. While organizations are well-versed in the basic concepts, it is essential to go through them before diving deep into OpenCV. Here are the various ways the tool is used: 

A. Image and Video Handling

  • Reading, displaying, and writing – OpenCV performs basic operations of loading original images and videos for displaying them on Windows and saves the results. 
  • Image properties – OpenCV understands img dimensions, color channels (BGR for color images, grayscale), and pixel values. 

B. Basic Image Manipulation

  • Geometric transformations – OpenCV can perform operations like resizing, cropping, rotating, and translating images.
  • Pixel manipulation – OpenCV offers direct access and modification of each pixel value. 

C. Image Processing Techniques

  • Color spaces – The tool can convert between different color representations (BGR to grayscale, HSV).
  • Image filtering and smoothing – OpenCV applies filters such as Gaussian blur, median blur, etc, to reduce noise or improve features.
  • Thresholding – The tool can perform tasks like converting grayscale images into binary images using a threshold value. 
  • Morphological operations – OpenCV can perform operations like erosion and dilation for shape analysis and removing noise. 
  • Edge detection – The tool uses algorithms like Canny to identify boundaries within an image.

D. GUI Features

  • HighGUI module – The tool offers functions to create Windows, display images/videos, handle mouse and keyboard events, and create trackbars for interactive control.

E. Core Data Structures

  • Mat object – This is the fundamental data structure in OpenCV to represent images and matrices for efficient handling of image data. These integrate seamlessly with Numpy arrays in Python. 

These form the base for advanced computer vision applications such as object detection and image recognition, face recognition, gesture recognition, contour analysis and detection, video processing, and augmented reality applications. 

Beyond Basics: Advanced Filtering Techniques in OpenCV

Infographic explaining OpenCV for advanced image sorting and filtering tasks using key techniques.

OpenCV goes beyond the basic smoothing and thresholding and has advanced filtering techniques for more sophisticated image manipulation. These techniques are effective for extracting specific features, cleaning complex noise patterns, and preparing images in a highly targeted manner. This allows subsequent analysis with respect to feature matching for image sorting for high-quality data annotation

Let’s take you through the advanced filtering techniques of OpenCV:

A. Edge Detection for Content Analysis

OpenCV’s edge detection technique is used to detect the boundaries of objects and regions within an image, highlighting significant changes in the pixel intensity. This is one of the crucial steps in different open computer vision systems, such as image segmentation, object detection, and feature extraction. 

OpenCV uses different vision algorithms to simplify image analysis, enhance feature extraction, improve object detection, and facilitate image segmentation. Here’s a detailed insight into the algorithms used in OpenCV: 

➞ Canny Edge Detection

  • This is a multi-stage algorithm designed to produce thin, connected edges.
  • The process involves noise reduction (applying a Gaussian blur), gradient calculation, non-maximum suppression, and hysteresis thresholding. 
  • As per the OpenCV documents, Canny is popular and is used for its ability to handle noisy images and produce accurate edge maps.

➞ Sobel Edge Detection

  • The algorithm calculates the gradient of image intensity to find the edges. 
  • The process uses different kernels to analyze horizontal and vertical gradients for better direction-specific edge detection.
  • This algorithm has proved to be very effective for quick edge detection, emphasizing edge orientation in relatively clean images.

➞ Laplacian Edge Detection

  • This edge detection technique detects edges by finding zero-crossings in the second derivative of the image intensity. 
  • The operator is generally used for pre-smoothed images.

➞ Holistically-Nested Edge Detection

  • OpenCV has a deep learning-based edge detection method, which is known as HED. 
  • The technique applies a convolutional neural network for predicting edges. It can handle more complex problems and provide more detailed edge maps. 

B. Morphological Operations for Shape-Based Filtering

OpenCV’s morphological operations are a set of advanced image processing techniques that analyze images based on shapes. The technique is primarily used for shape-based filtering and analysis, mainly for binary or grayscale images. It applies a ‘structuring element’ (or kernel) to the image to define the pixel neighborhood for each operation. 

Here’s a look at the key operations used in this technique:

➞ Erosion – This process shrinks the foreground objects (white regions) by removing pixels from object boundaries. This is an effective way to remove small noise or separate connected components. 

➞ Dilation – This is the opposite of erosion and expands the foreground objects by adding pixels to object boundaries. These can be used to fill small holes or connect broken components.

➞ Opening – The erosion process is followed by dilation and is effective in removing small objects or noise without disturbing the shape of larger objects. 

➞ Closing – In this case, dilation is followed by erosion. This helps in filling small holes within objects and connecting nearby components.

C. Custom Kernels (Convolutions) for Specific Effects

Custom kernels are used in real-time image processing to apply different effects to images using a process called convolution. The cv2.filter2D( ) function in OpenCV is primarily used for applying these custom kernels. 

Here’s a detailed explanation of the different aspects where custom kernels are used:

➞ Sharpening – Kernels with positive central value and negative surrounding values are used to improve edges and details, thus helping create a sharper image. 

  Application using Python:  

  import cv2

    import numpy as np

    # Sharpening kernel

    kernel_sharpening = np.array([[-1, -1, -1],

                                  [-1,  9, -1],

                                  [-1, -1, -1]])

➞ Edge detection – Sobel, Prewitt, and other such kernels are used to highlight the differences in pixel intensity and can also be used for edge detection. 

Application using Python:

  # Example Sobel X kernel

    kernel_sobel_x = np.array([[-1, 0, 1],

                               [-2, 0, 2],

                               [-1, 0, 1]])

➞ Embossing – The kernels that emphasize directional changes can help create an embossed effect, which makes the image appear raised or recessed. 

Application using Python:

    # Embossing kernel

    kernel_emboss = np.array([[-2, -1, 0],

                              [-1,  1, 1],

                              [ 0,  1, 2]])

Blurring (Averaging) – The kernels that carry equal positive values can calculate the average values of each pixel, thus helping in a blurring or smoothing effect. 

Application using Python:

 # Averaging blur kernel

    kernel_blur = np.ones((3, 3), np.float32) / 9

To put it in a nutshell, here’s how you can apply custom kernels in OpenCV:

import cv2

import numpy as np

 

# Load an image

image = cv2.imread(‘your_image.jpg’)

 

# Define your custom kernel (e.g., sharpening kernel)

kernel = np.array([[-1, -1, -1],

                   [-1,  9, -1],

                   [-1, -1, -1]])

 

# Apply the kernel using filter2D

output_image = cv2.filter2D(image, -1, kernel) # -1 indicates same depth as source

D. Frequency Domain Filtering Using the Fourier Transform

Frequency domain filtering with the help of the Fourier Transform feature in OpenCV helps in image processing in the frequency domain. This can give you an advantage in noise reduction and edge detection. 

Let’s take you through the details: 

➞ Fourier Transform (DFT)

  • The DFT decomposes an image into its constituent sine and cosine waves at different frequencies.
  • The cv2.dft( ) function is used for transformation in OpenCV. 
  • The result of this process is a complex-valued array with magnitudes representing the strength of each frequency component. 
  • You can find the zero-frequency component located at the center of the frequency spectrum. You can shift it to the center using cv2.fft.fftshift( ) to make visualization and filtering easier. 

➞ Frequency Domain Filtering

  • Filters are applied to the frequency domain by multiplying the image’s DFT with the frequency response of the filter. 
  • Low-pass filters weaken high-frequency components for better smoothing and blurring effects and reduce noise.
  • High-pass filters weaken the low-frequency components to improve edges and sharp details that can also amplify noise. 
  • Band-pass filters are used to filter out a range of frequencies to pass through, which is also useful for isolating key features in images. 

➞ Inverse DFT

The filtered frequency spectrum is transformed back to the spatial domain by applying the inverse DFT (cv2.idft ( ) in OpenCV).

➞ OpenCV Functions 

  • cv2.dft( ): Performs DFT
  • cv2.idft ( ): Performs inverse DFT
  • cv2.fft.fftshift( ): Shifts zero frequency spectrum to the center
  • cv2.magnitude( ): Calculates the DFT output magnitude
  • cv2.normalize( ): Scales the visualization magnitude
  • cv2.copyMakeBorder( ): The function is used to pad images to optimal sizes for DFT performance

That explains all the filtering methods used in OpenCV. But to make the most out of it, it is crucial to understand the sorting methods and follow the step-by-step OpenCV image filtering guide before putting them into practice. 

OpenCV for Advanced Image Sorting

Advanced image sorting is one of the many AI and computer vision tasks performed by OpenCV. You can access tools for feature extraction, image comparison, and clustering. These can be combined to implement sophisticated sorting algorithms. 

Let’s give you a detailed breakdown of advanced image sorting with OpenCV: 

A. Feature Extraction

➞ SIFT (Scale-Invariant Feature Transformation) – SIFT is a strong algorithm that is used to detect and describe local features in images, invariant to scale and rotation. You can do it using the code cv2.SIFT_create( ) to create a SIFT object and detectAndCompute( ) to extract descriptors and key points.

➞ ORB (Oriented FAST and Rotated BRIEF) – ORB can be a fast and efficient alternative to SIFT. This is especially suited for real-time applications. You can use the code cv2.ORB_create( ) to create an ORB object and detectAndCompute( ) to extract features. 

➞ Other feature extractors – OpenCV also supports SURF, AKAZE, and BRISK features.

B. Feature Matching

➞ Brute-force matcher – This is a simple process to match datasets. It is more useful for smaller datasets. You can use the code cv2.BFMatcher( ) to create a brute-force matcher and match( ) or knnMatch( ) to find matches. 

➞ FLANN (Fast Library for Approximate Nearest Neighbors) – The method is more applicable for larger datasets, especially when you are dealing with high-dimensional feature extractors. You can use the code cv2.FlannBasedMatcher( ) to create a FLANN matcher. 

C. Image Comparison and Similarity

➞ Feature distance – You can quantify the similarity between two images by analyzing the distance between feature descriptors. This can be done by using Euclidean distance for SIFT and Hamming distance for ORB. 

➞ Ratio test – The ratio test can help in filtering out ambiguous matches, which is crucial for feature matching. 

D. Clustering 

➞ K-means clustering – The popular algorithm is used to group data points into clusters on the basis of their proximity. You can use the code cv2.kmeans( ) to perform k-means clustering. 

➞ Hierarchical clustering – Building a hierarchy of structures is another way to group images. While you will not get it directly on OpenCV, you can implement it using libraries like SciPy. 

That will give you a clear idea about the different image sorting and filtering techniques in OpenCV. The following section will take you through the ways you can put these into practice and sort and filter images using OpenCV. 

Practical Guide: Implementing Advanced OpenCV Techniques

Infographic guide on using OpenCV for advanced image sorting and filtering tasks step by step.

All the previous sections explained the various techniques to sort and filter images using OpenCV. But how do you put them into practice? Let’s take you through the steps to implement those techniques:

A. Environment Setup: Install OpenCV and Python

➞ Prerequisites – Check for the latest version of Python before installing. 

➞ Installation methods – Install the OpenCV library for your chosen programming language. Generally, it is PIP for Python. ( code: pip install opencv-python )

➞ Import OpenCV – Import the OpenCV library in your code. For Python, the code is ‘importcv2.’

➞ Creating virtual environments – Virtual environments can be created using venv or Conda. These help in creating isolated spaces for each project. The setup helps developers minimize ‘dependency hell’ and switch between project requirements without affecting the system-wide Python installation. 

B. Loading and Displaying Images/Videos

➞ Images – Use cv2.imread( ) to load an image from a file and cv2.imshow( ) to display it. 

➞ Videos/Webcam – OpenCV provides cv2.VideoCapture( ) for accessing video files or webcam streams. Use read( ) to read the different frames. 

C. Image and Video Processing

OpenCV offers a wide array of functions to manipulate 2D images and videos. The operations include: 

➞ Resizingcv2.resize( )

➞ Cropping – Array slicing on the image data

➞ Color space conversioncv2.cvtColor( ) for conversion from BGR to Grayscale of HSV

➞ Filtering – You will have access to filters like Gaussian blur, median blur, etc., for OpenCV image filtering

➞ Edge detectioncv2.Canny( ) in the case of Canny edge detection

➞ Thresholdingcv2.threshold( ) to convert multiple images to binary

D. Advanced Computer Vision Tasks

➞ Object detection – Use pre-trained models or train your artificial intelligence model to detect specific objects using functions like objdetect or the dnn module.

➞ Feature detection and matching – You can use modules like feature2d for detecting key points and descriptors for tasks like image stitching or object recognition. 

➞ Machine learning – Integrate operations with machine learning and deep learning algorithms for image classification using OpenCV, regression, and clustering. 

E. Saving Results

Use the code cv2.imwrite( ) to save the processed images to a file. 

In the case of videos, you can use cv2.VideoWriter( ) to save processed frames as a new video file. 

F. Resource Management

Do not close OpenCV without releasing resources like video capture objects. Use the code release( ) and cv2.destroyAllWindows( ) to destroy all OpenCV windows when the program finishes. 

Here’s an example for loading and displaying an image:

import cv2

# Load an image

image = cv2.imread(‘path/to/your/image.jpg’)

# Check if the image loaded successfully

If the image is None:

    print(“Error: Could not load image.”)

else:

    # Display the image

    cv2.imshow(‘My Image’, image)

    # Wait for a key press and then close the window

    cv2.waitKey(0)

    cv2.destroyAllWindows()

Key Takeaways

The different methods to sort and filter images in OpenCV make things easier for all. However, not knowing the correct codes or the ways to use OpenCV or the right techniques will not help you get the right results. Considering the fact that the OpenCV library contains over 2500 algorithms, it is essential to know the right methods.

Also, understand the correct ways to install OpenCV and use it correctly to get the best results. 

Ankit Sureka