views:

420

answers:

3

Problem: I have two overlapping 2D shapes, A and B, each shape having the same number of pixels, but differing in shape. Some portion of the shapes are overlapping, and there are some pieces of each that are not overlapping. My goal is to move all the non-overlapping pixels in shape A to the non-overlapping pixels in shape B. Since the number of pixels in each shape is the same, I should be able to find a 1-to-1 mapping of pixels. The restriction is that I want to find the mapping that minimizes the total distance traveled by all the pixels that moved.

Brute Force: The brute force approach to solving this problem is obviously out of the question, since I would have to compute the total distance of all possible mappings of which I think there are n! (where n is the number of non-overlapping pixels in one shape) times the computation of calculating a distance for each pair of points in the mapping, n, giving a total of O( n * n! ) or something similar.

Backtracking: The only "better" solution I could think of was to use backtracking, where I would keep track of the current minimum so far and at any point when I'm evaluating a certain mapping, if I reach or exceed that minimum, I move on to the next mapping. Even this won't do any better than O( n! ).

Is there any way to solve this problem with a reasonable complexity?

Also note that the "obvious" approach of simply mapping a point to it's closest matching neighbour does not always yield the optimum solution.

Simpler Approach?: As a secondary question, if a feasible solution doesn't exist, one possibility might be to partition each non-overlapping section into small regions, and map these regions, greatly reducing the number of mappings. To calculate the distance between two regions I would use the center of mass (average of the pixel locations in the region). However, this presents the problem of how I should go about doing the partitioning in order to get a near-optimal answer.

Any ideas are appreciated!!

+4  A: 

You might consider simulated annealing for this. Start off by assigning A[x] -> B[y] for each pixel, randomly, and calculate the sum of squared distances. Then swap a pair of x<->y mappings, randomly. Then choose to accept this with probability Q, where Q is higher if the new mapping is better, and tends towards zero over time. See the wikipedia article for a better explanation.

FryGuy
A: 
  1. Sort pixels in shape A: in increasing order of 'x' and then 'y' ordinates
  2. Sort pixels in shape B: in decreasing order of 'x' and then increasing 'y'

Map pixels at the same index: in the sorted list the first pixel in A will map to first pixel in B. Is this not the mapping you are looking for?

Sesh
Not exactly, take this example where I sort as you say..A: (0,0), (1,0)B: (1,1), (0,1)In this example my total distance is 2 * sqrt(2) since it is sqrt(2) distance from (0,0) to (1,1) and from (1,0) to (0,1), but the optimal total distance is 2, (0,0) to (0,1) and (1,0) to (1,1).
MahlerFive
+6  A: 

This is the Minimum Matching problem, and you are correct that it is a hard problem in general. However for the 2D Euclidean Bipartite Minimum Matching case it is solvable in close to O(n²) (see link).

For fast approximations, FryGuy is on the right track with Simulated Annealing. This is one approach.

Also take a look at Approximation algorithms for bipartite and non-bipartite matching in the plane for a O((n/ε)^1.5*log^5(n)) (1+ε)-randomized approximation scheme.

Imran