Question: Write the warp _ image and image _ rectification functions. WRITE ONLY UNDER WRITE YOUR CODE HERE 1 . 4 . 4 Problem 3 .

Write the warp_image and image_rectification functions. WRITE ONLY UNDER WRITE YOUR CODE HERE
1.4.4 Problem 3.4: Uncalibrated Stereo Image Rectification [15 points]
In Assignment 2, you performed epipolar rectification with calibrated stereo cameras. Rectifying a pair of images can also be done for uncalibrated camera images. Using the fundamental matrix we can find the pair of epipolar lines \( l_{i}\) and \( l_{i}\) for each of the correspondences. The intersection of these lines will give us the respective epipoles \( e \) and \( e \). Now to make the epipolar lines to be parallel we need to map the epipoles to infinity. Hence, we need to find a homography that maps the epipoles to infinity.
The rectificaton method has already been implemented for you. You can get more details from the paper Theory and Practice of Projective Rectification by Richard Hartley.
Your task is to:
1) complete the warp_image function (Hint: You may reuse some of the codes from Homework2, but this time we perform the warp of the full image content, i.e., the output image will contain the full image content in the source image. Thus, the size of the output image may not be same as the size of the source image. Calculate the targeted size of the output image by piping four corners through the forward transformation).
2) complete the image_rectification function to find the rectified images. Use the provided compute_matching_homographies to compute the rectification transformation matrices.
3) plot the parallel epipolar lines using the plot_epipolar_lines function from above.
The figure below gives you an idea on how the final results look (Note that the two images may not be in the same shape). Show your result for matrix and warrior.
```
def warp_image(image, H):
"n"
Performs the warp of the full image content.
```
Calculates bounding box by piping four corners through the transformation.
Args:
image: Image to warp
H: The image rectification transformation matrices.
Returns:
Out: An inverse warp of the image, given a homography.
min_x, min_y: The minimum/maxmum of warped image bound.
"""
### YOUR CODE HERE
### END YOUR CODE
return out, min_x, min_y
[]: from math import floor, ceil
def compute_matching_homographies(e2, F, im2, points1, points2):
"" "This function computes the homographies to get the rectified images.
Args:
e2: epipole in image 2
F : the fundamental matrix (think about what you should be passing: F or F.T !
im2: image2
points1: corner points in image1
points2: corresponding corner points in image2
Returns:
H1: the image rectification transformation matrix for image 1
H2: the image rectification transformation matrix for image 2
"""
# calculate H2
width = im2. shape[1]
height = im2.shape[0]
T=np.identity (3)
T[0][2]=-1.0** width //2
T[1][2]=-1.0** height //2
e2hat = T. dot (e2)
norm_v = np.linalg.norm(e2hat [:2])
cos_theta = e2hat[0]/ norm_v
sin_theta = e2hat[1]/ norm_v
if cos_theta 0:
cos_theta =-cos_theta
sin_theta =-sin_theta
R = np.array([[cos_theta, sin_theta, 0],
[-sin_theta, cos_theta, 0],
[0,0,1]])
G = np.identity(3)
G[2,0]=-e2hat[2]/(e2hat[0]* cos_theta + e2hat[1]* sin_theta)
H2= np | dot(np.dot (G, R), T)
calculate H1
e_prime = np.zeros((3,3))
e_prime[0][1]=-e2[2]
e_prime[0][2]= e2[1]
e_prime[1][0]= e2[2]
e_prime[1][2]=-e2[0]
e_prime[2][0]=-e2[1]
e_prime[2][1]= e2[0]
v = np.array([1,1,1])
M = e_prime.dot(F)+ np.outer(e2, v)
points1_hat = H2.dot(M.dot(points1.T)).T
points2_hat = H2.dot(points2.T).T
W = points1_hat / points1_hat [:,2].reshape(-1,1)
b =(points2_hat / points2_hat[:,2].reshape(-1,1))[:,0]
least square problem
a1, a2, a3= np.linalg.lstsq(W, b, rcond=None)[0]
HA = np.identity(3)
HA[0]= np.array([a1, a2, a3])
H1= HA | dot(H2) dot(M)
return H1, H2
]: def image_rectification(im1, im2, points1, points2):
"""This function provides the rectified images along with the new corner
-points as
images with corner correspondences
Args:
im1: image1
im2: image2
points1: corner points in image1
points2: corner points in image2
Returns:
rectified_im1: rectified image 1
rectified_im2: rectified image 2
new_cor1: new corners in the rectified image 1
new_cor2: new corners in the rectified image 2
n""
YOUR CODE HERE
END YOUR CODE
return rectified_im1, rectified_im2, new_cor1, new_cor2
[]:
This code is for you to plot the results.
The total number of outputs is 4 images in 2 pairs
imgids =["matrix", "warrior"]
for imgid in imgids:
print("./p3/"+imgid+"/"+imgid+"0.png")
I1= imread("./p3/"+imgid+"/"+imgid+"0.png")
I2= imread("./p3/"+imgid+"/"+imgid+"1.png")
cor1= np.load("./p3/"+imgid+"/cor1.npy")
cor2= np.load("./p3/"+imgid+"/cor2.npy")
rectified_im1, rectified_im2, rect_cor1, rect_cor2=
4mage_rectification(I1, I2, cor1, cor2)
rect_F = fundamental_matrix(rect_cor1, rect_cor2)
plot_epipolar_lines(rectified_im1, rectified_im2, rect_F, rect_cor1,\sqcup
rrect_cor2)
 Write the warp_image and image_rectification functions. WRITE ONLY UNDER WRITE YOUR

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 Programming Questions!