Skip to main content
All questions
EasyArrays · Matrix Traversal

Detect Edge Pixels in a 2D Image

Written by a community member · · 1 attempt · 0% pass

0

Given a grayscale image encoded as a 1D list of integers, determine which pixels are edge pixels.

The image has width w; its height can be inferred from len(image) // w. Each consecutive group of w values represents one row.

For each pixel, compare its value to each existing 4-directional neighbor (up, down, left, right). If the absolute difference between the pixel and at least one adjacent neighbor is greater than the threshold x, that pixel is an edge pixel.

Return a 1D list of the same length as image, containing 1 for edge pixels and 0 for non-edge pixels.

Assume the input always represents a complete rectangular image and w > 0.

Examples:

  • image = [1, 1, 1, 1], w = 2, x = 0 -> [0, 0, 0, 0]
  • image = [1, 1, 1, 10], w = 2, x = 3 -> [0, 1, 1, 1]

Explanation for the second example: the bottom-right pixel differs from its left and top neighbors by more than 3, so it is an edge pixel; its adjacent neighbors are also edge pixels because they differ from 10 by more than 3.