セクション 2. 章 7
single
Edge 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
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 (set1for horizontal edges);dy: order of the derivative in the Y direction (set1for 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:
- Applying Gaussian blur to remove noise.
- Finding intensity gradients using Sobel filters.
- Suppressing weak edges.
- Using double thresholding and edge tracking.
# Apply Canny Edge Detector
canny_image = cv2.Canny(image, threshold1, threshold2, apertureSize, L2gradient)
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:
タスク
スワイプしてコーディングを開始
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_64Fand kernel size3) and store insobel_x,sobel_yaccordingly; - Combine Sobel-filtered directions in
sobel_img; - Apply a Canny filter with a threshold from
200to300and store incanny_img.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 2. 章 7
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください