PIXELBANKv8.2.1
Menu

Implement Prewitt Edge Detector

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=(101101101)I,Gy=(111000111)IG_x = \begin{pmatrix} -1 & 0 & 1 \\ -1 & 0 & 1 \\ -1 & 0 & 1 \end{pmatrix} * I, \quad G_y = \begin{pmatrix} -1 & -1 & -1 \\ 0 & 0 & 0 \\ 1 & 1 & 1 \end{pmatrix} * I

The edge magnitude and direction are: G=Gx2+Gy2,θ=arctan(GyGx)G = \sqrt{G_x^2 + G_y^2}, \quad \theta = \arctan\left(\frac{G_y}{G_x}\right)

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):

GxG_x convolution at (1,1): 1(0)+0(0)+1(0)+1(0)+0(255)+1(0)+1(0)+0(0)+1(0)=0-1(0) + 0(0) + 1(0) + -1(0) + 0(255) + 1(0) + -1(0) + 0(0) + 1(0) = 0

GyG_y convolution at (1,1): 1(0)+1(0)+1(0)+0(0)+0(255)+0(0)+1(0)+1(0)+1(0)=0-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=255G_x = 255, Gy=0G_y = 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

Test Results

0/0
Run code to see test results.