# C.4 Concatenate histograms from R, G, B one below the other into a single histogram histo = np.concatenate([histo_r, histo_g, histo_b]) matplotlib.pyplot.bar(range(0, len(histo)), histo, 0.8, None, None, color='red') ###### # C.5 PUT YOUR CODE INTO THE FUNCTION extractColorHistogram( im ) IN week1.py ###### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Step D. Compute distances between vectors [You should implement] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # D.1 Open images and extract their RGB histograms im1 = imread('../../data/objects/flower/1.jpg') histo1 = week1.extractColorHistogram(im1) im2 = imread('../../data/objects/flower/3.jpg') histo2 = week1.extractColorHistogram(im2) # D.2 Compute euclidean distance: d=Σ(x-y)^2 # Note: the ***smaller*** the value, the more similar the histograms dist_euc = np.linalg.norm(histo2-histo1) print dist_euc # D.3 Compute histogram intersection distance: d=Σmin(x, y) # Note: the ***larger*** the value, the more similar the histograms dist_inter = np.sum(np.minimum(histo1,histo2)) print dist_inter # D.4 Compute chi-2 similarity: d= Σ(x-y)^2 / (x+y) # Note: the ***larger*** the value, the more similar the histograms
# C.2 Compute histogram from channel R using the bincount command, as indicated in the handout histo_r = np.bincount(im_r, None, 256) # C.3 Compute now the histograms from the other channels, that is G and B im_g = im[:, :, 1].flatten() histo_g = np.bincount(im_g, None, 256) im_b = im[:, :, 2].flatten() histo_b = np.bincount(im_b, None, 256) # C.4 Concatenate histograms from R, G, B one below the other into a single histogram histo = np.concatenate([histo_r, histo_g, histo_b]) print histo histo_opdr = week1.extractColorHistogram(im) print histo_opdr matplotlib.pyplot.bar(range(0, len(histo_opdr)), histo_opdr, 0.8, None, None, edgecolor='red') ###### # C.5 PUT YOUR CODE INTO THE FUNCTION extractColorHistogram( im ) IN week1.py ###### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Step D. Compute distances between vectors [You should implement] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ reload(week1) # D.1 Open images and extract their RGB histograms im1 = imread('../../data/objects/flower/1.jpg') histo1 = week1.extractColorHistogram(im1)
ax.set_xticks(labels) ax.set_ylabel("Relative frequency") ax.set_xlabel("Bin label") plt.bar(np.arange(len(histo)), histo, 1.0, color='gray', edgecolor='none') ###### # C.5 PUT YOUR CODE INTO THE FUNCTION extractColorHistogram( im ) IN week1.py ###### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Step D. Compute distances between vectors [You should implement] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # D.1 Open images and extract their RGB histograms im1 = plt.imread('../../data/objects/flower/1.jpg') histo1 = week1.extractColorHistogram(im1) im2 = plt.imread('../../data/objects/flower/3.jpg') histo2 = week1.extractColorHistogram(im2) # D.2 Compute euclidean distance: d=Σ(x-y)^2 # Note: the ***smaller*** the value, the more similar the histograms dist_euc = sum(map(lambda x, y: (x - y) ** 2, histo1 / math.sqrt(sum(map(lambda x: x * x, histo1))), histo2 / math.sqrt(sum(map(lambda x: x * x, histo2))))) # D.3 Compute histogram intersection distance: d=Σmin(x, y) # Note: the ***larger*** the value, the more similar the histograms dist_hi = sum(map(min, histo1 / float(sum(histo1)), histo2 / float(sum(histo2))))
enumeration = range(len(values)) plt.plot(enumeration, values, QUERY_PLOT_SYMBOLS[AVERAGE_PRECISION], label='average') plt.plot(enumeration, values, QUERY_PLOT_SYMBOLS[AVERAGE_PRECISION][0] + '--', label=None) enumeration = [enumeration[0] - 0.5]\ + enumeration\ + [enumeration[-1] + 0.5] ax.set_xticks(range(len(values))) ax.set_xticklabels(map(str, BOW_SIZES)) # PART 5. STEP 6. COMPUTE THE PRECISION@{5,10} FOR HISTOGRAM BASED RETRIEVAL # Generate histogram for each image in the dataset print time.strftime("[%H:%M:%S] Computing part 5, step 6") getHistogram = lambda x: week1.extractColorHistogram(np.array(plt.imread(x))) histograms = {d['norm']: {filename: getHistogram(IMG_FOLDER + filename) for filename in os.listdir(IMG_FOLDER)} for d in DISTANCE_MEASURES} color_dist_matrices = week3.distance_matrices(histograms, DISTANCE_MEASURES) color_ranking = {} for q in QUERIES: for d in color_dist_matrices: r = week3.rank_images(color_dist_matrices[d], q) for n in PRECISION_LIMITS: p = week3.custom_precision_at_N(q, indexed_labels, r, n) print "Precision@%d with %s measure for %s's color histograms: %f"\ % (n, d, q, p) print time.strftime("[%H:%M:%S] Done.")