Skip to main content
All questions
EasyArrays · Simulation

2D Edge Detection in a Flattened Image

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

0

Given a grayscale image stored as a 1D array, determine which pixels are edge pixels.

The image has width w and height h = len(image) // w. Each consecutive block of w values represents one row.

For each pixel, compare its value to each valid adjacent neighbor in the 8-connected grid: up, down, left, right, and the four diagonals. If the absolute difference between the pixel and any valid neighbor is greater than the threshold x, mark that pixel as an edge pixel.

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

Assume the input image length is always a multiple of w.

Examples:

  1. image = [10, 10, 10, 10, 10, 10], w = 3, x = 0 -> no adjacent differences exceed 0, so the result is [0, 0, 0, 0, 0, 0].

  2. image = [1, 1, 1, 1, 9, 1], w = 3, x = 3 -> all pixels are edge pixels because each one has at least one adjacent neighbor that differs by more than 3, so the result is [1, 1, 1, 1, 1, 1].

Write a function that solves this problem.