This project is a Python implementation of the Canny Edge Detection algorithm, developed as part of CAP 5415 – Computer Vision (Programming Assignment 1).
The program follows the standard Canny edge detection pipeline:
- Grayscale conversion – load an image and represent it as a NumPy matrix.
- Gaussian smoothing – build a 1D Gaussian kernel and compute its derivative.
- Convolution – apply the kernels separately along the x and y axes.
- Gradient calculation – compute gradient magnitude and direction.
- Non-maximum suppression – thin edges to one-pixel width.
- Hysteresis thresholding – use dual thresholds and connectivity analysis to keep only meaningful edges.
Implemented from scratch (except for utility functions like connected components from OpenCV) to understand the steps behind Canny instead of relying on a single built-in function.
Requirements
- Python 3.12+
- Poetry (recommended)
Install dependencies (Poetry):
poetry installIf Poetry fails, install manually (pip):
pip install opencv-python matplotlib numpyRun the main program:
poetry run python -m src.pa1.mainSteps:
- Place input images in
images/images/train/. - Edit
src/pa1/main.pyto set the image name and parameters (kernel_size,sigma,low_thresh,high_thresh). - Outputs will be saved to
images/output/with parameterized subfolders. - To view results from
.npyarrays:You will be prompted to enter the output folder name (e.g.,poetry run python -m src.pa1.openNPYFiles
513531_5_1_5_10), then results are displayed one by one.
- Changing sigma affects edge clarity:
- Higher sigma → more blur, softer gradients.
- Lower sigma (e.g.,
1) → sharper, more defined edges.
- The combination of low/high thresholds controls edge continuity.
Example results are included in the report PDF (see /docs).
Re-implementing Canny gave me a deeper appreciation for how much work underlies “simple” functions in computer vision libraries. It showed the role of parameters in balancing sensitivity and noise, and why Canny remains a robust edge detector decades after its introduction.