Contour Tracing

Time limit1sMemory limit128 MB

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

  1. 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 b0b_0, and let c0c_0 be its west (left) background neighbour.
  2. Examine the 8 neighbours of b0b_0, starting at c0c_0 and proceeding clockwise. Let b1b_1 be the first object pixel encountered, and let c1c_1 be the background neighbour examined immediately before b1b_1. Append b0b_0 and b1b_1 to the contour.
  3. Set b=b1b = b_1 and c=c1c = c_1.
  4. Examine the 8 neighbours of bb, starting at cc and proceeding clockwise; call them n1,n2,…,n8n_1, n_2, \dots, n_8. Let nkn_k be the first object pixel in this sequence.
  5. Set b=nkb = n_k and c=nk−1c = n_{k-1}, and append bb to the contour.
  6. Repeat steps 4 and 5 until b=b0b = b_0 and the next contour pixel found is b1b_1. This final pair b0,b1b_0, b_1 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 RR and CC: the number of rows and the number of columns of the bitmap. The next RR lines each contain a string of CC characters, each 0 or 1. The input ends with a case where R=C=0R = C = 0; 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.

Examples3

  1. Example 1

    Input
    7 7
    0000000
    0011110
    0111100
    0011100
    0111100
    0111100
    0000000
    16 7
    0000000
    0011110
    0111100
    0011100
    0111100
    0111100
    0000000
    0011000
    0100110
    0000000
    0001000
    0010100
    0010000
    0111000
    0111000
    0000000
    4 4
    0000
    0000
    0010
    0000
    0 0
    
    Expected output
    Case 1
    14
    Case 2
    8 12 14
    Case 3
    no objects found
    
  2. Example 2

    Input
    5 5
    00000
    01110
    01110
    01110
    00000
    0 0
    
    Expected output
    Case 1
    8
    
  3. Example 3

    Input
    3 7
    0000000
    0111110
    0000000
    0 0
    
    Expected output
    Case 1
    8