def apply_color_map(rescaled_grayscale_diff, color_map,partitions,color_model): rows,cols=rescaled_grayscale_diff.shape height = rows width = cols blank_image = np.zeros((height,width,3), np.uint8) for row in range(rows): row_bin_indexes = np.digitize(rescaled_grayscale_diff[row],partitions) for col in range(cols): color_instance = list(int(round(float(i))) for i in color_map[row_bin_indexes[col]].split(',')) color_instance = hl.get_color_values_in_bgr(color_instance[0],color_instance[1],color_instance[2],color_model) blank_image[row][col] = np.array(color_instance).astype(np.ndarray) return blank_image
def main(): cit_low = -1 ci0_mid = 0 ciT_high = 1 small_value = 0.000000000001 current_working_dir = os.getcwd() # Read the color mode from user input color_model_code = raw_input( """Select a Color Model:\n Press 1 for RGB \n Press 2 for XYZ \n Press 3 for Lab \n Press 4 for YUV \n Press 5 for YCbCr \n Press 6 for YIQ \n Press 7 for HSL\n Press 8 for HSV \n Model: """ ) color_model_data = get_user_color_model_components(color_model_code) color_model = color_model_data[0] comp1 = color_model_data[1] comp2 = color_model_data[2] comp3 = color_model_data[3] print "You have selected " + color_model + " Color Model\n" # Read the input color instance from user cit = get_color_comp_from_user("cit", (comp1, comp2, comp3)) ci0 = get_color_comp_from_user("ci0", (comp1, comp2, comp3)) ciT = get_color_comp_from_user("ciT", (comp1, comp2, comp3)) cit_old = cit ci0_old = ci0 ciT_old = ciT cit = hl.get_color_values_in_bgr(cit[0], cit[1], cit[2], color_model) ci0 = hl.get_color_values_in_bgr(ci0[0], ci0[1], ci0[2], color_model) ciT = hl.get_color_values_in_bgr(ciT[0], ciT[1], ciT[2], color_model) # calculate total number of color instances to be created num_of_bits = int(raw_input("Enter the Number of bits <b>:")) total_num_of_color_instances = 2 ** num_of_bits print "Total of 2^{0} color instance would be created\n".format(num_of_bits) partition_size = (ciT_high - cit_low) / float(total_num_of_color_instances) partitions = get_partitions_normalized(cit_low, ciT_high, total_num_of_color_instances) # get the parititon ranges for each component first_partitions = get_partitions(cit[0], ci0[0], ciT[0], total_num_of_color_instances) second_partitions = get_partitions(cit[1], ci0[1], ciT[1], total_num_of_color_instances) third_partitions = get_partitions(cit[2], ci0[2], ciT[2], total_num_of_color_instances) color_map_partitions = np.r_[first_partitions[None, :], second_partitions[None, :], third_partitions[None, :]] color_map_partitions = color_map_partitions.transpose() # create a blank image dimensions based on the number of color instances to be displayed # multiple of 21 as each instance occupy atleast 21 pixel in height height = total_num_of_color_instances * 21 width = 1080 image = create_blank_image(height, width) # save the color map to file with name created from colormodel name, color instance values and number of bits filename = "{0}_{1}-{2}-{3}_{4}-{5}-{6}_{7}-{8}-{9}_{10}.txt".format( color_model, cit_old[0], cit_old[1], cit_old[2], ci0_old[0], ci0_old[1], ci0_old[2], ciT_old[0], ciT_old[1], ciT_old[2], num_of_bits, ) color_map_filename = "{0}\{1}".format(current_working_dir, filename) colormap_bins = genereate_colormap_and_bins(partitions, color_map_partitions, partition_size) image = save_colormap_and_bins(colormap_bins, color_map_filename, image, color_model, num_of_bits) # create named window to display image cv2.namedWindow("ColorMap in " + color_model, cv2.WINDOW_AUTOSIZE) # display image to user cv2.imshow("ColorMap in " + color_model, image) # save image to disc in current working directory cv2.imwrite("%s.jpg" % filename, image) # wait for user to press some key to close windows and exit c = cv2.waitKey(0) if "q" == chr(c & 255): cv2.destroyAllWindows()