An automated computer vision system for detecting and classifying defects in wires/cables using OpenCV and C++.
This is a software program that acts like an automated quality inspector for wires and cables. Just like how a human inspector would carefully examine a wire for problems, this program uses computer vision to automatically find defects in photographs of wires. This is not a trained model and uses nothing but the functions provided by OpenCV which allows it to be fast and run on minimal hardware.
When you give this program a photo of a wire or cable, it:
- Measures the wire - Automatically calculates how thick the wire is at different points (in pixels)
- Finds defects - Scans the entire wire for three types of manufacturing problems:
- Pin holes: Small dark dots where material is missing
- Cuts: Larger dark areas indicating damage or gaps
- Scratches: Bright lines showing surface damage
- Creates visual reports - Generates new images with:
- Red circles drawn around each defect found
- Labels showing what type of defect each one is
- Measurements showing wire thickness
In manufacturing, checking every wire by eye is:
- Time consuming and expensive
- Prone to human error (inspectors might miss small defects)
- Difficult to maintain consistency
This automated system can inspect wires consistently, quickly, and document everything it finds, helping manufacturers maintain quality control. Currently works best with blue wires (see sample images provided)
- CMake 3.10+
- OpenCV 4.x
- C++17 compatible compiler
- Visual Studio 2019/2022 (Windows) or GCC/Clang (Linux/Mac)
wire-defect-detection/
├── images/ # Input images (wire1.jpg - wire4.jpg)
├── output/ # Generated output (created automatically)
├── src/
│ ├── main.cpp # Application entry point
│ ├── preprocessing.cpp # Image preprocessing
│ ├── defects.cpp # Defect detection algorithms
│ └── measurements.cpp # Diameter measurement
├── include/
│ ├── preprocessing.h
│ ├── defects.h
│ └── measurements.h
└── CMakeLists.txt
Windows (CMD):
mkdir build
cd build
cmake ..
cmake --build . --config DebugLinux/Mac:
mkdir build
cd build
cmake ..
make# Syntax: wire-defect-detection <image_path> <image_number>
# Windows
Debug\wire-defect-detection.exe ..\..\images\wire1.jpg 1
# Linux/Mac
./wire-defect-detection ..\../images/wire1.jpg 1Create a batch script to process all images:
Windows (process_all.bat):
@echo off
for %%i in (1,2,3,4) do (
Debug\wire-defect-detection.exe ..\images\wire%%i.jpg %%i
echo Processed image %%i
)Linux/Mac (process_all.sh):
#!/bin/bash
for i in {1..4}; do
./wire-defect-detection ../../images/wire$i.jpg $i
done| File | Description |
|---|---|
Task 1: diameteroutput.jpg |
Wire with diameter measurements at 3 points |
Task 2: defectoutput[1-4].jpg |
Detected defects circled in red |
Task 3: classifyoutput[1-4].jpg |
Detected defects circled with labels |
- Appearance: Small, dark, circular spots
- Criteria: Area < 15% of wire width, aspect ratio 0.85-1.15
- Appearance: Larger dark regions with irregular shapes
- Criteria: Dark areas that don't meet pin hole criteria
- Appearance: Bright, elongated marks
- Criteria: Low saturation, elongated shape
1. Image Preprocessing
├── Grayscale conversion
├── Bilateral filtering
└── Wire segmentation
2. Diameter Measurement
├── Edge detection
├── Width calculation at 25%, 50%, 75% height
└── Visualization
3. Defect Detection
├── Dark defects (HSV value channel)
│ ├── Adaptive thresholding
│ ├── Edge exclusion
│ └── Classification
└── Bright defects (HSV saturation channel)
├── Threshold + region merging
└── Proximity filtering
// Key parameters (in defects.cpp)
const int EDGE_EROSION_SIZE = 11; // Edge exclusion kernel
const int ADAPTIVE_BLOCK_SIZE = 27; // Adaptive threshold window
const int SATURATION_THRESHOLD = 40; // Scratch detection threshold
const int DEFECT_MERGE_SIZE = 17; // Scratch merging kernel
const int MIN_DEFECT_AREA = 4; // Minimum defect size (pixels)- Processing Time: ~500ms per image
- Supported Formats: JPEG, PNG, BMP
- Recommended Resolution: 1000x3000 pixels
| Issue | Solution |
|---|---|
| "Can't read image" | Check file path and format |
| No defects detected | Adjust threshold parameters |
| Build errors | Verify OpenCV installation |
| Missing output folder | Program creates it automatically |
# Single image processing
Debug\wire-defect-detection.exe C:\path\to\wire_image.jpg 1
# Expected console output:
Saved diameteroutput.jpg: "C:\\Users\\modin\\Projects\\wire-defect-detection\\output"
Saved visualization: defectoutput4.jpg
Saved classification visualization: classifyoutput4.jpg- Modin Wang