# Find transformation function for point on an image

I am trying to develop an algorithm, but I barely can describe it so I'm not sure what to look for. I have two images img_A and img_B. img_A is the original, while img_B is an image of the same item but slightly different. Let's say one is a photo of a person, while the other is a drawing of that same person. This means that img_B is very similar, to img_A, but it could be slightly rotated and maybe a bit bigger/smaller.

What I am trying to do is : from a point from img_A a_a get the corresponding a_b on the img_B. I think I need to define two points on img_A and their correspoinding points on img_B to define the axis correspondance. Once I have those 4 points, I calculate a_b. The 4 "axis points" will be defined by hand once, then the User will select a point "a_a" by hand and the correspoinding "a_b" will be calculated.

On a visual example: I select the center of the left eye and the lower point of the chin from both images. I then select the right eye from img_A and as a result I get the coordinates of a_b. (if the drawing was perfectly done, the coordinate of a_b will correspond to the right eye on img_b).

How I develop this? I need a matrix to multiply a_a to obtain a_b? how?

• Are you looking for an algorithm to find corresponding points in those images automatically? Or do you intend to let a human pick some corresponding points, and you are just looking for the related coordinate transformation? Please clarify! – Doc Brown Dec 12 '16 at 16:28
• You would map that with a vector. Though constructing that vector is extremely complex and definitely too broad as a question. – qwerty_so Dec 12 '16 at 16:52
• @DocBrown I edited the question. I will do all by hand. Pick the first 2+2 reference points and then select one on the original image and see where it should be on the second one. – maugch Dec 12 '16 at 16:56
• @ThomasKilian Why too broad? I think it's not more complex that what a Photoshop/Gimp does when you resize an image, only done in a different portion of the screen. – maugch Dec 12 '16 at 17:01
• @mobinoob: it would have probably been too complex if it you were looking for an algorithm to find the points automatically. – Doc Brown Dec 12 '16 at 17:07

You are looking for an affine transformation of the form

``````f(v) = s * R * v + t
``````

where v is a 2D vector in the coordinate system of image A, `s` is a scaling factor (positive real value), `R` is a 2x2 rotation matrix of the form

`````` [cos(alpha)   sin(alpha)]
[-sin(alpha)  cos(alpha)]
``````

and `t` is a 2D vector, the translation. `f(v)` then gives you the corresponding point in image B. So what you need to calculate is s, alpha and t.

Lets assume you have two points `p1` and `p2` in the coordinate system of image A, and two corresponding points `q1` and `q2` in the coordinate system of image B. Then you can easily calculate `s = |q2-q1|/|p2-p1|`. The angle `alpha` is the angle difference between the direction angles of the vector from p1 to p2 and the vector from q1 to q2. The direction angle for p1 to p2 is calculated by `atan(dy/dx)` where dx and dy are the x and y coordinates of `p2-p1` (for q1 to q2 it works similar). To avoid problems of a zero denominator and the sign, use the `atan2` function, which is available in many programming languages.

So now you have `s` and `R`, and from `f(p1)=q1` you can now deduce

``````t= q1- s * R * p1
``````

which gives you finally the translation `t`.

• That works for a simple transformation, but not for a photo to a drawing as the OP requires. – qwerty_so Dec 12 '16 at 17:37
• @ThomasKilian: honestly, we don't know what the OP really requires. He said he wants to use two control points, and used the photo/drawing example as - just an example. Lets see if my answer helps him. – Doc Brown Dec 12 '16 at 17:41
• I believe this is exactly what I need. I am going to test it soon. see other comment below – maugch Dec 13 '16 at 18:28
• @ThomasKilian This is what I want to do: imgur.com/a/4XxHg note that f(v) does not correspond to the chin just to explain that the right version of the monalisa does not have the same proportion as the left one. This is the easiest way to see if a reproduction is similar to the original, even if a bit rotated. – maugch Dec 13 '16 at 18:30
• @mobinoob Indeed that's what Doc Brown suggested. From your description it sounded like photo vs. drawing which is not really possible to achieve with an affine transformation. – qwerty_so Dec 13 '16 at 22:33