Part 1: Image Rectification
To rectify an image, I first compute the homography (projective transformation) between two sets of correspondences for the first and second image. For the first set of points, I used the object I am trying to rectify and for the second image, I chose a set of points representing the shape of that object from a head-on view. To calculate the homography transformation, we can solve a system of linear equations solving for a 3x3 matrix with the right most bottom value set to 1 using least squares estimate.
In the example below, I rectified a receipt on the table by warping the shape of the first image to the plane of the head-on shape I want.
Here, I rectified this image of my Bose headphopnes case lying on the table to the rounded rectangular shape of the case.