This project implements a high-performance Canny Edge Detector in C++ from scratch, utilizing Pthreads for row-based parallel processing. It demonstrates manual implementation of core computer vision concepts—including Gaussian blur, gradient computation (Sobel), non-maximum suppression, and double thresholding—without relying on external libraries for the algorithms themselves. OpenCV is used strictly for reading and writing image files.
Download the latest release from the GitHub Releases page:
- Download the
canny-edge-detector-vX.X.X-linux-x64.tar.gzfile - Extract the archive:
tar -xzf canny-edge-detector-vX.X.X-linux-x64.tar.gz
- Make sure you have OpenCV installed:
# Ubuntu/Debian sudo apt install libopencv-dev - Run the executables:
./canny ./benchmark
If you prefer to build from source, follow the build instructions below.
Before building, ensure you have the following installed:
# Ubuntu/Debian
sudo apt update
sudo apt install cmake libopencv-dev build-essentialgit clone <repository-url>
cd canny-edge-detectormkdir build
cd buildcmake ..make -j4This will generate two executables:
canny- Main edge detection programbenchmark- Performance benchmarking tool
./cannyThis processes ../images/Sukuna.jpg and saves the result to ../images/SukunaCanny.jpg.
./canny <num_threads>Examples:
./canny 1 # Single thread
./canny 4 # 4 threads
./canny 6 # 6 threads (maximum recommended)./canny <num_threads> <input_image> <output_image>Examples:
./canny 6 ../images/Sukuna.jpg ../images/output.jpg
./canny 4 /path/to/input.jpg /path/to/output.jpgRun the benchmark tool to measure performance across different thread counts:
./benchmarkThis will output three tables:
Shows total execution time and speedup for 1-6 threads.
Shows timing breakdown for each function:
- Gaussian blur
- RGB to Grayscale conversion
- Canny filter
Tests performance with different noise levels:
sukuna_noise0.jpg(original)Sukuna_noise1_gauss15.jpg(Gaussian noise σ=15)Sukuna_noise2_gauss30.jpg(Gaussian noise σ=30)
Sample Output:
================================================================================
Table 1: Overall Performance
================================================================================
Threads Average time (ms) Speedup (= T1 / Tn)
--------------------------------------------------------------------------------
1 2030.00 1.00
2 965.00 2.10
3 670.00 3.03
4 540.00 3.76
5 460.00 4.41
6 410.00 4.95
================================================================================
canny-edge-detector/
├── CMakeLists.txt # Build configuration
├── README.md # This file
├── canny.h # Original header (serial version)
├── canny.cpp # Original implementation (serial version)
├── canny_parallel.h # Parallel version header
├── canny_parallel.cpp # Pthread parallel implementation
├── main.cpp # Main program entry point
├── benchmark.cpp # Benchmarking tool
├── images/
│ ├── Sukuna.jpg # Sample input image
│ └── SukunaCanny.jpg # Sample output image
└── build/ # Build directory (created after cmake)
├── canny # Main executable
└── benchmark # Benchmark executable
The Canny edge detection algorithm consists of the following steps:
- Gaussian Blur - Reduces noise using a 5x5 Gaussian kernel
- Grayscale Conversion - Converts RGB to grayscale
- Sobel Filter - Computes gradient magnitude and direction
- Non-Maximum Suppression - Thins edges to 1-pixel width
- Double Thresholding - Classifies edges as strong, weak, or non-edges
- Hysteresis - Connects weak edges to strong edges
The implementation uses row-based parallelization with pthread:
| Function | Strategy | Scalability |
|---|---|---|
| Gaussian Blur | Row partitioning | Excellent (near-linear) |
| RGB to Grayscale | Row partitioning | Good |
| Canny Filter | Phase-based parallel | Moderate (has sequential phase) |
You can adjust edge detection sensitivity in main.cpp:
double lowerThreshold = 0.03; // Lower threshold ratio
double higherThreshold = 0.1; // Higher threshold ratio- Lower values → More edges detected
- Higher values → Fewer edges detected
This project is open source. Feel free to use and modify as needed.