./anomalies-detecting ./source.jpg 20,100,280,200,20,400,600,200,600,20,100,600,850,600,900,20 ./output.jpg
A fast, KD-tree–based image anomaly-detection tool implemented in C++ using OpenCV and nanoflann.
The algorithm searches for unusual local patterns (patches) in an image.
Each image patch is embedded into a high-dimensional feature space and compared against all other patches using efficient nearest-neighbor search.
Patches that significantly deviate from their neighbors are marked as anomalies.
./anomalies-detecting ./source.jpg 20,100,280,200,20,400,600,200,600,20,100,600,850,600,900,20 ./output.jpg
This project implements local anomaly detection using self-similarity:
- Convert image to grayscale
- Extract sliding-window patches
- Normalize each patch vector
- Build KD-tree (nanoflann)
- For each ROI patch:
- Query nearest neighbors
- Compute anomaly score
- Generate anomaly mask
- C++17
- CMake ≥ 3.10
- OpenCV
- nanoflann (header-only)
mkdir build
cd build
cmake ..
cmake --build .
Sliding window of DIMENSION × DIMENSION extracts all patches.
Each patch → normalized feature vector.
Includes brightness coefficient.
All patches inserted into nanoflann KD-tree for fast nearest-neighbor search.
KNN search with filtering of nearby spatial neighbors.
N-th nearest neighbor distance used as anomaly score.
Higher distance = more anomalous.
Image is split into vertical strips processed by multiple threads.
getAnomalies() returns a grayscale anomaly mask.
| File | Description |
|---|---|
| main.cpp | CLI runner |
| anomalies-detecting.cpp/.h | Core algorithm |
| CMakeLists.txt | Build config |
MIT License
