Edge Detection
Time limit1sMemory limit128 MB
Given an image as run-length encoded runs, set each output pixel to the largest absolute difference from its 8 neighbors, and emit the result as runs.
- Level
Hard8 of 10
- Topics
- Simulation, Implementation, Intervals, Array
- Solved
- No attempts yet
Problem
A satellite-imaging company records and stores very large images using run-length encoding. Write a program that reads a compressed image, detects the edges in it (as described below), and outputs another compressed image of the detected edges.
A simple edge-detection algorithm sets each output pixel to the maximum absolute difference between that pixel and all of its surrounding pixels in the input image. Consider the input image below:
Input image
Output image
The upper-left pixel of the output image is the maximum of , , and , which is .
The pixel in the 4th row, 2nd column is the maximum of , , , , , , , and , which is .
Every image contains between and pixels. Every image is encoded with run-length encoding (RLE): a sequence of pairs, each holding a pixel value (-) and a run length (-). An input image has at most such pairs. Consecutive pairs always have different pixel values. Every line of an image contains the same number of pixels.
Input
The input describes one or more images. Each image begins with the width, in pixels, of every image line, on its own line. This is followed by the RLE pairs, one pair (value length) per line. A line containing 0 0 marks the end of that image's data. A width of 0 indicates that there are no more images to process.
Output
For each image, in order, output its edge-detected result in the same format as an input image: the image width on its own line, then the RLE pairs (value length) one per line, and finally a line containing 0 0. The result may contain more than RLE pairs. Do not print a trailing width-0 line.
Hint
A brute-force solution that computes an output value for every individual pixel will likely exceed the time or memory limits.