A minimal, production-style C++17 project for surface defect detection using OpenCV and CMake.
This is a classical CV pipeline (preprocess → threshold → morphology → filtering) with clean structure, tests, CI, and reproducible builds.
- C++17, CMake
- OpenCV (core, imgproc, imgcodecs)
- Binary mask generation and annotated output
- Zero external test deps (simple sanity test + CTest)
- GitHub Actions CI (Ubuntu)
sudo apt update
sudo apt install -y build-essential cmake libopencv-dev git
git clone https://github.com/yourusername/defect_detection_cpp.git
cd defect_detection_cpp
cmake -S . -B build
cmake --build build./build/defect_detection data/sample_1.png --out results/sample_1_annotated.png --min-area 80Outputs:
results/sample_1_annotated.png(red boxes over detected defects)results/sample_1_annotated_mask.png(binary mask)
ctest --test-dir build --output-on-failuredefect_detection_cpp/
├── CMakeLists.txt
├── include/defect_detector.hpp
├── src/{defect_detector.cpp, main.cpp}
├── tests/{CMakeLists.txt, test_main.cpp, test_defect_detector.cpp}
├── data/ (sample images)
├── results/ (.gitkeep by .gitignore)
├── .github/workflows/ci.yaml
├── .clang-format
└── README.md
- No GUI windows are opened (CI-friendly). Results are saved to files.
- You can extend the pipeline with adaptive thresholding, texture features, or ONNX Runtime for a small CNN.
sudo apt update
sudo apt install -y onnxruntime-devIf CMake cannot find ORT automatically, you can hint the path:
cmake -S . -B build -DWITH_ONNXRUNTIME=ON -DORT_ROOT=/usrAssumes a segmentation model with input shape (1, C, H, W) and output (1, 1, H, W) where values are probabilities in [0,1].
./build/defect_detection data/sample_1.png --onnx path/to/model.onnx --out results/onnx_annotated.pngIf --onnx is omitted, the classical threshold + morphology pipeline is used.