This page is still under construction.

Parts of this page are still being built. What you see may change.

Open and Close

Time limit1sMemory limit128 MB

Summary
Compute the morphological opening and closing of a binary image by a small structuring element for each test case.
Level

Medium5 of 10

Topics
Implementation, Simulation, Matrix
Solved
No attempts yet

Problem

Morphological operations extract the image components used to represent and describe region shapes. Two common ones are opening and closing. First we fix how images are represented.

A binary image AA with MM rows and NN columns is the set of coordinates (r,c)(r, c) (where 1≤r≤M1 \le r \le M and 1≤c≤N1 \le c \le N) at which the pixel equals 11; the top-left corner is (1,1)(1, 1). A second binary image BB, called the structuring element, has 2S+12S+1 rows and 2S+12S+1 columns and is represented the same way, except that its top-left pixel is at (−S,−S)(-S, -S) (so its center is at (0,0)(0, 0)).

Two primitive operations are dilation and erosion. The dilation of AA by BB is

A (+) B = { a + b | a in A, b in B } ∩ Z

where coordinates are added componentwise and ZZ is the set of (i,j)(i, j) with 1≤i≤M1 \le i \le M and 1≤j≤N1 \le j \le N. The erosion of AA by BB is

A (-) B = { w | w + b in A for every b in B }

Using these, the opening of AA by BB is

A o B = (A (-) B) (+) B

and the closing of AA by BB is

A . B = (A (+) B) (-) B

Intuitively, opening removes small details while preserving the overall shape, and closing fills small gaps while preserving the overall shape.

Input

The input consists of several test cases. Each case begins with a line containing three integers MM, NN, and SS separated by spaces (10≤M,N≤25610 \le M, N \le 256, 1≤S≤41 \le S \le 4). The next MM lines contain the rows of image AA, each written as NN characters that are . (0) or * (1). The following 2S+12S+1 lines describe the structuring element BB in the same way. The input terminates with a line 0 0 0.

Output

For each case, print the case number on its own line as Case x, followed by a blank line. Then print the opening A∘BA \circ B, followed by a blank line, followed by the closing A∙BA \bullet B. Each result image uses the same format as the input images. Separate the output of consecutive cases with a line of exactly 75 equals signs (=); do not print this separator after the last case.

Examples1

  1. Example 1

    Input
    10 12 1
    ............
    ..*.........
    .***...***..
    .***...***..
    .*********..
    .**********.
    .*********..
    .***...*.*..
    ............
    ............
    ***
    ***
    ***
    10 12 1
    ............
    ..*.........
    .***...***..
    .***...***..
    .*********..
    .**********.
    .*********..
    .***...*.*..
    ............
    ............
    .*.
    ***
    .*.
    0 0 0
    
    Expected output
    Case 1
    
    ............
    ............
    .***...***..
    .***...***..
    .*********..
    .*********..
    .*********..
    .***........
    ............
    ............
    
    ............
    ..*.........
    .***...***..
    .***...***..
    .*********..
    .**********.
    .*********..
    .***...***..
    ............
    ............
    ===========================================================================
    Case 2
    
    ............
    ..*.........
    .***....*...
    .***...***..
    .*********..
    .**********.
    .*********..
    ..**...*....
    ............
    ............
    
    ............
    ..*.........
    .***...***..
    .****.****..
    .*********..
    .**********.
    .*********..
    .***...*.*..
    ............
    ............