1
$\begingroup$

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?

Many thanks in advance.

Image 1

Image 2

$\endgroup$
  • $\begingroup$ You can try working off of ImageCorrespondingPoints $\endgroup$ – b3m2a1 Sep 15 '17 at 6:13
5
$\begingroup$

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]}

dots

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]}
  }]

confirmation

transf // InputForm

TransformationFunction[{{0.9559398632333099, 0.29120310722820486, -42.71894444440079}, 
  {-0.29120310722820486, 0.9559398632333099, 33.01504546027206}, {0, 0, 1}}]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.