Draft:Edge Detection Algorithms

Introduction To Canny Edge Detection Algorithm

The Canny Edge Detection Algorithm introduced by John F. Canny in 1986, Canny Edge Detection Algorithm is a multi stage algorithm. Edge detection is a important for image Segmentation, Since many image processing algorithms are first required to identify the object and then process them.In the algorithm their are Multi stage is given below.

1. Noice Reduction

2. Gradient Calculation

3. Non-maximum Suppression

4. Double Thresholding and Edge

5. Edge Tracking by Hysteresis

Steps To Perform Canny Edge Detection

1. Start

2. Load a Color Image

3. Convert to a Binary Image

4. Apply Canny Edge Detection Algorithm

5. End

Reference of Flowchart of Canny Edge Detection  [https://stackoverflow.com/questions/18194870/canny-edge-image-noise-removal '''1. Noice Reduction'''] This is initial step of Canny Edge Detection Algorithm. The primary objective of Canny Edge detection algorithm is Noice Reduction. The Noice Reduction work within the Canny Edge Detection Algorithm is given below

Gaussian Blur :The first step in noise reduction is to convolve the input image with a Gaussian filter kernel.

Impact on Noise :Gaussian blur effectively reduces high-frequency components in the image, which are typically associated with noise.

Computational Efficiency : In practical implementations of the Canny algorithm, Gaussian blur is often performed using a separable filter to improve computational efficiency.

Integration with Edge Detection:After noise reduction, the smoothed image is then passed through subsequent stages of the Canny algorithm

Parameter Tuning :The effectiveness of noise reduction in the Canny algorithm depends on the choice of parameters, such as the standard deviation of the Gaussian kernel (which determines the amount of smoothing) and the threshold values used for edge detection.

[https://medium.com/jun94-devpblog/cv-5-canny-edge-detector-with-image-gradients-a42f07dc69c '''2. Gradient Calculation'''] Gradient calculation plays a crucial role in the Canny edge detection algorithm by identifying areas of significant intensity change, which serve as potential edge candidates for further processing in subsequent stages of the algorithm. The Gradient Calculation work within the Canny Edge Detection Algorithm is given below

Gradient Approximation: The first step in gradient calculation involves approximating the gradients of the image using edge detection filters, such as the Sobel or Prewitt operators.

Sobel Operator: One commonly used edge detection filter is the Sobel operator, which consists of two separate 3x3 convolution kernels: one for horizontal changes and the other for vertical changes.

Gradient Magnitude: Once the horizontal and vertical gradients are computed, the gradient magnitude at each pixel can be obtained using the formula

Gradient Direction: The gradient direction at each pixel is calculated using the formula

Edge Strength and Orientation: The resulting gradient magnitude and direction provide valuable information about the strength and orientation of edges in the image.

Normalization and Scaling: It's common practice to normalize the gradient magnitude to ensure that it falls within a specific range (typically 0 to 255) Gradient Maps: The computed gradient magnitude and direction can be visualized as gradient magnitude maps and gradient orientation maps.

[https://learnopencv.com/non-maximum-suppression-theory-and-implementation-in-pytorch/ '''3. Non-maximum Suppression'''] non-maximum suppression is a crucial step in the Canny edge detection algorithm, as it refines the edge map obtained from gradient calculation by retaining only the most relevant edge responses and discarding non-maximal responses. The Non-Maximum Suppression work within the Canny Edge Detection Algorithm is given below

Gradient Magnitude and Direction: After computing the gradient magnitude and direction using techniques like Sobel or Prewitt operators, each pixel in the image has associated gradient strength and direction.

Local Maximum Detection: For each pixel in the image, non-maximum suppression checks if the gradient magnitude at that pixel is a local maximum along the direction of the gradient.

Suppression of Non-Maximum Pixels: If a pixel's gradient magnitude is not greater than its neighbors along the gradient direction, it is suppressed.

Resulting Edge Thinning: By applying non-maximum suppression, the edges in the image are thinned to a one-pixel width, enhancing the precision of edge detection.

Edge Response Preservation: Non-maximum suppression helps preserve only the sharpest and most prominent edges.

Directional Filtering: The suppression process is typically performed along the gradient direction to ensure that only edges with the steepest intensity changes are retained.

[https://justin-liang.com/tutorials/canny/ '''4. Double Thresholding and Edge'''] Double thresholding and edge tracking by hysteresis play a crucial role in the Canny edge detection algorithm by effectively distinguishing between strong, weak, and non-edge pixels and connecting weak edges to form continuous edge contours. The Double Thresholding and edge work within the Canny Edge Detection Algorithm is given below

Thresholding: In this step, the gradient magnitudes obtained from the previous stages are compared against two user-defined thresholds: a high threshold (T_high) and a low threshold (T_low).

Strong Edge Pixels: Any pixel with a gradient magnitude greater than or equal to the high threshold (T_high) is classified as a strong edge pixel.

Weak Edge Pixels: Pixels with gradient magnitudes between the low threshold (T_low) and the high threshold (T_high) are classified as weak edge pixels.

Non-Edge Pixels: Pixels with gradient magnitudes below the low threshold (T_low) are classified as non-edge pixels and are discarded from further consideration.

Edge Tracking by Hysteresis: After thresholding, weak edge pixels are subjected to a process called edge tracking by hysteresis.

Connected Component Analysis: Edge tracking involves examining neighboring pixels of weak edge pixels to determine if they are connected to strong edge pixels.

Threshold Selection: The choice of high and low thresholds is critical and depends on factors such as image noise level and edge detection requirements.

Adjustment and Optimization: The selection of threshold values often involves empirical tuning and optimization based on the specific characteristics of the input images and the desired edge detection performance.

[https://www.youtube.com/watch?v=B-b6mWww2xo '''5. Edge Tracking by Hysteresis'''] Edge tracking by hysteresis is a crucial component of the Canny edge detection algorithm, enabling the formation of continuous edge contours by connecting weak edge pixels to strong edge pixels based on a hysteresis-based thresholding logic. The Edge Tracking By Hysteresis work within the Canny Edge Detection Algorithm is given below

Weak Edge Pixel Selection: In the previous step of double thresholding, weak edge pixels were identified based on their gradient magnitudes falling between the low and high thresholds.

Initialization: Edge tracking begins by selecting a starting point, typically a strong edge pixel with a gradient magnitude exceeding the high threshold.Edge Following: From the initial seed pixel, edge tracking proceeds by iteratively examining neighboring pixels to determine if they should be included as part of the edge.

Thresholding Logic: The inclusion of weak edge pixels is governed by a concept of hysteresis, which involves two thresholds: a high threshold (T_high) and a low threshold (T_low).

Continuity Preservation: By incorporating hysteresis-based thresholding, edge tracking ensures the continuity of edges by allowing weak edge pixels to be included if they are part of a continuous edge structure

Recursive Exploration: Edge tracking continues recursively, exploring neighboring pixels of included weak edge pixels to identify additional edge pixels.

Termination: Edge tracking terminates when no more weak edge pixels satisfying the threshold conditions are found in the neighborhood of the currently tracked edge.

Output: The result of edge tracking by hysteresis is a set of connected edge segments, comprising both strong and weak edge pixels The OpenCv code of Canny Edge detection Algorithm in Python

import cv2

import numpy as np

image = cv2.imread(test_img)

image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

gradient_sobelx = cv2.Sebel(image, -1,-1,0)

gradient_sobely = cv2.Sebel(image, -1,0,1)

gradient_sobelxy = cv2.addWeighted(gradient_sobelx, 0.5 , gradients_sobely , 0.5,0)

gradients_laplacian=cv2.Laplacian(image, -10)

canny_output = cv2.Canny(image, 80,150)

cv2.imshow('Sobel x' gradients_sobelx)

cv2.imshow('Sobel y' gradients_sobely)

cv2.imshow('Sobel x+y' gradients_sobelxy)

cv2.imshow('laplacian' gradients_laplacian)

cv2.imshow('canny' canny_output)

cv2.waitKey