Canny Edge Detector
View on Github ~~>Canny Edge Detector
Description
Created a custom Canny Edge Detector that, given an input image, a value for sigma, and values for high and low thresholds, outputs the edges of the image.
Algorithm
The Canny Edge Detector is a multi-step algorithm that takes an input image and outputs the edges of the image. The algorithm is as follows:
- Generate Gaussian Mask: generate a gaussian mask using an input sigma.
- Generate Gaussian Derivative: generate a 1D gaussian derivative from the original gaussian mask.
- Convolve: convolve the image in both directions with the gaussian mask.
- Convolve Prime: convolve the x and y outputs from Convolve step with the gaussian derivative mask.
- Compute Magnitude: computes the magnitude and phase (theta) of an image using the x and y derivatives.
- Non-max suppression: suppresses non-maximal values given input phase (theta) and magnitude of an image.
- Hysteresis Thresholding: compute threshold and hysteresis given input non-maximum suppression image and lower and upper threshold ratios.
Setup
- Add the image you want to generate edges for to the ‘Canny Edge Detector/’ folder
- Open the run_canny_detector.py file and update the im_name parameter
- Update all other parameters as described below
- Run the algorithm using run_canny_detector.py
Output files will be im_name_step where step is the output of each step of the detector as described above.
Parameters
im_name
The name of the image you want to generate edges for. The image must be in the same folder as the run_canny_detector.py file.
sigma
The sigma value for the gaussian mask. The higher the sigma, the more blurred the image will be.
low_thresh_ratio
The ratio of the low threshold to the high threshold. The lower the ratio, the more edges will be detected.
high_thresh_ratio
The ratio of the high threshold to the low threshold. The higher the ratio, the more edges will be detected.
show_steps
A boolean value that determines whether or not to show the output of each step of the algorithm.
save_steps
A boolean value that determines whether or not to save the output of each step of the algorithm.
Example
Parameters
im_name = '42049.jpg'
sigma = 1
low_thresh_ratio = 0.1
high_thresh_ratio = 0.5
show_steps = True
save_steps = True
Results
Original Image
Convolved Image
Convolved Prime
Magnitude
Non-max Suppression
Final Output
Future Work
- Add a GUI to allow for easier parameter tuning
- Add a function to automatically tune parameters
- Add a function to automatically detect edges in multiple images
- Add a function to automatically detect edges in a video
- Add a function to automatically detect edges in a live video feed