📘
Implement Prewitt Edge Detector
HardEdge Detection
Implement the Prewitt edge detector, one of the earliest edge detection methods (1970).
The Prewitt operator uses two 3×3 kernels to compute horizontal and vertical gradients:
Gx=−1−1−1000111∗I,Gy=−101−101−101∗I
The edge magnitude and direction are: G=Gx2+Gy2,θ=arctan(GxGy)
Return both the magnitude map and the edge direction map (in degrees, 0-360).
Example:
Input:
image = [[0, 0, 0],
[0, 255, 0],
[0, 0, 0]]Output:
{'magnitude': [[..], [..], [..]], 'direction': [[..], [..], [..]]}Reasoning:
Applying Prewitt kernels to center pixel (1,1):
Gx convolution at (1,1): −1(0)+0(0)+1(0)+−1(0)+0(255)+1(0)+−1(0)+0(0)+1(0)=0
Gy convolution at (1,1): −1(0)+−1(0)+−1(0)+0(0)+0(255)+0(0)+1(0)+1(0)+1(0)=0
Center has no edge (surrounded by zeros).
At (1,0): Gx=255, Gy=0 → magnitude = 255, direction = 0°
Constraints:
- image: 2D grayscale array
- Return: Dict with 'magnitude' and 'direction' arrays
- Use zero-padding for borders
- Direction in degrees [0, 360), rounded to 1 decimal
- Magnitude rounded to 2 decimals
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.