title: Calculating rotation between two images
body: <p>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.</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-py prettyprint-override"><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()
  
  
  </code></pre>
  
  <p>Original image:</p>
  
  <p><a href="https://i.stack.imgur.com/fwRbj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fwRbj.png" alt="Original image"></a></p>
  
  <hr>
  
  <p>Original image rotated by 180°:</p>
  
  <p><a href="https://i.stack.imgur.com/kFIPH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kFIPH.png" alt="Original image rotated by 180°"></a></p>