예제 #1
0
def color_extract(img, clusters):
	image = cv2.imread(img)
	image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 	fig, (ax1, ax2) = plt.subplots(2, 1)

 	im1 = ax1.imshow(image)
 	plt.axis("off")

	# show our image
	# plt.figure()
	# plt.axis("off")
	# plt.imshow(image)

	# reshape into pixel matrix
	image = image.reshape((image.shape[0] * image.shape[1], 3))
	
	# cluster using kmeans package
	clt = KMeans(n_clusters = (clusters+1))
	clt.fit(image)
	zippy = utils.remove_bg(clt)

	# zippy = utils.centroid_histogram(clt)
	bar = utils.plot_colors(zippy)

	im2 = ax2.imshow(bar)

	plt.axis("off")
	# plt.show()
	zippy = sorted(zippy, reverse = True)
	zippy = [i for j in zippy for i in j]
	zippy = [i for j in zippy for i in j.flatten('F')]
	zippy.insert(0, img[2:len(img)-4])

	return zippy
예제 #2
0
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()
예제 #3
0
	help = "# of clusters")
args = vars(ap.parse_args())

# load the image and convert it from BGR to RGB so that
# we can dispaly it with matplotlib
image = cv2.imread(args["image"])
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 = args["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)
bar = utils.plot_colors(hist, clt.cluster_centers_)

# show our color bart
plt.figure()
plt.axis("off")
plt.imshow(bar)
plt.show()
cv2.waitKey(0);

# reshape image to metrix N x 3
imageReshaped = image.reshape((-1, 3))
# change data in array from uint8 to float32
imageReshaped = np.float32(imageReshaped)

# rule the iteration termination
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
# number of cluster
K = 8
ret, label, center = cv2.kmeans(imageReshaped, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

# find mean color range and percent of color in image
hist = utils.centroid_histogram(center, label)
bar = utils.plot_colors(hist, center)

# convert back to uint8 and reshape image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((image.shape))

# print color and percent in image
for(percent, color) in zip(hist, center):
    print "color(", color, ") = ", percent*100, "%"

plt.figure()
plt.axis("off")
plt.imshow(bar)
plt.show()