{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows primary color analysis of color image using K-Means algorithm.\n", "The output are N primary colors and their corresponding percentage." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.cluster import KMeans\n", "import matplotlib.pyplot as plt\n", "import cv2\n", "import numpy as np\n", "import requests" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def centroid_histogram(clt):\n", " # grab the number of different clusters and create a histogram\n", " # based on the number of pixels assigned to each cluster\n", " numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)\n", " (hist, _) = np.histogram(clt.labels_, bins=numLabels)\n", "\n", " # normalize the histogram, such that it sums to one\n", " hist = hist.astype(\"float\")\n", " hist /= hist.sum()\n", "\n", " # return the histogram\n", " return hist\n", "\n", "\n", "def plot_colors(hist, centroids):\n", " # initialize the bar chart representing the relative frequency\n", " # of each of the colors\n", " bar = np.zeros((50, 300, 3), dtype=\"uint8\")\n", " startX = 0\n", " # loop over the percentage of each cluster and the color of\n", " # each cluster\n", " for (percent, color) in zip(hist, centroids):\n", " # plot the relative percentage of each cluster\n", " endX = startX + (percent * 300)\n", " cv2.rectangle(\n", " bar, (int(startX), 0), (int(endX), 50), color.astype(\"uint8\").tolist(), -1\n", " )\n", " startX = endX\n", "\n", " # return the bar chart\n", " return bar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load the image and convert it from BGR to RGB so that\n", "# we can dispaly it with matplotlib\n", "# image_path = './data/blue.jpg'\n", "# image = cv2.imread(image_path)\n", "\n", "file = requests.get(\n", " \"https://heibox.uni-heidelberg.de/thumbnail/537e6da0a8b44069bc96/1024/images/100361_asm.png\"\n", ")\n", "image = cv2.imdecode(np.fromstring(file.content, np.uint8), 1)\n", "\n", "# BGR-->RGB cv to matplotlib show\n", "image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "\n", "# show our image\n", "plt.figure()\n", "plt.axis(\"off\")\n", "plt.imshow(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# reshape the image to be a list of pixels\n", "image = image.reshape((image.shape[0] * image.shape[1], 3))\n", "\n", "# cluster the pixel intensities\n", "clt = KMeans(n_clusters=8)\n", "clt.fit(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# build a histogram of clusters and then create a figure\n", "# representing the number of pixels labeled to each color\n", "hist = centroid_histogram(clt)\n", "bar = plot_colors(hist, clt.cluster_centers_)\n", "\n", "# show our color bart\n", "plt.figure()\n", "plt.axis(\"off\")\n", "plt.imshow(bar)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for (percent, color) in zip(hist, clt.cluster_centers_):\n", " print(\"color:\", color, \" percentage:\", percent)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }