def hist_inters_and_col_model_exercises(model, bins = (10, 10, 10)): """ Corresponds to the first exercise of this assignment. Prints a histogram of one image for the given colour model; calculates all histograms in the "database" of images; calculates and displays a table of intersections of all database histograms; calculates and displays a table of intersections of the database images against a selection of external images. """ print '\nColour histogram and histogram intersection exercise\n' image_paths, stored_model = load_images(('db', 'ext')) image = set_model(mpimg.imread(image_paths[1][0] + image_paths[0][0][0]), out_model=model) plt.subplot(1, 1, 1) print '\nSo, we\'re going to build a 3d colour histogram.' raw_input('\nLet\'s have a look at the image in question:\n(Press enter)') plt.imshow(np.flipud(image)) plt.show() print '\nNow let\'s calculate and view the corresponding histogram printout.' press_enter_and_wait() print col_hist(image, bins, model) print '\nNow we\'ll build a table of histogram intersections for all the' print 'images in our database' intersections_table(image_paths, bins, model) print '\nOk, next we\'ll build a table of intersections for database images' print 'against a selection of external images' intersections_table(image_paths, bins, model, with_ext = True) menu()
def intersections_table(image_paths, bins, model, with_ext = False): """ Returns a table of intersections of database image histograms against themselves, or, if "with_ext" == "true", of database histograms against "external" image histograms. """ print '\nStep 1: calculate histograms' press_enter_and_wait() db_histograms = [] for img_name in image_paths[0][0]: image = set_model(mpimg.imread(image_paths[1][0]+img_name), out_model=model) db_histograms.append(col_hist(image, bins, model)) ext_histograms = [] if with_ext: for img_name in image_paths[0][1]: image = set_model(mpimg.imread(image_paths[1][1]+img_name), out_model=model) ext_histograms.append(col_hist(image, bins, model)) else: ext_histograms = db_histograms print '\nStep 2: build table of intersections' press_enter_and_wait() intersections_table = [['I\\M ']] for name in image_paths[0][0]: intersections_table[0].append(name[0:4]) image_names = image_paths[0][0] if with_ext: image_names = image_paths[0][1] for name in image_names: intersections_table.append([name[0:4]]) i = 1 for h1 in ext_histograms: for h2 in db_histograms: rounded_result = '%.2f'%round(histogram_intersect(h1, h2), 2) intersections_table[i].append(rounded_result) i += 1 raw_input('\nA printout of the resulting table follows:\n(press enter)') for item in intersections_table[0]: print '%s '%item, print '\n', for row in intersections_table[1:]: for item in row: print '%s '%item, print '\n',