Perspective Transformation
This is a photo of a paper on a table. Is there a way to make it look as if it was scanned, removing everything outside the paper? This is possible if we use prospective transformation that restores the image to a picture that is centered with the optical axis and does not contain any rotation, changes to aspect ratio, skew, or keystone distortion.
 
The picture is the letter sized paper, and thus aspect ratio is 8 x 11. We need to know the coordinates of four corners of the paper in the original image and the coordinates of desired shape of the output image (letter size). Then we calculate the coefficients of perspective transformation (3 x 3 matrix) and plug the matrix in to the warpPerspective function, which uses forwardWarp unless flagged in the function.
This is the 3 x 3 projective transform matrix for this case. The OpenCV Github lines 3252 - 3273
 
Why Projective Transformation
Among several transformations available, translation, rigid, similarity, and affine transformations provide not enough degree of freedom to map trapazoid into rectangle.
paper = cv2.imread(F'/content/gdrive/My Drive/Colab Notebooks/ComputerVision/paper.jpg')
paper = cv2.cvtColor(paper, cv2.COLOR_BGR2RGB)
rows, cols, ch = paper.shape
pts1 = np.float32([[500,1100],[1500,1100],[125,2350],[1950,2330]])
pts2 = np.float32([[0,0],[800,0],[0,1100],[800,1100]])
M = cv2.getPerspectiveTransform(pts1, pts2)
print(M)
dst = cv2.warpPerspective(paper, M, (800, 1100), cv2.WARP_INVERSE_MAP)
display_side(img, dst)
plt.subplot(121),plt.imshow(paper),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
