# Pattern recognition for motion detection

I have two images with multiple dots. If the dots are high-correlated between the two images and the movement of dots can be expressed by an universal rotational and translational motions, is there any way to sort out the rotation center and translational motion by pattern recognition in Mathematica?

• You can try working off of ImageCorrespondingPoints Sep 15, 2017 at 6:13

So this turns out to be a fun little problem. Here's a solution that will probably work for any collection of these black dots under a shape-preserving transformation.

First we extract the dot pixels, group them, and compute their means:

img1 = Import["https://i.stack.imgur.com/CsOMn.png"];
img2 = Import["https://i.stack.imgur.com/8MvRx.png"];

ptGrpFind =
Compile[{
{pts, _Real, 2},
{pt, _Real, 1},
{tol, _Real}
},
Pick[pts, Norm[# - pt] < tol & /@ pts]
];

pixies1 = PixelValuePositions[img1, Black];
pixies2 = PixelValuePositions[img2, Black];

pts1 = Mean /@
DeleteDuplicates[ptGrpFind[pixies1, #, 10] & /@ pixies1];
pts2 = Mean /@
DeleteDuplicates[ptGrpFind[pixies2, #, 10] & /@ pixies2];

GraphicsRow@{Graphics[Point@pts1], Graphics[Point@pts2]}


Then we find the top-left and top-right points and use FindGeometricTransformation on them:

topPts[pts_] :=
With[{bbox = CoordinateBounds[pts]},
First /@
{
MinimalBy[pts, Norm[{bbox[[1, 1]], bbox[[2, 2]]} - #] &],
MinimalBy[pts, Norm[{bbox[[1, 2]], bbox[[2, 2]]} - #] &]
}
]

correspondingPts[pts1_, pts2_] :=
{topPts[pts1], topPts[pts2]}

{err, transf} =
FindGeometricTransform[
Sequence @@ correspondingPts[pts1, pts2],
TransformationClass -> "Similarity"
];


And then just to check that this worked:

Graphics[
{
{PointSize[Large], Point[pts1]},
{Red, PointSize[Medium], Point@Map[transf, pts2]}
}]


transf // InputForm

TransformationFunction[{{0.9559398632333099, 0.29120310722820486, -42.71894444440079},
{-0.29120310722820486, 0.9559398632333099, 33.01504546027206}, {0, 0, 1}}]