Search code examples
mathopencvtransformationpolygonsroi

Deform points in a plane


Point :: (x y) is known.

I have points inside a triangular region.
I know the coordinates of the vertices in initial and final positions. I know the initial coordinates of all the points inside the triangular region.

Now I want to find out the final coordinates of all the points inside the triangular region.

The picture below shows the points in initial and final positions.

enter image description here enter image description here

Can anyone tell me how to do this in OpenCV and C++ platform??

Can I do this for arbitary shaped contour also?


Solution

  • I'm using 3D coordinates to take account

    • translation by vector [u v]
    • 2D linear transformation made by matrixabcd

    The overall transformation matrix will have the form

        [ a b u]
    M = [ c d v]
        [ 0 0 1]
    
    1. Take 3 points A=[x1 y1 1] and B=[x2 y2 1] and C=[x3 y3 1] from the triangle
    2. Compare them with their position after transformation A' = [x1' y1' 1] and B'=[x2' y2' 1] and C'=[x3' y3' 1]. Id est: Do your math to get the transformation matrix M so that A' = M A and B' = M B and C' = M C
    3. Apply x -> M x to every input points

    Edit: Incorporate the translation in the matrix M using Translation in transformation matrix

    Edit: It seems "do your math" is not clear for you.

    You'll realize that the 3 equations can be written as:

    [x1' x2' x3']     [x1 x2 x3]
    [y1' y2' y3'] = M [y1 y2 y3]
    [1   1   1  ]     [1  1  1 ]
    

    or

    X' = M X
    

    Or

    M = X . X'^-1
    

    and yes, OpenCV has a function inv() on matrices.