# Rotation matrix between two objects - alignment

I am trying to align two objects based on the notion of points and their correspondence, but struggling to find a solution.

I have two objects A and B. However, they are rotated in a different direction, and therefore I would like them to align. I have a notion of some correspondence between the points in object A and in object B.

The initial idea that I had looks as follows. Pick two points( let's call them center and end point) in A, and then pick the corresponding points in B. Then measure the angle between them.

I believe that I could get an angle between them, but I am not sure how would that translate to the rotation matrix. I was also wondering whether it is not better to split it into different axes , and then perform multiplication. However, I am not sure how would I proceed with that.

Could anybody give me advice whether my approach is ok, or possibly provide a way of obtaining a rotation matrix for the whole object based on those vectors?

My idea:

Let's say that I have object A, and two points:

A0 = [2,4,6]

A1 = [3,5,7]

Then I calculate a vector

A0A1 = [1,1,1]

Then I find a corresponding two points in object B:

B0 = [4,8,12]

B1 = [2,7,5]

Then B0B1 = [-2,-1,-7]

Now I would calculate the angle

You probably want to find a homography that maps object A to object B. I suggest reading about image rectification/alignment/registration. A homography is a linear function of the form

$$q = Ap+b,$$

where $A$ is a matrix, $b$ is a translation vector, $p$ is a point from object $A$, and $q$ is the corresponding point from object $B$.

If you have four points from object A and the corresponding four points in B, you can solve uniquely for a homography that maps each of those A-points to the corresponding B-point (using linear algebra). There are standard methods for finding that homography in the computer vision literature, based on solving a system of linear equations. For instance, OpenCV has a method that will do that for you.

More generally, if your points have some small amount of error in them, it can be more effective to take more than four points and then find the best-fit homography. For instance, you could take five points, or six points, or 10 points, and find the best-fit homography that maps those 10 points from object A (as close as possible) to the corresponding 10 points from object B. This becomes a least-squares fit problem, where you want to minimize the sum of the squared distances between (homography applied to point from object A) and (location of corresponding point in object B). There are ways to find that best-fit homography using a least-squares fit.

There's lots more one can say about the subject -- it has been studied extensively in the image processing and computer vision literature. See https://en.wikipedia.org/wiki/Point_set_registration for a starting point.