Contour Tracing
Time limit1sMemory limit128 MB
Trace each 8-connected object's boundary with the Moore tracking algorithm and print the contour length, ignoring objects smaller than 5 pixels.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Graph, Array
- Solved
- No attempts yet
Problem
In computer vision, objects of interest are often represented as regions of 1s in a binary image (bitmap). An important step in identifying such objects is tracing the contour (also called the border or boundary) of an object.
Assume the bitmap contains no 1s on its outer border. To trace the contour of a single object, use the Moore boundary tracking algorithm:
- Scan the bitmap from the top row to the bottom row, and left to right within each row, until an object pixel is found. Call this pixel , and let be its west (left) background neighbour.
- Examine the 8 neighbours of , starting at and proceeding clockwise. Let be the first object pixel encountered, and let be the background neighbour examined immediately before . Append and to the contour.
- Set and .
- Examine the 8 neighbours of , starting at and proceeding clockwise; call them . Let be the first object pixel in this sequence.
- Set and , and append to the contour.
- Repeat steps 4 and 5 until and the next contour pixel found is . This final pair repeats the start and must not be appended again.
You are given a bitmap with at most 200 rows and 200 columns containing several objects. For each object, determine the length of its contour (the number of pixels in the traced contour) using the procedure above.
A pixel value of 0 is background and 1 is an object pixel. Two object pixels belong to the same object if they are joined by a path of object pixels moving in any of the 8 compass directions (8-connectivity). The border of the bitmap (first row, last row, first column, last column) is always background. Any object with fewer than 5 pixels is treated as noise and ignored. No object has holes; equivalently, every pair of background pixels is joined by a path of background pixels using only the 4 main compass directions (N, S, E, W).
Input
The input contains several test cases. Each case begins with a line holding two positive integers and : the number of rows and the number of columns of the bitmap. The next lines each contain a string of characters, each 0 or 1. The input ends with a case where ; this last case is not processed.
Output
For each case, print the case number on its own line, formatted as Case k. On the next line, print the contour lengths of all objects in the bitmap, sorted in ascending order and separated by single spaces. If the bitmap contains no object with at least 5 pixels, print no objects found on that line instead.