Question: Uses Python! For each image, we will need to compute the homography (3x3 transformation matrix) using linear least squares. This transformation should map the points

Uses Python!

For each image, we will need to compute the homography (3x3 transformation matrix) using linear least squares. This transformation should map the points in the peripheral image that you clicked to their corresponding points in the base "central" image. For the central image itself, this transformation would just be the identity matrix.

For this part, you should write two functions. The first function, compute_homography should estimate a transformation matrix H given the pairs of points. The second function, apply_homography should take as input an array of point coordinates and a 3x3 matrix and return the transformed coordinates.

Note that if the matrix H maps (x1,y1) to (x2,y2), then the inverse mapping is given simply by inverting the matrix. So applying the homography inv(H) will map (x2,y2) back to (x1,y1).

You will want to use np.linalg.lstsq to solve for the coeffecients of H.

--------the code------------

def compute_homography(pts1,pts2): """ Computes the 3x3 transformation matrix (homography) that when applied to pts1 yields pts2 Parameters ---------- pts1 : 2xN array 2D coordinates of points (to warp)

pts2 : 2xN array target 2D coordinates where points should end up

Returns ------- numpy.array (dtype=float) 3x3 array containing the matrix H """

#expected dimensions of input assert(pts1.shape[0]==2) assert(pts2.shape[0]==2) assert(pts1.shape[1]>=4) assert(pts2.shape[1]>=4) # #your code goes here # #expected dimension of output assert (H.shape==(3,3)) return H

def apply_homography(H,pts): """ Apply a specified homography H to a set of 2D point coordinates Parameters ---------- H : 3x3 array matrix describing the transformation

pts : 2xN array 2D coordinates of points to transform Returns ------- numpy.array (dtype=float) 2xN array containing the transformed points """

#assert expected dimensions of input assert(H.shape==(3,3)) assert(pts.shape[0]==2) assert(pts.shape[1]>=1) #your code goes here #make sure transformed pts are correct dimension assert(tpts.shape[0]==2) assert(tpts.shape[1]==pts.shape[1]) return tpts

--------Testing--------

# since there may be some numerical imprecision, specify a small error tolerance tol = 1e-10

### testing apply homography

# # If you apply the identity transformation you should get back the same points # pts1 = np.random.random((2,4)) H = np.array([[1.,0,0],[0,1,0],[0,0,1]]) pts2 = apply_homography(H,pts1) err = np.mean(np.abs(pts1-pts2)) assert(err

# # If you apply H to some points and then inv(H) to the results, you should get back the points you started with #

#your code here

### testing computeHomography # # NOTE: make sure you don't try to estimate a homography for cases where three or # four of the points are in a line since this will lead to an underdetermined # linear system #

# # If you compute the homography between a set of points and itself, you should get back the identity matrix #

#your code here

# # If you compute the homography between a set of points and those points multipled by two, you should get # back a matrix which looks like a simple scaling (i.e. 2 2 1 on the diagonal) # pts1 = np.array([[0.,0,1,1],[0.,1,1,0]]) pts2 = 2*pts1 H = compute_homography(pts1,pts2) Htrue = np.array([[2.,0.,0.],[0.,2.,0],[0.,0.,1]]) err = np.mean(np.abs(H-Htrue)) assert(err

# # If you generate two random sets of points and compute the homography between them, then apply the # estimated homography to the first set of points, you should get back the second set. #

#your code here --------------------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!