Skip to content

junming732/defect_detection

Repository files navigation

defect_detection_cpp

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.

Features

  • 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)

Build (Ubuntu/Debian)

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

Run

./build/defect_detection data/sample_1.png --out results/sample_1_annotated.png --min-area 80

Outputs:

  • results/sample_1_annotated.png (red boxes over detected defects)
  • results/sample_1_annotated_mask.png (binary mask)

Tests

ctest --test-dir build --output-on-failure

Project Layout

defect_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

Notes

  • 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.

ONNX Runtime (optional segmentation hook)

Install ONNX Runtime (Ubuntu CPU)

sudo apt update
sudo apt install -y onnxruntime-dev

If CMake cannot find ORT automatically, you can hint the path:

cmake -S . -B build -DWITH_ONNXRUNTIME=ON -DORT_ROOT=/usr

Run with a model

Assumes 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.png

If --onnx is omitted, the classical threshold + morphology pipeline is used.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published