def match_sift(sift1,sift2, dist_thresh = 1.1): matches = -1 * np.ones((sift1.shape[0]), 'int') # FIRST NORMALIZE SIFT VECTORS sift1 = tools.normalizeL2(sift1, 0) sift2 = tools.normalizeL2(sift2, 0) sift2transp = sift2.T # loop through all sift1 features for i in range(len(sift1)): # make sure vars are empty before looping. canopy rules dist_sift = [] dist_sift_sort = [] # compute distance for each sift1 vector with all sift2 vectors and keep track of distances in dist_sift dist_sift = dot(sift1[i], sift2transp) # sort all found distances to get the 2 best matches dist_sift_sort = np.argsort(dist_sift) # since the matches are the highest ones, they're at the end of the sorted array dist_frst = (len(dist_sift)-1) dist_scnd = (len(dist_sift)-2) # apply Lowe's match criterion if (dist_sift[dist_sift_sort[dist_frst]] / dist_sift[dist_sift_sort[dist_scnd]]) > dist_thresh: # seems we've found a match for feature sift1[i] # adding the sift2 index to our match array matches[i] = dist_sift_sort[dist_frst] #else: # no match for sift1[i] in sift2. # so no kudos for this one #matches[i] = -1 # FOR ALL FEATURES IN SIFT1 FIND THE 2 CLOSEST FEATURES IN SIFT2 #for i in sift1: # IF SIFT1_i IS MUCH CLOSER TO SIFT2_j1 THAN SIFT2_j2, THEN CONSIDER THIS A MUCH # MUCH CLOSER MEANS MORE THAN A PREDEFINED DISTANCE THRESHOLD return matches
def match_sift(sift1, sift2, dist_thresh=1.1): global CACHING, WRITTEN_TO_CACHE, RENEW_CACHE if CACHING: h1 = arrayhash(sift1) h2 = arrayhash(sift2) if (h1, h2) in cache['match_sift']: print "retrieving from cache: 'match_sift'" return cache['match_sift'][h1, h2] matches = -1 * np.ones((sift1.shape[0]), 'int') # FIRST NORMALIZE SIFT VECTORS sift1 = tools.normalizeL2(sift1, 0) sift2 = tools.normalizeL2(sift2, 0) # FOR ALL FEATURES IN SIFT1 FIND THE 2 CLOSEST FEATURES IN SIFT2 # IF SIFT1_i IS MUCH CLOSER TO SIFT2_j1 THAN SIFT2_j2, THEN CONSIDER THIS # A MUCH MUCH CLOSER MEANS MORE THAN A PREDEFINED DISTANCE THRESHOLD dists = np.zeros(sift2.shape[0], 'float') ranked_ratio = float('infinity') * np.ones(sift1.shape[0], 'float') dist_l2 = lambda x, y: float(x * y) for i, s1 in enumerate(sift1): for j, s2 in enumerate(sift2): dists[j] = sum(map(dist_l2, s1, s2)) closest_id = np.argmin(dists) closest = dists[closest_id] dists[closest_id] = float('infinity') second_closest = np.amin(dists) if closest / float(second_closest) < dist_thresh: matches[i] = closest_id ranked_ratio[i] = closest / float(second_closest) matches[i] = closest_id cache['match_sift'][arrayhash(sift1), arrayhash(sift2)] \ = matches, ranked_ratio WRITTEN_TO_CACHE = True return matches, ranked_ratio
def match_sift(sift1,sift2, dist_thresh = 2.0): matches = -1 * np.ones((sift1.shape[0]), 'int') # FIRST NORMALIZE SIFT VECTORS sift1 = tools.normalizeL1(sift1, 0) sift2 = tools.normalizeL2(sift1, 0) # FOR ALL FEATURES IN SIFT1 FIND THE 2 CLOSEST FEATURES IN SIFT2 N1 = sift1.shape[0] N2 = sift2.shape[0] matches = -1 * np.ones((sift1.shape[0]), 'int') dist = np.dot(sift1, sift2.T) for i in range(0, N1): temp = dist[i,:] indx = np.argsort(temp) indx = indx[::-1] if temp[indx[0]] / temp[indx[1]] > dist_thresh: matches[i] = indx[0] # IF SIFT1_i IS MUCH CLOSER TO SIFT2_j1 THAN SIFT2_j2, THEN CONSIDER THIS A MUCH # MUCH CLOSER MEANS MORE THAN A PREDEFINED DISTANCE THRESHOLD return matches
impath2 = '../../data/oxford_scaled/all_souls_000076.jpg' frames2, sift2 = week2.compute_sift(impath2) figure(1) subplot(1, 2, 1) im1 = Image.open(impath1) week2.plot_features(im1, frames1, False, 'r') subplot(1, 2, 2) im2 = Image.open(impath2) week2.plot_features(im2, frames2, False, 'r') # STEP C.1 NORMALIZE SIFT VECTORS DEFINE_AXIS=0 sift1 = tools.normalizeL2(sift1, DEFINE_AXIS) sift2 = tools.normalizeL2(sift2, DEFINE_AXIS) threshold = 1.1 # setup our array of matches matches = np.zeros(len(sift1)) sift2transp = sift2.T # loop through all sift1 features for i in range(len(sift1)): # make sure vars are empty before looping. canopy rules dist_sift = [] dist_sift_sort = []l # compute distance for each sift1 vector with all sift2 vectors and keep track of distances in dist_sift
impath1 = '../../data/oxford_scaled/all_souls_000026.jpg' frames1, sift1 = week2.compute_sift(impath1) impath2 = '../../data/oxford_scaled/all_souls_000068.jpg' frames2, sift2 = week2.compute_sift(impath2) figure(1) subplot(1, 2, 1) im1 = Image.open(impath1) week2.plot_features(im1, frames1, False, 'r') subplot(1, 2, 2) im2 = Image.open(impath2) week2.plot_features(im2, frames2, False, 'r') # STEP C.1 NORMALIZE SIFT VECTORS sift1 = tools.normalizeL2(sift1, 1) sift2 = tools.normalizeL2(sift2, 1) # STEP C.2 COMPUTE DISTANCES BETWEEN ALL VECTORS IN SIFT1 AND SIFT2. N1 = sift1.shape[0] N2 = sift2.shape[0] dist_thresh = 1.1 matches = -1 * np.ones((sift1.shape[0]), 'int') dist = np.dot(sift1, sift2.T) for i in range(0, N1): temp = dist[i,:] indx = np.argsort(temp) indx = indx[::-1] if temp[indx[0]] / temp[indx[1]] > dist_thresh:
bows.append(week3.load_bow(CODEBOOK_DIR + files[i])) # PART 5. STEP 2. COMPUTE DISTANCE MATRIX # setup matrix for comparison outcomes. 0,0 = img1,img1, 0,1=img1,img2 bowdists = np.zeros((len(bows), len(bows))) # setup check matrix to keep track of comparisons already done. set to 1 if done bowcheck = np.zeros((len(bows), len(bows))) # reset for canopy... bowdiststorage = 0 x = -1 for bw1 in bows: x += 1 y = -1 bw1n = tools.normalizeL2(bw1) # for l2 distance #bw1n = tools.normalizeL1(bw1) # for intersect distance for bw2 in bows: bowdiststorage = 0 y += 1 if bowcheck[x,y] == 0 and bowcheck[y,x] == 0: # new bow distance bowcheck[x,y] = 1 bowcheck[y,x] = 1 bw2n = tools.normalizeL2(bw2) # for l2 distance #bw2n = tools.normalizeL1(bw2) # for intersect distance bowdiststorage = week3.computeVectorDistance(bw1n, bw2n, DISTANCE) bowdists[x,y] = bowdiststorage bowdists[y,x] = bowdiststorage # PART 5. STEP 3. PERFORM RANKING SIMILAR TO WEEK 1 & 2 WITH QUERIES 'all_souls_000065.jpg', 'all_souls_0000XX.jpg', 'all_souls_0000XX.jpg'
# PART 5. STEP 1. LOAD BAG-OF-WORDS VECTORS FROM ../../data/bow/codebook_100/ using the week3.load_bow function files = os.listdir('../../data/bow/codebook_100/') bows = [] for f in files: bows.append(week3.load_bow('../../data/bow/codebook_100/' + f)) # PART 5. STEP 2. COMPUTE DISTANCE MATRIX dist = [] dist_type = 'intersect' dist = np.zeros(len(files)**2).reshape((len(files),len(files))) for i in range(0,len(files)): for j in range(0,len(files)): if dist_type == 'euclidean' or dist_type == 'l2': dist[i][j] = sum(((bows[i][k] - bows[j][k])**2) for k in range(len(bows[i]))) tools.normalizeL2(dist[i][j]) elif dist_type == 'intersect' or dist_type == 'l1': dist[i][j] = sum((np.minimum(bows[i][k], bows[j][k])) for k in range(len(bows[i]))) tools.normalizeL1(dist[i][j]) elif dist_type == 'chi2': dist[i][j] = sum((((bows[i][k] - bows[j][k]) **2) / (bows[i][k]+bows[j][k])) for k in range(len(bows[i]))) elif dist_type == 'hellinger': dist[i][j] = sum((np.sqrt((bows[i][k]*bows[j][k]))) for k in range(len(bows[i]))) tools.normalizeL2(dist[i][j]) print dist[i][j] # PART 5. STEP 3. PERFORM RANKING SIMILAR TO WEEK 1 & 2 WITH QUERIES 'all_souls_000065.jpg', 'all_souls_0000XX.jpg', 'all_souls_0000XX.jpg' query_id = int(89) #89, 21, 48 ranking = np.argsort(dist[query_id])