Moving Object Recognition

Time limit1sMemory limit128 MB

Summary
Find the largest connected white blob in each image, track its centroid over time, and report the average per-second velocity in x and y, each to two decimals.
Level

Medium7 of 10

Topics
BFS, Simulation, Implementation, Geometry
Solved
No attempts yet

Problem

There are many situations in which we need to know how fast an object is moving. For example, an air-traffic controller at an airport wants to know the speed of a descending plane and warn if it is too fast or too slow, speed cameras help the police catch speeding drivers, and so on.

In this problem you must write a program that recognizes the speed of a moving object.

Assume an object moves on a two-dimensional plane with a constant speed and direction. A camera takes one shot every second. Assume the camera records the true scene of the plane (you do not need to worry about the viewing angle).

The background is black and the only object is white, except for some noise introduced by the camera. You may assume that the largest continuous white area is always the object, and there is always exactly one such largest area.

Given several pictures taken by the camera, compute and output the speed of the object.

A picture is a matrix containing only . or x, where . is a black block and x is a white block. There are always at least two pictures.

Definitions

The speed of the object is the moving speed of the object's geometric center, defined as

(∫(x,y)∈Objectx dx dy∫(x,y)∈Objectdx dy, ∫(x,y)∈Objecty dx dy∫(x,y)∈Objectdx dy)\left( \dfrac{\int_{(x,y)\in \text{Object}} x\,dx\,dy}{\int_{(x,y)\in \text{Object}} dx\,dy},\ \dfrac{\int_{(x,y)\in \text{Object}} y\,dx\,dy}{\int_{(x,y)\in \text{Object}} dx\,dy} \right)

Every object is made of unit square blocks, and the geometric center of a single block is the block's own center, so the geometric center of the object can be computed as

(∑i∈ObjectX[i]N, ∑i∈ObjectY[i]N)\left( \dfrac{\sum_{i \in \text{Object}} X[i]}{N},\ \dfrac{\sum_{i \in \text{Object}} Y[i]}{N} \right)

where (X[i],Y[i])(X[i], Y[i]) is the coordinate of the center of the ii-th block and NN is the number of blocks in the object.

The average speed is computed as in a physics textbook:

AvgSpeed=∑t=0T−1pos(t+T)−pos(t)TT\text{AvgSpeed} = \dfrac{\sum_{t=0}^{T-1} \dfrac{\text{pos}(t+T) - \text{pos}(t)}{T}}{T}

where tt ranges over the discrete observation times from 00 to T−1T-1. Here TT is half of the number of observation points (the number of observation points is always even), and pos(t)\text{pos}(t) is the observed position of the object's center at time tt, that is, the geometric center computed from the tt-th image.

Other definitions:

  • Each block is a 1 mm×1 mm1\text{ mm} \times 1\text{ mm} square (every . or x is one such block).
  • A white area is a set of white blocks.
  • A white area is a continuous white area if and only if any two of its blocks are joined by a path that stays inside the area and whose consecutive blocks share a common edge (4-directional adjacency).
  • The positive XX direction is from left to right; the positive YY direction is from top to bottom.
  • The largest continuous white area is always the object, and it is always unique.
  • The object may appear with a different shape in different pictures.

Input

The input contains several data cases. Each case begins with a line containing two integers mm and kk. It is followed by several m×km \times k matrices (mm columns, kk rows); each matrix is one picture taken by the camera, and there are always at least two pictures.

Within one case, consecutive matrices are separated by a line of mm - characters. The last matrix of a case is followed by a line of mm = characters.

A line containing two zeros marks the end of the input.

Each picture is at most 256×256256 \times 256, and one case contains at most 256256 pictures.

Output

For each test case, output one line with two numbers: the speed of the object in the XX and YY directions respectively, each printed to exactly two digits after the decimal point. Speeds are in mm/s.

Examples3

  1. Example 1

    Input
    10 5
    .........x
    .....xxx.x
    ....xxx...
    .....xxx..
    x.........
    ----------
    .........x
    .........x
    ...xxx....
    ..xxx.....
    x..xxx....
    ==========
    0 0
    
    Expected output
    -2.00 1.00
    
  2. Example 2

    Input
    6 3
    xxx...
    xxx...
    xxx...
    ------
    ...xxx
    ...xxx
    ...xxx
    ======
    0 0
    
    Expected output
    3.00 0.00
    
  3. Example 3

    Input
    5 5
    .xx..
    .xx..
    .....
    .....
    ....x
    -----
    ....x
    .....
    .xx..
    .xx..
    .....
    =====
    0 0
    
    Expected output
    0.00 2.00