Assignment 2: Creating Panoramas
CSE 576, Spring 2017

Due: April 29 at 11:59pm (Saturday); Late Date May 1 at 11:59pm (Monday)
with penalty 10% per day

Before you start:
Download the assignment's files here.

Assignment:
For this assignment you'll be implementing a panorama image stitcher. This requires the implementation of a Harris corner detector, RANSAC alignment and an image stitcher. Feel free to reuse any code from your last assignment.

In the project, you should only have to update one file "Project2.cpp. " The buttons in the UI will call the corresponding functions in "Project2.cpp. "

Tasks:

Step 1: (10 pts) Implement the Harris corner detector. To do this, you'll need to write two routines:

void GaussianBlurImage (double *image, int w, int h, double sigma)

This function is the same as the Gaussian blur function you wrote for Assignment 1, but it computes the values on a single channel floating point image (that comes from your derivatives). Your code should be nearly identical to the first assignment.

void HarrisCornerDetector (QImage image, double sigma, double thres, CIntPt **cornerPts, int &numCornersPts, QImage &imageDisplay)

image: the input image,
sigma: the standard deviation for the Gaussian,
thres: the threshold for detection corners,
cornerPts: the returned corner points,
numCornersPts: the number of points returned,
imageDisplay: image returned to display for debugging.

Compute the Harris corner detector using the following steps:
i. Compute x and y derivatives of the image, use them to produce 3 images (I_x^2, I_y^2, and I_x*I_y) and smooth each of them with the above 5x5 Gaussian.
ii. Compute the Harris matrix H for each pixel.
iii. Compute corner response function R = Det(H)/Tr(H), and threshold R. Try threshold 50 on the UI.
iv. Find local maxima of the response function using nonmaximum supression.

Required: Open "Boxes.png" and compute the Harris corners. Save an image "1a.png" showing the Harris response on "Boxes.png" (you'll need to scale the response of the Harris detector to lie between 0 and 255). Open "Rainier1.png" with tab "Image 1" selected, and open "Rainier2.png" with tab "Image 2" selected. Use DrawCornerPoints to overlay red crosses on the images.


Step 2: (10 pts) Implement the following keypoint matching function:

void MatchCornerPoints (Qimage image1, Cintpt *cornerPts1, int numCornerPts1, Qimage image2, Cintpt *cornerPts2, int numCornerPts2, CMatches **matches, int &numMatches, Qimage &image1Display, Qimage &image2Display)

image1: the first input image,
image2: the second (match from image1 to image2),
cornerPts1: a vector of corner points found in image1,
cornerPts2: a vector of corner points found in image2,
numCornerPts1: the number of corner points in image1,
numCornerPts2: the number of corner points in image2,
matches: a vector of matches; each match has X and Y coordinates from each image

To do this, you'll need to follow these steps:
a. Compute the descriptors for each corner point. This code has already been written for you.
b. For each corner point in image 1, find its best match in image 2. The best match is defined as the closest distance (L1-norm distance. )
c. Add the pair of matching points to "matches".
d. Display the matches using DrawMatches (code is already written. ) You should see many correct and incorrect matches.

Required: Open "Rainier1.png" and "Rainier2.png", compute the Harris corner detector and find matches. Save the images "2a.png" and "2b.png" respectively. (UUse DrawMatches to overlay green lines on the images. )


Step 3: (10 pts) Compute the homography between the images using RANSAC (Szeliski, Section 6.1.4). Following these steps:

3a: Write the prjection function:

void Project(double x1, double y1, doube &x2, double &y2, double h[3][3])

This should project point (x1, y1) using the homography h. Return the projected point (x2, y2). See the slides for details on how to project using homogeneous coordinates.

3b: Write the function to count inliers:

int ComputeInlierCount(double h[3][3], Cmatches *matches, int numMatches, double inlierThreshold).

This is a helper function for RANSAC that computes the number of inlying points given a homography h. That is, project the first point in each match using the function "Project". If the projected point is less than the distance inlierThreshold (default is 5) from the second point, it is an inlier. Return the total number of inliers.

3c: Write the main function:

void RANSAC (Cmatches *matches, int numMatches, int numIterations, double inlierThreshold, double hom[3][3], double homInv[3][3], Qimage &image1Display, Qimage &image2Display)

matches: a set of numMatches matches,
numIterations: the number of times to iterate,
inlierThreshold: a real number so that the distance from a projected point to the match is less than its square,
hom: the homography and homInv its inverse,
Image1Display, Image2Display: images that hold the matches to display.

This function takes a list of potentially matching points between two images and returns the homography transformation that relates them. To do this follow these steps:
a. Iteratively do the following for "numIterations" times: (try 200 on the UI)
          i. Randomly select 4 pairs of potentially matching points from "matches".
          ii. Compute the homography relating the four selected matches with the function "ComputeHomography. "
          iii. Using the computed homography, compute the number of inliers using "ComputeInlierCount".
          iv. If this homography produces the highest number of inliers, store it as the best homography.
b. Given the highest scoring homography, once again find all the inliers. Compute a new refined homography using all of the inliers (not just using four points as you did previously. ) Compute an inverse homography as well (the fourth term of the function ComputeHomography should be false), and return their values in "hom" and "homInv".
c. Display the inlier matches using "DrawMatches".

Required: Open "Rainier1.png" with tab "Image 1" selected, and open "Rainier2.png" with tab "Image 2" selected. Compute the Harris corner detector, find matches and run RANSAC. Save the images "3a.png" and "3b.png" of the found matches . (Use DrawMatches to overlay green lines on the images. ) You should only see correct matches, i.e. , all the incorrect matches from the previous step should be removed. If you see all or some incorrect matches try running RANSAC with a larger number of iterations. You may try tuning the other parameters as well.


Step 4: (10 pts) Stitch the images together using the computed homography. Following these steps:

4a: Write bilinear interpolation function:

bool BilinearInterpolation (QImage *image, double x, double y, double rgb[3])

This code should be the same as in Assignment 1, but if x and y are out of range, it should just return false, else fill the rgb and return true.

4b: Write image stitching function:

void Stitch (Qimage image1, Qimage image2, double hom[3][3], double homInv[3][3], Qimage &stitchedImage)

image1, image2: the input images,
hom: the homography and homInv its inverse,
stitchedImage: result.

Follw these steps:
a. Compute the size of "stitchedImage. " To do this project the four corners of "image2" onto "image1" using Project and "homInv". Allocate the image.
b. Copy "image1" onto the "stitchedImage" at the right location.
c. For each pixel in "stitchedImage", project the point onto "image2". If it lies within image2's boundaries, add or blend the pixel's value to "stitchedImage. " When finding the value of image2's pixel use BilinearInterpolation.

Required: Open "Rainier1.png" with tab "Image 1" selected, and open "Rainier2.png" with tab "Image 2" selected. Compute the Harris corner detector, find matches, run RANSAC and stitch the images. Save the stitched image as "4.png". It should look like the image "Stitched.png".


Bell and Whistles (extra credit)
(Whistle = 1 point, Bell = 2 points)

Description: Description: [whistle] Create a panorama that stitches together the six Mt. Rainier photographs, i.e. , Rainier1.png, ... Painier6.png. The final result should look similar to "AllStitched.png".

Description: Description: [bell]Create your own panorama using three or more images. You must capture the images yourself, and not find them on the web. I would recommend downsampling them before stitching, i.e. , make them approximately the same size as the images in the homework assignment.

Description: Description: [bell]Implement a new image descriptor or detector that can stitch the images "Hanging1.png" and "Hanging2.png". Save the Stitched image and the "match" images. In case you're wondering - No, you cannot rotate "Hanging2.png" by hand before processing, that's cheating.

Description: Description: [bell]Do a better job of blending the two images. That is, make the seams between the two images invisible. One possible method to do this is to use center-weighting. See Szeliski, Sections 9.3.2 - 9.3.4.

Description: Description: [bell]Description: Description: [bell]Implement a new image descriptor and/or detector that can stitch the images ND1.png and ND2.png. Save the Stitched image and the "match" images.