title: How to rotate all the images to the same orientation?
body: <p>I am trying to do image classification task and want to make sure my input data all have the same orientation.
  The code bellow did not match all the images to the same directions and some flipped wrongly.
  I will be thankful if anyone can help me with this matter, Thank you
  <a href="https://i.stack.imgur.com/DxoTu.png" rel="nofollow noreferrer">original image 1</a><br />
  <a href="https://i.stack.imgur.com/NX5qE.png" rel="nofollow noreferrer">original image 2</a></p>
  <pre><code>import cv2
  import numpy as np
  import matplotlib.pyplot as plt
  def getSubImage(rect, image):
      center, size, theta = rect
      center, size = tuple(map(int, center)), tuple(map(int, size))
      M = cv2.getRotationMatrix2D( center, theta, 1)
      dst = cv2.warpAffine(image, M, src.shape[:2])
      out = cv2.getRectSubPix(dst, size, center)
      return out
  
  image = cv2.imread('orginal1.png')
  im_bw = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  blur = cv2.GaussianBlur(im_bw, (5,5), 0)
  im_bw = cv2.Canny(blur, 10, 90)
  contours, hierarchy = cv2.findContours(im_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  rect = cv2.minAreaRect(contours[0])
  
  out = getSubImage(rect, image)
  cv2.imwrite('rotedorginal1.jpg', out)
  plt.imshow(out)
  plt.show()
  </code></pre>