Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Edge Detection | Image Processing with OpenCV
Computer Vision Essentials

bookEdge Detection

Edge Detection

Edges represent sudden changes in pixel intensity, which usually correspond to object boundaries. Detecting edges helps in shape recognition and segmentation.

Sobel Edge Detection

The Sobel operator calculates gradients (changes in intensity) in both the X and Y directions, helping detect horizontal and vertical edges.

# Convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Apply Sobel filter
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)  # Detects vertical edges
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)  # Detects horizontal edges
sobel_combined = cv2.magnitude(sobel_x, sobel_y)  # Combines both directions
Note
Note

Key Parameters:

  • src: input image (must be grayscale);
  • ddepth: depth of the output image (e.g., cv2.CV_64F);
  • dx: order of the derivative in the X direction (set 1 for horizontal edges);
  • dy: order of the derivative in the Y direction (set 1 for vertical edges);
  • ksize: kernel size (must be odd, e.g., 3, 5, 7).

Canny Edge Detection

The Canny Edge Detector is a multi-stage algorithm that provides more accurate edges by:

  1. Applying Gaussian blur to remove noise.
  2. Finding intensity gradients using Sobel filters.
  3. Suppressing weak edges.
  4. Using double thresholding and edge tracking.
# Apply Canny Edge Detector 
canny_image = cv2.Canny(image, threshold1, threshold2, apertureSize, L2gradient)  
Note
Note
  • image: input grayscale image;
  • threshold1: lower threshold for edge detection (e.g., 50);
  • threshold2: upper threshold for edge detection (e.g., 150);
  • apertureSize (optional): size of the Sobel kernel (default: 3, must be odd);
  • L2gradient (optional): use more accurate L2 norm gradient calculation (default: False).

A comparison of edge detection methods:

edge detect compared
Task

Swipe to start coding

You are given an image:

  • Convert image to grayscale and store in gray_image;
  • Apply Sobel filter on X and Y directions (output depth cv2.CV_64F and kernel size 3) and store in sobel_x, sobel_y accordingly;
  • Combine Sobel-filtered directions in sobel_img;
  • Apply a Canny filter with a threshold from 200 to 300 and store in canny_img.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 3.45

bookEdge Detection

Swipe to show menu

Edge Detection

Edges represent sudden changes in pixel intensity, which usually correspond to object boundaries. Detecting edges helps in shape recognition and segmentation.

Sobel Edge Detection

The Sobel operator calculates gradients (changes in intensity) in both the X and Y directions, helping detect horizontal and vertical edges.

# Convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Apply Sobel filter
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)  # Detects vertical edges
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)  # Detects horizontal edges
sobel_combined = cv2.magnitude(sobel_x, sobel_y)  # Combines both directions
Note
Note

Key Parameters:

  • src: input image (must be grayscale);
  • ddepth: depth of the output image (e.g., cv2.CV_64F);
  • dx: order of the derivative in the X direction (set 1 for horizontal edges);
  • dy: order of the derivative in the Y direction (set 1 for vertical edges);
  • ksize: kernel size (must be odd, e.g., 3, 5, 7).

Canny Edge Detection

The Canny Edge Detector is a multi-stage algorithm that provides more accurate edges by:

  1. Applying Gaussian blur to remove noise.
  2. Finding intensity gradients using Sobel filters.
  3. Suppressing weak edges.
  4. Using double thresholding and edge tracking.
# Apply Canny Edge Detector 
canny_image = cv2.Canny(image, threshold1, threshold2, apertureSize, L2gradient)  
Note
Note
  • image: input grayscale image;
  • threshold1: lower threshold for edge detection (e.g., 50);
  • threshold2: upper threshold for edge detection (e.g., 150);
  • apertureSize (optional): size of the Sobel kernel (default: 3, must be odd);
  • L2gradient (optional): use more accurate L2 norm gradient calculation (default: False).

A comparison of edge detection methods:

edge detect compared
Task

Swipe to start coding

You are given an image:

  • Convert image to grayscale and store in gray_image;
  • Apply Sobel filter on X and Y directions (output depth cv2.CV_64F and kernel size 3) and store in sobel_x, sobel_y accordingly;
  • Combine Sobel-filtered directions in sobel_img;
  • Apply a Canny filter with a threshold from 200 to 300 and store in canny_img.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 7
single

single

some-alt