Home > other >  What is the simplest way to detect the angle of rotation between two images of the same dimensions (
What is the simplest way to detect the angle of rotation between two images of the same dimensions (

Time:09-30

Some posts recommend Image 1

Image 2

CodePudding user response:

I made up this very basic algorithm to calculate the angle of rotation. It rotates the second image with 1 degree and calculate the mean square error (MSE) for each angle between 0 and 360 degrees. The rotation angle that we are looking for should be corresponding to the minimal MSE.

from PIL import Image, ImageChops
import numpy as np
import math 
import matplotlib.pyplot as plt

def rmsdiff(x, y):
  """Calculates the root mean square error (RSME) between two images"""
  errors = np.asarray(ImageChops.difference(x, y)) / 255
  return math.sqrt(np.mean(np.square(errors)))


im1 = Image.open("1.png")

im2 = Image.open("2.png")
print(im1)

mse = []
for i in range(360):
  im2_rot = im2.rotate(i)
  mse.append(rmsdiff(im1,im2_rot))

print(mse.index(min(mse))) # outputs 90 degrees
plt.plot(mse)
plt.show()

In the case of the images in the question this is the MSE plot, and the minimum MSE is corresponding to 90 degrees.

enter image description here

  • Related