0

Some posts recommend homography decomposition, but is there a simpler way (e.g., converting the images to NumPy arrays and using Pillow methods)?

Image 1

Image 2

  • 2
    Any method that would solve this for equilateral triangles would come up with 3 unique solutions. Thus there is no one solution. – fmw42 Sep 28 '22 at 19:45
  • 2
    Please provide representative images - the examples you show have simple and obvious solutions - which won't work for real-world images. – Mark Setchell Sep 28 '22 at 20:06
  • Do you know which corners correspond? – Micka Sep 28 '22 at 20:19

1 Answers1

1

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

Reda El Hail
  • 966
  • 1
  • 7
  • 17