title: Perspective Transform using paper
body: <p>I've got an image from phone camera which have a paper inside it. In the image are also, some coordinates marked to get the distance between them. Since the aspect ratio of paper is known in advance (0.7072135785007072), I want to correct the distortion so that the whole image looks as if it's taken from the top view. I collect the four corners of the paper and apply opencv <code>getPerspectiveTransform</code> as follows:</p>
  
  <pre><code>pts1 = [[ 717.,  664.],
  [1112.,  660.],
  [1117., 1239.],
  [ 730., 1238.]]
  
  ratio=0.7072135785007072
  cardH=math.sqrt((pts1[2][0]-pts1[1][0])*(pts1[2][0]-pts1[1][0])+(pts1[2][1]-pts1[1][1])*(pts1[2][1]-pts1[1][1]))
  cardW=ratio*cardH;
  pts2 = np.float32([[pts1[0][0],pts1[0][1]], [pts1[0][0]+cardW, pts1[0][1]], [pts1[0][0]+cardW, pts1[0][1]+cardH], [pts1[0][0], pts1[0][1]+cardH]])
  
  M = cv2.getPerspectiveTransform(pts1,pts2)
  </code></pre>
  
  <p>with this matrix <code>M</code> I'm transforming the whole image as follows:</p>
  
  <pre><code>transformed = np.zeros((image.shape[1], image.shape[0]), dtype=np.uint8);
  dst = cv2.warpPerspective(image, M, transformed.shape)
  _ = cv2.rectangle(dst, (pts2[0][0], pts2[0][1]), (int(pts2[2][0]), int(pts2[2][1])), (0, 255, 0), 2)
  </code></pre>
  
  <p>The problem with this is that it's correcting the perspective of paper but distorting the overall image. I don't know why. The input image is <a href="https://drive.google.com/open?id=1dWvuq7qMBV7rVosWaTuYOvP411KIeP_L" rel="nofollow noreferrer">this</a> and the corresponding output image is <a href="https://drive.google.com/open?id=1aRUP-uc9ehdtNC2TekuM9ktAGFKh8Y40" rel="nofollow noreferrer">this</a>. In the input image point M and O and aligned horizontally, but to my surprise after transforming the overall image the point M and O are no longer aligned horizontally, why is that happening ? </p>