def kmeans_image(image_path, clt): image = cv2.imread(image_path) # TODO CHECK IF OK TO REMOVE? Matlab needs rbg. but order might be different image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image.reshape((image.shape[0] * image.shape[1], 3)) # cluster the pixel intensities clt.fit(image) # build a histogram of clusters and then create a figure # representing the number of pixels labeled to each color hist = utils.centroid_histogram(clt) percentage_color_list = utils.get_colors_and_percentages(hist, clt.cluster_centers_) return percentage_color_list
def kmeans_image_show(image_path, clusters=3): pass # construct the argument parser and parse the arguments # ap = argparse.ArgumentParser() # ap.add_argument("-i", "--image", required = True, help = "Path to the image") # ap.add_argument("-c", "--clusters", required = True, type = int, help = "# of clusters") # args = vars(ap.parse_args()) # # load the image and convert it from BGR to RGB so that # we can display it with matplotlib image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # show our image plt.figure() plt.axis("off") plt.imshow(image) # reshape the image to be a list of pixels image = image.reshape((image.shape[0] * image.shape[1], 3)) # cluster the pixel intensities clt = KMeans(n_clusters = clusters) clt.fit(image) # build a histogram of clusters and then create a figure # representing the number of pixels labeled to each color hist = utils.centroid_histogram(clt) percentage_color_list = utils.get_colors_and_percentages(hist, clt.cluster_centers_) print percentage_color_list print percentage_color_list[0][1][0] bar = utils.plot_colors(hist, clt.cluster_centers_) # show our color bart plt.figure() plt.axis("off") plt.imshow(bar) plt.show()