Search code examples
c++visual-studio-2010mfcaffinetransform

Affine transformation implementation


I am trying to implement affine transformation on two images. First i find the matching pairs in both of the images. One of them is zoomed image and the other is a reference image. The pairs returned me co-efficients as :

     |1   0 |      | x |       |  1  |
A  = |      |  X = |   |   B = |     |
     |0   0 |      | y |       | 221 |   

The equation formed is X' = AX + B;

x_co_efficients[2] = (((x_new_Cordinate[2]-x_new_Cordinate[0])*(xCordinate[1]- xCordinate[0])) - ((x_new_Cordinate[1]-x_new_Cordinate[0])*(xCordinate[2] - xCordinate[0])))/
    (((xCordinate[1]-xCordinate[0])*(yCordinate[2]-yCordinate[0])) - ((xCordinate[2]-xCordinate[0])*(yCordinate[1]-yCordinate[0])));

x_co_efficients[1] = ((x_new_Cordinate[1]-x_new_Cordinate[0]) - (yCordinate[1]-yCordinate[0])*(x_co_efficients[2]))/(xCordinate[1]-xCordinate[0]);

x_co_efficients[0] = x_new_Cordinate[0] - (((x_co_efficients[1])*(xCordinate[0])) + ((x_co_efficients[2])*(yCordinate[0])));



y_co_efficients[2] = (((y_new_Cordinate[2]-y_new_Cordinate[0])*(xCordinate[1]- xCordinate[0])) - ((y_new_Cordinate[1]-y_new_Cordinate[0])*(xCordinate[2] - xCordinate[0])))/
    (((xCordinate[1]-xCordinate[0])*(yCordinate[2]-yCordinate[0])) - ((xCordinate[2]-xCordinate[0])*(yCordinate[1]-yCordinate[0])));

y_co_efficients[1] = ((y_new_Cordinate[1]-y_new_Cordinate[0]) - (yCordinate[1]-yCordinate[0])*(y_co_efficients[2]))/(xCordinate[1]-xCordinate[0]);

y_co_efficients[0] = y_new_Cordinate[0] - (((y_co_efficients[1])*(xCordinate[0])) + ((y_co_efficients[2])*(yCordinate[0])));

These are the equations i am used to finned the co-efficients from using the matching pairs. The equations are working fine for same images, for zooming image it is giving me those co-efficients. Now the problem is i have a 24 bit binary image, i want to implement affine transformation on that image with respect to reference. Now when i try to find the new co-ordinates of that image and change its current value to that co-ordinate I get a very much distorted image. which is should not get otherwise if the transformation is right. Can Some one please have a look at the equations and also explain a little bit how to implement these equations on the second image. My code is in C++. Thank you. This is my reference image

My reference image is above.. and my comparison image is enter image description here

The result i am getting is a distorted image with lines only. enter image description here

Edit 1

I have now changed the solving method to matrices. Now i am getting the right output but the image i am getting after registration is like this.. Also i have to applied limit of 0 to 320*240 in the new co-ordinates to get the pixel value. Now my result is somewhat like this. enter image description here

EDIT 2

I have changed the code and m getting this result without any black pixels. I am getting a little tilting.. have removed zoom effect in the given image though. enter image description here


Solution

  • Your transformation matrix A is problematic. It destroys the y coordinate value and assigns 221 to all y coordinates

    You can make the element at (2,2) in A just 1 and problem should be solved.