title: Python angle calculation for image rotation
body: <p>I am working on an application which first needs to rotate an image slightly in order to deskew it.</p>
  
  <p>I have detected a vertical line down the image between two points (x1, y1) and (x2,y2).  The vertical line is not entirely vertical - the second x coordinate is slightly less or slightly more than the top x and therefore the line has a slope.</p>
  
  <p>I am trying to calculate the angle of the slope of the line so that I can rotate the image and then re-detect the line so that it is perfectly vertical.  I am using OpenCV in Python for this.</p>
  
  <p>Unfortunately I am having problems calculating the slope of the line and the rotation of the image is therefore inaccurate.</p>
  
  <p>My function to detect the slope of the vertical line in the image is as follows:</p>
  
  <pre><code>def find_vert_angles(vertical_line_candidates, vertical_line_candidates2, roi_x_coordinates,      roi_x_coordinates2):
  line_angles_radians = []
  
  for line_x, line_x2 in itertools.izip(vertical_line_candidates, vertical_line_candidates2):
      x_diff = line_x2 - line_x
      y_diff =  roi_x_coordinates[1][1] - roi_x_coordinates[0][1]
  
      if x_diff != 0:
          slope = y_diff / x_diff
          angle_in_radians = atan(slope)
          line_angles_radians.append(angle_in_radians)
      else:
          line_angles_radians.append(0)
  
  return line_angles_radians
  </code></pre>
  
  <p>My code to rotate the image so that the line can be vertical is as follows:</p>
  
  <pre><code>skew_angle = degrees(vert_angle[1])
  print "Detected skew angle is " + str(skew_angle) + " degrees"
  warp = cv2.getRotationMatrix2D((img_width/2,img_height/2),skew_angle,1)
  image = cv2.warpAffine(image,warp,(img_width,img_height))
  </code></pre>
  
  <p>But the angles of rotation are coming out as 249 degrees, 89 degrees etc. when they should just be a few degrees each way.</p>
  
  <p>If anyone can help me find a solution to this problem so that I can correct the skew of the image correctly, that would be much appreciated.</p>