← Back to Showcase

Cricket Detection and Clustering in Images

Executive Summary

1. Image Pre-Processing & Feature Extraction

The first stage of the pipeline focuses on isolating the subjects from the background. Raw RGB images are loaded and converted to grayscale. To optimize computational cost and eliminate external background noise, a Region of Interest (ROI) is interactively cropped based on bounding box coordinates.

Due to the presence of varying lighting conditions and shadows within the test images, global thresholding methods proved insufficient. Therefore, an Adaptive Thresholding algorithm (Mean/Gaussian) was implemented via OpenCV. This method calculates the threshold for small regions of the image, providing superior robustness to illumination gradients. The resulting binary features are mapped into a spatial coordinate matrix $P$, representing the pixels of interest, as demonstrated in Figure 1.

Comparison of the original grayscale ROI and the binary image obtained after adaptive thresholding.
Figure 1 - Pre-processing stage: The cropped Region of Interest (left) and the resulting binary mask after adaptive thresholding (right).

2. Density-Based Spatial Clustering (DBSCAN)

To group the isolated pixels into individual distinct crickets, unsupervised machine learning was applied. The DBSCAN (Density-Based Spatial Clustering of Applications with Noise) algorithm from Scikit-learn was selected over traditional methods like K-Means.

The choice of DBSCAN is highly engineering-driven for this specific application: it groups together points that are closely packed together based on two parameters (eps and min_samples), without requiring a predefined number of clusters. This makes it exceptionally suited for spatial coordinate matrices where the exact number of targets (crickets) is unknown a priori. The resulting cluster map is shown in Figure 2.

Visual representation of the DBSCAN clustering output, with different colors identifying individual clusters.
Figure 2 - DBSCAN clustering output. The algorithm groups density-reachable pixels together, assigning a unique identifier to each independent cricket mapped onto the F-matrix.

3. Noise Filtering & Final Results

The raw clustering output inevitably contains artifacts due to residual background noise or physical anomalies in the image. A post-processing filtration logic was implemented to evaluate the pixel count (dimension) of each identified cluster.

By enforcing an upper limit parameter ($N$), any cluster exceeding the expected size of a cricket is classified as an artifact and mathematically excluded (values reset to -1 in the image array). The final output dynamically visualizes only the accurately clustered targets, successfully isolating the crickets from the background noise (Figure 3).

Final processed image showing only the correctly identified and sized cricket clusters.
Figure 3 - Final filtered result demonstrating the successful isolation of the targets, excluding oversized noise artifacts.

4. Software Engineering & OOP Architecture

Beyond the algorithmic workflow, the project emphasizes high-quality software engineering practices: