def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) best_match = [] for i, query_hist in enumerate(query_hists): min_d = np.inf min_d_index = -1 for j, model_hist in enumerate(model_hists): d = dist_module.get_dist_by_name(query_hist, model_hist, dist_type) D[j][i] = d if d < min_d: min_d = d min_d_index = j best_match.append(min_d_index) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) #------------------------------------------------------------- #print(D.shape) #print(model_hists) #print(query_hists) for qe in (range(len(query_hists))): for mo in (range(len(model_hists))): D[mo][qe] = dist_module.get_dist_by_name(query_hists[qe], model_hists[mo], dist_type) #index of best match to each querry image best_match = np.argmin(D, axis=0) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) # best_match = [99999] * len(query_images) # your code here # compute distance matrix for i in range(len(model_images)): hist1 = model_hists[i] for j in range(len(query_images)): hist2 = query_hists[j] D[i, j] = dist_module.get_dist_by_name(hist1, hist2, dist_type) # if D[i,j] < best_match[j]: # best_match[j] = D[i,j] # find the best match for each query image best_match = D.argmin(axis=0) # print (best_match) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): # whether histogram function expects grayvalue or color image hist_isgray = histogram_module.is_grayvalue_hist(hist_type) # form list of file images to list of histograms model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros( (len(model_images), len(query_images))) # container for distances best_match = [] # container for best match for each image for i in range(len(query_images)): Q = query_hists[i] for j in range(len(model_hists)): V = model_hists[j] h1 = Q.copy() h2 = V.copy() dist = dist_module.get_dist_by_name( h1, h2, dist_type) # distance between histogram D[j, i] = dist # save distance measure in correct position idx = list(D[:, i]).index( min(D[:, i]) ) # find histogram with lower distance respect to the one in exam best_match.append(idx) return np.array(best_match), D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) best_match = np.zeros(len(query_images)) for i in range(len(model_hists)): for j in range(len(query_hists)): D[i, j] = dist_module.get_dist_by_name(query_hists[j], model_hists[i], dist_type) for i in range(len(best_match)): best_match[i] = np.where(D == np.amin(D[:,i]))[0][0] return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) # ... (your code here) for i in range(len(model_images)): for j in range(len(query_images)): D[i][j] = dist_module.get_dist_by_name(model_hists[i], query_hists[j], dist_type) best_match = D.argmin(axis=0) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms( model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms( query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) # your code here for model_image_hist_index in range(D.shape[0]): for query_image_hist_index in range(D.shape[1]): model_hist = model_hists[model_image_hist_index] query_hist = query_hists[query_image_hist_index] D[model_image_hist_index][query_image_hist_index] = dist_module.get_dist_by_name( model_hist, query_hist, dist_type) best_match = np.argmin(D, 0) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) best_match=[] for i in range(len(query_images)): for j in range(len(model_images)): D[j,i]=dist_module.get_dist_by_name(model_hists[j],query_hists[i],dist_type) minpos = list(D[:,i]).index(min(list(D[:,i]))) best_match.append(int(model_images[minpos].split('/')[1].split('_')[0].split('obj')[1])) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(model_images), len(query_images))) # D[i][j] = the best match is j for query i for i in range(len(model_images)): histogram_model = model_hists[i] for j in range(len(query_images)): histogram_image = query_hists[j] D[i][j] = dist_module.get_dist_by_name(histogram_image, histogram_model, dist_type) best_match = np.argmin(D, axis=0) return best_match, D
def find_best_match(model_images, query_images, dist_type, hist_type, num_bins): hist_isgray = histogram_module.is_grayvalue_hist(hist_type) model_hists = compute_histograms(model_images, hist_type, hist_isgray, num_bins) query_hists = compute_histograms(query_images, hist_type, hist_isgray, num_bins) D = np.zeros((len(query_images), len(model_images))) best_match = np.zeros((len(query_images), len(model_images))) for i in range(len(query_images)): for j in range(len(model_images)): D[i, j] = dist_module.get_dist_by_name(query_hists[i], model_hists[j], dist_type) best_match[i] = np.argsort(D[i]) #... (your code here) best_match = best_match[:, 0] best_match = best_match.astype(int) return best_match, D
hist2 = histogram_module.get_hist_by_name( img2_gray, num_bins_gray, hist_types[hidx]) else: hist1 = histogram_module.get_hist_by_name( img1_color, num_bins_color, hist_types[hidx]) hist2 = histogram_module.get_hist_by_name( img2_color, num_bins_color, hist_types[hidx]) if len(hist1) == 2: hist1 = hist1[0] if len(hist2) == 2: hist2 = hist2[0] D[didx, hidx] = dist_module.get_dist_by_name(hist1, hist2, distance_types[didx]) print('Comparison of images "%s" to "%s":' % (img1_file, img2_file)) print(D) print('\n') print('Legend:') print('%s-%s, %s-%s, %s-%s, %s-%s' % ('l2', 'grayvalue', 'l2', 'rgb', 'l2', 'rg', 'l2', 'dxdy')) print('%s-%s, %s-%s, %s-%s, %s-%s' % ('intersect', 'grayvalue', 'intersect', 'rgb', 'intersect', 'rg', 'intersect', 'dxdy')) print('%s-%s, %s-%s, %s-%s, %s-%s' % ('chi2', 'grayvalue', 'chi2', 'rgb', 'chi2', 'rg', 'chi2', 'dxdy')) # %%