title: Scan a perspective image in Python
body: <p>I am trying to prepare several images of music sheets taken from different perspectives so that I can do OMR.</p>
  <p>I have tried transforming and rotating them but it did not help, I think I need to adjust the images' perspective</p>
  <p><a href="https://i.stack.imgur.com/iajcD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iajcD.png" alt="enter image description here" /></a></p>
  <p>I tried:</p>
  <pre class="lang-py prettyprint-override"><code>def rotate_image(mat, angle):
      height, width = mat.shape[:2] # image shape has 3 dimensions
      image_center = (width/2, height/2) # getRotationMatrix2D needs coordinates in reverse order (width, height) compared to shape
  
      rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1.)
  
      # rotation calculates the cos and sin, taking absolutes of those.
      abs_cos = abs(rotation_mat[0,0])
      abs_sin = abs(rotation_mat[0,1])
  
      # find the new width and height bounds
      bound_w = int(height * abs_sin + width * abs_cos)
      bound_h = int(height * abs_cos + width * abs_sin)
  
      # subtract old image center (bringing image back to origo) and adding the new image center coordinates
      rotation_mat[0, 2] += bound_w/2 - image_center[0]
      rotation_mat[1, 2] += bound_h/2 - image_center[1]
  
      # rotate image with the new bounds and translated rotation matrix
      rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h), borderValue=[255,255,255,0])
      return rotated_mat
  
  # the first call
  def our_rotate(RGBImage):
      angle = getAngle(RGBImage)
      rot = rotate_image(RGBImage,angle)
      return rot
  </code></pre>