Loading...
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).
image = [[0, 0, 0],
[0, 255, 0],
[0, 0, 0]]{'magnitude': [[..], [..], [..]], 'direction': [[..], [..], [..]]}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°