Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Clustering.ipynb 7.68 KB
Newer Older
Hachemin Pierre-Yves's avatar
Hachemin Pierre-Yves committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the necessary packages\n",
    "from sklearn.cluster import KMeans\n",
    "import matplotlib.pyplot as plt\n",
    "import argparse\n",
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def centroid_histogram(clt):\n",
    "\t# grab the number of different clusters and create a histogram\n",
    "\t# based on the number of pixels assigned to each cluster\n",
    "\tnumLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)\n",
    "\t(hist, _) = np.histogram(clt.labels_, bins = numLabels)\n",
    " \n",
    "\t# normalize the histogram, such that it sums to one\n",
    "\thist = hist.astype(\"float\")\n",
    "\thist /= hist.sum()\n",
    " \n",
    "\t# return the histogram\n",
    "\treturn hist\n",
    "\n",
    "def plot_colors(hist, centroids):\n",
    "\t# initialize the bar chart representing the relative frequency\n",
    "\t# of each of the colors\n",
    "\tbar = np.zeros((50, 300, 3), dtype = \"uint8\")\n",
    "\tstartX = 0\n",
    " \n",
    "\t# loop over the percentage of each cluster and the color of\n",
    "\t# each cluster\n",
    "\tfor (percent, color) in zip(hist, centroids):\n",
    "\t\t# plot the relative percentage of each cluster\n",
    "\t\tendX = startX + (percent * 300)\n",
    "\t\tcv2.rectangle(bar, (int(startX), 0), (int(endX), 50),\n",
    "\t\t\tcolor.astype(\"uint8\").tolist(), -1)\n",
    "\t\tstartX = endX\n",
    "\t\n",
    "\t# return the bar chart\n",
    "\treturn bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAABhCAYAAADGD1rPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAABW9JREFUeJzt3E2rHmcZwPH/dc88zzlPzjlNTvNqa4umTSAtVEtxIYguSutCcCvoV/CDKPgJFD+Fq1QJDZaiC0WtupCkbWqStjZtzvt5Xmbu28UJFUF3xeLF/7eaxTBzzcXwZ1YTrTUkSf//yuc9gCTps2HQJSkJgy5JSRh0SUrCoEtSEgZdkpIw6JKUhEGXpCQMuiQlYdAlKYn+f3mz2ZlZu7C9xcak57FZz6XTp9iardPFfz4/Dj/89LgLaG3kmctP08dAtEoloBVaFIIlrQV1hK7AUFdEBH+/v0ujo0ajtRWTrqPVoGsDNQp757/GfpxlbbVPRDDvZkzHA1Yfvc2kLRjpqW1gWicc9SumFMbWc+fhLst+SjecDN+iEg1aABS6mDMpHY0lJYI6/usXC40pj59+hgp8vHOHBrQo9G3B2a0JXVvS6Gm1MgkYhoFWpnTRCFZUgDaltCUdCy5cuMxj209QaKzXexRGlm2dQv10L2O/zvqFK6xig0nMGdsmd++8x1rMOSgbXL76PFvlkLu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2SkjDokpSEQZekJAy6JCVh0CUpCYMuSUkYdElKwqBLUhIGXZKSMOiSlIRBl6QkDLokJWHQJSkJgy5JSRh0SUoiWmuf9wySpM+AX+iSlIRBl6QkDLokJWHQJSkJgy5JSRh0SUrCoEtSEgZdkpIw6JKUhEGXpCQMuiQlYdAlKQmDLklJGHRJSsKgS1ISBl2SkjDokpSEQZekJAy6JCVh0CUpCYMuSUkYdElKwqBLUhL/BJnjuczbAJ7+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAABaCAYAAACosq2hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAAcpJREFUeJzt2yFKBGEYgGFXrd5CD+ARNmw1Cia7QfAm1sXsGQwGMVsUTB5g0VOM2SAIO86wL8/TBob/+9LLX/7FMAx7AOy+/bkXAGAcgg4QIegAEYIOECHoABGCDhAh6AARgg4QIegAEYdTDju/vJrlWerz4/0cY391vTqefOb758HkM8d2dHqx9Rk3Zyc/vu/Wt1ufuete3j7mXmF0y+Vqsllfrw//PmP9tFn85T83dIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdIAIQQeIEHSACEEHiBB0gAhBB4gQdICIxTAMc+8AwAjc0AEiBB0gQtABIgQdIELQASIEHSBC0AEiBB0gQtABIgQdIELQASIEHSBC0AEiBB0gQtABIgQdIELQASIEHSBC0AEiBB0gQtABIgQdIELQASK+AW5kGK8hto3wAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "path=\"./spectrumImages/spectrumImagesAsterix/DR1a7R5usts.jpg\"\n",
    "nb_clusters=10\n",
    "\n",
    "# load the image and convert it from BGR to LAB so that\n",
    "# we can dispaly it with matplotlib\n",
    "image = cv2.imread(path,1)\n",
    "image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
    "\n",
    "# show our image\n",
    "plt.figure()\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(image)\n",
    "\n",
    "# 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 = nb_clusters)\n",
    "clt.fit(image)\n",
    "\n",
    "# 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()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path=\"./spectrumImages/spectrumImagesAsterix/7plagklTwWw.jpg\"\n",
    "nb_clusters=10\n",
    "\n",
    "# load the image and convert it from BGR to LAB so that\n",
    "# we can dispaly it with matplotlib\n",
    "image = cv2.imread(path,1)\n",
    "image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
    "\n",
    "# show our image\n",
    "plt.figure()\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(image)\n",
    "\n",
    "# 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 = nb_clusters)\n",
    "clt.fit(image)\n",
    "\n",
    "# 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()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}