# How to automatically align an image using OpenCV in Python

I have dozens of images of this coin at various rotations (34, 120, 170 degrees). What would be the easiest and best way in Python to automatically straighten the images so they are "aligned" as shown in the image below?

edit retag close merge delete

Sort by » oldest newest most voted

You should define what is aligned from the algorithm point of view. For us, aligned means that the writing of the coins are readable such as they have a sense. If all the coin are the same or a small set, then you can take one for each kind as a reference and compute the homography for the others: this can be an idea to fastly do it. If you have more than one coin you can do de following.

A training phase:

• Take the images of the aligned coins as reference, one per type
• Run a feature detector on them and store the descriptors

This is your database of features, it need to be quite accurate so I suggest you to use a nice feature detector (SIFT probably can do the job) and a nice matcher (take a look at GMS, in my honest opinion one of the best so far)

For every coin you would like to align you should:

• Run a feature detector
• Match the detected features with every features stored (previously, in the training phase) and count how many matched featurew you got for every coin
• Retain only the coin that have the highest number of matched features (what happen if the number is equal for, let's say, 2 coins? You should define a robust criteria...)
• Compute the homography between the found coin and the one that is processed
• Decompose the homography to obtain just the rotation matrix
• Apply the rotation matrix to the image and enjoy!

That should do the job. For the coin for which there is uncertainty you can propose a rotation angle and let the user decide which one should be applied.

HINT: You can detect features just inside the coin detecting the circle shape of the coin first and discarding all the features outside the coin to avoid some noise. This can be an improvement

I can't provide to you Python code because I use OpenCV in C++, but you can find many tutorial of every point of the bullet list. Just fuse everything together!

I hope that helped, let me know! Sorry for my english, I'm in a rush, but I hope that you understood.

more

Thanks for the suggestions. I will have to research them point by point. I couldn't find a very good tutorial but wasn't using the right terms.... Thanks a ton!!

( 2020-05-09 14:12:09 -0500 )edit

The feature detection and matching are very common tasks, there are a LOT of tutorials. You just need to understand basically what is a feature and what is a descriptor, just to store them to match against new images. For find homography is simple, there is the function findHomography, and to decompose the homography again, there is the function decomposeHomographyMat. This is a good tutorial.

If you found my answer useful please mark it as the correct answer, maybe it can help other people!

( 2020-05-10 09:14:03 -0500 )edit

Official site

GitHub

Wiki

Documentation