$24
2 Problem 1 (Programming): Feature detection (20 points)
Download input data from the course website. The file price_center20.jpeg contains image 1 and the file price_center21.jpeg contains image 2.
For each input image, calculate an image where each pixel value is the minor eigenvalue of the gradient matrix
∑ x y
∑
y
Ix2
IxIy
∑
∑
I2
N =
wI I
w
w
w
where w is the window about the pixel, and Ix and Iy are the gradient images in the x and y direc-tion, respectively. Calculate the gradient images using the five-point central difference operator. Set resulting values that are below a specified threshold value to zero (hint: calculating the mean instead of the sum in N allows for adjusting the size of the window without changing the threshold
1
value). Apply an operation that suppresses (sets to 0) local (i.e., about a window) nonmaximum pixel values in the minor eigenvalue image. Vary these parameters such that around 600–650 fea-tures are detected in each image. For resulting nonzero pixel values, determine the subpixel feature coordinate using the Förstner corner point operator.
Report your final values for:
• the size of the feature detection window (i.e., the size of the window used to calculate the elements in the gradient matrix N)
• the minor eigenvalue threshold value
• the size of the local nonmaximum suppression window
• the resulting number of features detected (i.e., corners) in each image.
Display figures for:
• minor eigenvalue images before thresholding
• minor eigenvalue images after thresholding
• original images with detected features
A typical implementation takes around 30 seconds to run. If yours takes more than 120 seconds, you may lose points.
[1]: %matplotlib inline
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import time
# open the input images
I1 = np.array(Image.open('price_center20.jpeg'), dtype='float')/255.
I2 = np.array(Image.open('price_center21.jpeg'), dtype='float')/255.
• Display the input images plt.figure(figsize=(14,8)) plt.subplot(1,2,1) plt.imshow(I1) plt.subplot(1,2,2) plt.imshow(I2) plt.show()
2
[2]: def ImageGradient(I):
• inputs:
• I is the input image (may be mxn for Grayscale or mxnx3 for RGB)
•
• outputs:
• Ix is the derivative of the magnitude of the image w.r.t. x
• Iy is the derivative of the magnitude of the image w.r.t. y
m, n = I.shape[:2]
"""your code here"""
return Ix, Iy
def MinorEigenvalueImage(Ix, Iy, w):
• Calculate the minor eigenvalue image J
•
• inputs:
• Ix is the derivative of the magnitude of the image w.r.t. x
• Iy is the derivative of the magnitude of the image w.r.t. y
• w is the size of the window used to compute the gradient matrix N
•
• outputs:
• J0 is the mxn minor eigenvalue image of N before thresholding
m, n = Ix.shape[:2]
J0 = np.zeros((m,n))
#Calculate your minor eigenvalue image J0.
"""your code here"""
3
return J0
def NMS(J, w_nms):
• Apply nonmaximum supression to J using window w_nms
•
• inputs:
• J is the minor eigenvalue image input image after thresholding
• w_nms is the size of the local nonmaximum suppression window
•
• outputs:
• J2 is the mxn resulting image after applying nonmaximum suppression
•
J2 = J.copy()
"""your code here"""
return J2
def ForstnerCornerDetector(Ix, Iy, w, t, w_nms):
• Calculate the minor eigenvalue image J
• Threshold J
• Run non-maxima suppression on the thresholded J
• Gather the coordinates of the nonzero pixels in J
• Then compute the sub pixel location of each point using the Forstner␣ ,→operator
•
• inputs:
• Ix is the derivative of the magnitude of the image w.r.t. x
• Iy is the derivative of the magnitude of the image w.r.t. y
• w is the size of the window used to compute the gradient matrix N
• t is the minor eigenvalue threshold
• w_nms is the size of the local nonmaximum suppression window
#
• outputs:
• C is the number of corners detected in each image
• pts is the 2xC array of coordinates of subpixel accurate corners
• found using the Forstner corner detector
• J0 is the mxn minor eigenvalue image of N before thresholding
• J1 is the mxn minor eigenvalue image of N after thresholding
• J2 is the mxn minor eigenvalue image of N after thresholding and NMS
m, n = Ix.shape[:2]
J0 = np.zeros((m,n))
4
J1 = np.zeros((m,n))
#Calculate your minor eigenvalue image J0 and its thresholded version J1.
"""your code here"""
#Run non-maxima suppression on your thresholded minor eigenvalue image. J2 = NMS(J1, w_nms)
#Detect corners.
"""your code here"""
return C, pts, J0, J1, J2
# feature detection
def RunFeatureDetection(I, w, t, w_nms):
Ix, Iy = ImageGradient(I)
C, pts, J0, J1, J2 = ForstnerCornerDetector(Ix, Iy, w, t, w_nms)
return C, pts, J0, J1, J2
[3]: # input images
I1 = np.array(Image.open('price_center20.jpeg'), dtype='float')/255.
I2 = np.array(Image.open('price_center21.jpeg'), dtype='float')/255.
• parameters to tune w = 1
t = 1
w_nms = 1
tic = time.time()
# run feature detection algorithm on input images
C1, pts1, J1_0, J1_1, J1_2 = RunFeatureDetection(I1, w, t, w_nms) C2, pts2, J2_0, J2_1, J2_2 = RunFeatureDetection(I2, w, t, w_nms) toc = time.time() - tic
print('took %f secs'%toc)
# display results
plt.figure(figsize=(14,24))
• show pre-thresholded minor eigenvalue images plt.subplot(3,2,1)
plt.imshow(J1_0, cmap='gray')
plt.title('pre-thresholded minor eigenvalue image') plt.subplot(3,2,2)
5
plt.imshow(J2_0, cmap='gray')
plt.title('pre-thresholded minor eigenvalue image')
• show thresholded minor eigenvalue images plt.subplot(3,2,3)
plt.imshow(J1_1, cmap='gray')
plt.title('thresholded minor eigenvalue image') plt.subplot(3,2,4)
plt.imshow(J2_1, cmap='gray')
plt.title('thresholded minor eigenvalue image')
• show corners on original images
ax = plt.subplot(3,2,5)
plt.imshow(I1)
for i in range(C1): # draw rectangles of size w around corners
x,y = pts1[:,i]
ax.add_patch(patches.Rectangle((x-w/2,y-w/2),w,w, fill=False))
• plt.plot(pts1[0,:], pts1[1,:], '.b') # display subpixel corners plt.title('found %d corners'%C1)
ax = plt.subplot(3,2,6)
plt.imshow(I2)
for i in range(C2):
x,y = pts2[:,i]
ax.add_patch(patches.Rectangle((x-w/2,y-w/2),w,w, fill=False))
• plt.plot(pts2[0,:], pts2[1,:], '.b') plt.title('found %d corners'%C2)
plt.show()
took 17.647548 secs
6
7
Final values for parameters
• w =
• t =
• w_nms =
• C1 =
• C2 =
2.1 Problem 2 (Programming): Feature matching (15 points)
Determine the set of one-to-one putative feature correspondences by performing a brute-force search for the greatest correlation coefficient value (in the range [-1, 1]) between the detected features in image 1 and the detected features in image 2. Only allow matches that are above a specified correlation coefficient threshold value (note that calculating the correlation coefficient allows for adjusting the size of the matching window without changing the threshold value). Further, only allow matches that are above a specified distance ratio threshold value, where distance is measured to the next best match for a given feature. Vary these parameters such that around 200 putative feature correspondences are established. Optional: constrain the search to coordinates in image 2 that are within a proximity of the detected feature coordinates in image 1.
Note: You must center each window at the sub-pixel corner coordinates while com-puting normalized cross correlation; otherwise, you will lose points.
Report your final values for:
• the size of the matching window
• the correlation coefficient threshold
• the distance ratio threshold
• the size of the proximity window (if used)
• the resulting number of putative feature correspondences (i.e., matched features)
Display figures for:
• pair of images, where the matched features in each of the images are indicated by a square window about the feature
A typical implementation takes around 10 seconds to run. If yours takes more than 120 seconds, you may lose points.
[4]: def NCC(I1, I2, pts1, pts2, w, p):
• compute the normalized cross correlation between image patches I1, I2
• result should be in the range [-1,1]
#
• Do ensure that windows are centered at the sub-pixel co-ordinates
• while computing normalized cross correlation.
#
• inputs:
• I1, I2 are the input images
8
• pts1, pts2 are the point to be matched
• w is the size of the matching window to compute correlation coefficients
• p is the size of the proximity window
#
• output:
• normalized cross correlation matrix of scores between all windows in
• image 1 and all windows in image 2
#
"""your code here"""
return scores
def Match(scores, t, d):
• perform the one-to-one correspondence matching on the correlation␣ ,→coefficient matrix
•
• inputs:
• scores is the NCC matrix
• t is the correlation coefficient threshold
• d distance ration threshold
#
• output:
• 2xM array of the feature coordinates in image 1 and image 2,
• where M is the number of matches.
"""your code here"""
inds = []
return inds
def RunFeatureMatching(I1, I2, pts1, pts2, w, t, d, p):
• inputs:
• I1, I2 are the input images
• pts1, pts2 are the point to be matched
• w is the size of the matching window to compute correlation coefficients
• t is the correlation coefficient threshold
• d distance ration threshold
• p is the size of the proximity window
#
• outputs:
• inds is a 2xk matrix of matches where inds[0,i] indexs a point pts1
• and inds[1,i] indexs a point in pts2, where k is the number of matches
9
scores = NCC(I1, I2, pts1, pts2, w, p)
inds = Match(scores, t, d)
return inds
[5]: # parameters to tune
w =
1
t =
1
d
=
1
p
=
np.inf
tic = time.time()
• run the feature matching algorithm on the input images and detected features inds = RunFeatureMatching(I1, I2, pts1, pts2, w, t, d, p)
toc = time.time() - tic
print('took %f secs'%toc)
• create new matrices of points which contain only the matched features match1 = pts1[:,inds[0,:].astype('int')]
match2 = pts2[:,inds[1,:].astype('int')]
• display the results
plt.figure(figsize=(14,24))
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
ax1.imshow(I1)
ax2.imshow(I2)
plt.title('found %d putative matches'%match1.shape[1])
for i in range(match1.shape[1]):
x1,y1 = match1[:,i]
x2,y2 = match2[:,i]
ax1.plot([x1, x2],[y1, y2],'-r')
ax1.add_patch(patches.Rectangle((x1-w/2,y1-w/2),w,w, fill=False))
ax2.plot([x2, x1],[y2, y1],'-r')
ax2.add_patch(patches.Rectangle((x2-w/2,y2-w/2),w,w, fill=False))
plt.show()
# test 1-1
print('unique points in image 1: %d'%np.unique(inds[0,:]).shape[0])
print('unique points in image 2: %d'%np.unique(inds[1,:]).shape[0])
took 5.386107 secs
10
unique points in image 1: 202
unique points in image 2: 202
Final values for parameters
• w =
• t =
• d =
• p =
• num_matches =
11