Image to layout alignment using 3 landmarks registrartion

Hello Matthias-

This question is more of a geometric question but I'm sure you will have a very short and simple code that can help a lot:
Say I want to align an image to a layout. Both the image and the layout have a set of 3 matching respective landmarks.
So the image landmarks set is: (Im1x,Im1y) (Im2x,Im2y) (Im3x,Im3y) and the layout landmark set is (La1x,La1y) (La2x,La2y) (La3x,La3y)

Can you suggest a code that will transpose the image to the layout using above landmarks coordinates?

Thanks for your time and effort! Itamar

Comments

  • edited February 2020

    Hi Itamar,

    The method which achieves this is "Matrix3d.adjust". Here is a sample:

    # Landmarks image (in pixel coordinates)
    landmarks_img = [
      pya.DPoint(100, 100),
      pya.DPoint(200, 200),
      pya.DPoint(200, 101)
    ]
    
    # Landmarks layout (in micrometers)
    landmarks_layout = [
      pya.DPoint(105.0, 25.0),
      pya.DPoint(110.0, 30.0),
      pya.DPoint(110.0, 25.0)
    ]
    
    matrix = pya.Matrix3d()
    matrix.adjust(landmarks_img, landmarks_layout, pya.Matrix3d.AdjustAll, -1)
    
    print(str(matrix))
    

    The output is (beautified):

    (0.05,      0,      100) 
    (-0.000505, 0.0505, 20) 
    (0,         0,      1)
    

    which is a displacement of 100, 20 plus a scaling by 0.05 plus a small shear I have induced because I used a y value of 101 for the third image point instead of 100.

    This matrix you can then put into the "matrix" property of your pya.Image object to place it according to the landmark mapping.

    Matthias

  • Thank you so much for it Matthias

  • Hi Matthias -

    Can you suggest a method to evaluate similarity of shapes?

    Looking at the example above, the image coords form a right-angle triangle. Triangle with (almost) the same proportion of angles and sides is formed by the layout coords.
    So in this case the similarity method should indicate that shapes are pretty similar (after the appropriate rotation and scaling).

    If we change for example the first image coord to be (100, 150), the similarity method should give lower score for similarity.

    Thanks!

  • Hi @mikamar,

    at first glance I'd say the "diagonality" of the upper-left 2x2 matrix of the example case should give you a similarity. That matrix is

    (0.05,      0) 
    (-0.000505, 0.0505) 
    

    That is pretty "diagonal". It's obviously possible to define a metrics that indicates the "non-diagonality", but that depends on whether you include rotations or mirroring into your definition of "similarity".

    Matthias

Sign In or Register to comment.