Given two binary matrices of the same size with the same element counts, how can we find a map mapping ones to ones such that the sum of differences of distances of all pairs of neighbors is minimised, where the distance of each cell is the number of steps it needs to walk horizontally, vertically or diagonally to reach where the mapping sent it?
For example, given $A= \begin{bmatrix} 1& 1 & 1\\ 1& 0& 0\\ 0& 0& 0 \end{bmatrix} B= \begin{bmatrix} 0& 0 & 0\\ 0& 0& 1\\ 1& 1& 1 \end{bmatrix} $
The function $\psi$ mapping the ones of $A$ to the ones of $B$ by $\psi(1,1) = (3,1), \psi(1,2) = (3,2),\psi(1,3) = (3,3), \psi(2,1) = (2,3)$ gives all the ones of $A$ the same distance of 2, which gives the sum of all differences of distances of neighbors the minimal value possible of zero.
How can we generally approach this problem efficiently?
The motivation is creating smooth animation between simple binary black and white images of shapes such as circle and square. Combining this approach with also minimising the total distance covered by the pixels will help finding the path each pixel makes along the frames to make the animation smooth. It is for practical usage having typical input size of about $500 \times 500$ with relatively small amount of black pixels (ones) of about 500.
differences of distances of all pairs of neighbors is minimised
- do you mean that you want to minimize $\sum_{i,j} |d_i - d_j|$, where for each black pixel $b_i$ we have $d_i = dist(b_i, \psi(b_i))$? $\endgroup$