1

I want to measure the rotation between two objects in two images. The second image is created by rotating the first one. I tried techniques like fitting a line but like you can see they get the same slope/angle.

Here is my code:


import cv2
import numpy as np
import matplotlib.pyplot as plt
#import imutils

img = cv2.imread("4.jpg",0)
indices = np.where(img!= [255])
coordinates = zip(indices[0], indices[1])

from scipy.optimize import curve_fit

def f(x, A, B): # this is your 'straight line' y=f(x)
    return A*x + B

A,B = curve_fit(f, indices[1], indices[0])[0] # your data x, y to fit
x=np.arange(0,1000,1)
y=A*x+B
#%%
plt.plot(y)
plt.scatter(indices[1],indices[0])

plt.show()


Original image:

Original image


Original image rotated by 180°:

Original image rotated by 180°

kkuilla
  • 2,226
  • 3
  • 34
  • 37
Alsen57
  • 133
  • 1
  • 8

1 Answers1

0

You can calculate the center of mass and find the point that is the farthest point from the center. Based on this two points you can calculate the line. You can find the third point that is the farthest point from this line and belong to this object. This set of 3 points will give you the unique solution unless your object is symmetric. Based on these 3 points you can calculate the rotation between original and rotated image:

  1. Calculate the matrix using getAffineTransform https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#Mat%20getAffineTransform(InputArray%20src,%20InputArray%20dst) function
  2. Calculate angle from rotation this matrix using equations from this link: opencv estimateRigidTransform: How to get global scale?
  3. Additionally, you can also get the scale factor and translation vector from this matrix.
Piotr Siekański
  • 1,665
  • 8
  • 14