Canny Edge Detector

Canny Edge Detector

View on Github ~~>
computer vision
machine learning

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:

  1. Generate Gaussian Mask: generate a gaussian mask using an input sigma.
  2. Generate Gaussian Derivative: generate a 1D gaussian derivative from the original gaussian mask.
  3. Convolve: convolve the image in both directions with the gaussian mask.
  4. Convolve Prime: convolve the x and y outputs from Convolve step with the gaussian derivative mask.
  5. Compute Magnitude: computes the magnitude and phase (theta) of an image using the x and y derivatives.
  6. Non-max suppression: suppresses non-maximal values given input phase (theta) and magnitude of an image.
  7. Hysteresis Thresholding: compute threshold and hysteresis given input non-maximum suppression image and lower and upper threshold ratios.

Setup

  1. Add the image you want to generate edges for to the ‘Canny Edge Detector/’ folder
  2. Open the run_canny_detector.py file and update the im_name parameter
  3. Update all other parameters as described below
  4. 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

Original Image

Convolved Image

Convolved Image

Convolved Prime

Convolved Prime

Magnitude

Magnitude

Non-max Suppression

Non-max Suppression

Final Output

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

References

© 2024 Joshua Gracie
Thanks for stopping by! Don't forget to check out my LinkedIn 💼 and TryHackMe