Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Edge Detection | Image Processing with OpenCV
/
Applied Computer Vision
セクション 2.  7
single

single

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
タスク

スワイプしてコーディングを開始

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.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  7
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt