2022-11-28 11:40:47 +01:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# TP1 KMEANS\n",
|
|
|
|
"\n",
|
|
|
|
"On nous propose de coder l'algorithme des kmeans afin de faire du clustering sur 2 classes puis plus de 2 classes.\n",
|
|
|
|
"Plus tard, on utilisera notre algorithme pour segmenter une image sur l'information de couleur."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 1,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import matplotlib.pyplot as plt\n",
|
|
|
|
"import numpy as np\n",
|
|
|
|
"import scipy.spatial\n",
|
|
|
|
"from skimage import io"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 2,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"# mean = [1,2,3,4]\n",
|
|
|
|
"# sd = [0.25, 0.25, 0.1, 0.2]\n",
|
|
|
|
"clusters = 5\n",
|
|
|
|
"dim = 2\n",
|
2022-12-08 22:12:07 +01:00
|
|
|
"nb = 50\n",
|
2022-11-28 11:40:47 +01:00
|
|
|
"K= clusters\n",
|
2022-11-29 12:15:07 +01:00
|
|
|
"mean = np.random.randint(5, size=clusters)\n",
|
|
|
|
"mean = mean.T * np.random.random(size=clusters)\n",
|
2022-12-08 22:12:07 +01:00
|
|
|
"sd = np.random.random(size=clusters)\n",
|
2022-11-28 11:40:47 +01:00
|
|
|
"path_image = \"fruits.jpg\"\n",
|
|
|
|
"# print(mean)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Fonctions à utiliser pour le clustering"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 3,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def gen_points(mean=1,sd=0.5, nb=100, dim=2, clusters=2):\n",
|
|
|
|
" size = []\n",
|
|
|
|
" # for i in range(0,dim):\n",
|
|
|
|
" size.append(nb)\n",
|
|
|
|
" size.append(dim)\n",
|
|
|
|
" points = np.random.normal(mean[0],sd[0],size=size)\n",
|
|
|
|
" for i in range(1,clusters):\n",
|
|
|
|
" points = np.concatenate((points,np.random.normal(mean[i],sd[i],size=size)),axis=0)\n",
|
|
|
|
" \n",
|
2022-11-29 12:15:07 +01:00
|
|
|
" return points, mean"
|
2022-11-28 11:40:47 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 4,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def distance(points,Pc): \n",
|
|
|
|
" return scipy.spatial.distance.cdist(points[:,:], Pc[:,:])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 5,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def kmeans(points = [0,0], K = 1):\n",
|
|
|
|
" # Initialisation K prototypes\n",
|
|
|
|
" dim = points.shape[1]\n",
|
|
|
|
" N = points.shape[0]\n",
|
|
|
|
" iter = 0\n",
|
|
|
|
" eps = 0.1\n",
|
|
|
|
" Pc_index = []\n",
|
|
|
|
" Pc_save = np.zeros([K,dim])\n",
|
|
|
|
" clusters = []\n",
|
|
|
|
"\n",
|
|
|
|
" for i in range(0,K):\n",
|
|
|
|
" Pc_index.append(np.random.randint(0,N))\n",
|
|
|
|
" Pc = points[Pc_index,:]\n",
|
|
|
|
"\n",
|
2022-12-05 18:14:46 +01:00
|
|
|
" while (np.mean(distance(Pc,Pc_save)) > eps and iter < 3):\n",
|
2022-11-28 11:40:47 +01:00
|
|
|
" iter += 1\n",
|
|
|
|
" Pc_save = Pc\n",
|
|
|
|
" # print(Pc)\n",
|
|
|
|
" # print(points[:,:Pc.shape[0]])\n",
|
|
|
|
" dist = distance(points=points[:,:Pc.shape[1]],Pc=Pc)\n",
|
|
|
|
" clust = np.argmin(dist, axis=1)\n",
|
|
|
|
" clust = np.expand_dims(clust, axis=0)\n",
|
|
|
|
" points = np.append(points[:,:Pc.shape[1]], clust.T, axis=1)\n",
|
|
|
|
" # print(points)\n",
|
|
|
|
" Pc = np.zeros([K,dim])\n",
|
|
|
|
" index = np.array([])\n",
|
|
|
|
"\n",
|
|
|
|
" for n in range(0,N):\n",
|
|
|
|
" for k in range(0,K):\n",
|
|
|
|
" index = np.append(index, (clust==k).sum())\n",
|
|
|
|
" if points[n,-1] == k:\n",
|
|
|
|
" # print(points)\n",
|
|
|
|
" # print(Pc)\n",
|
|
|
|
" Pc[k,:] = np.add(Pc[k,:], points[n,:-1])\n",
|
|
|
|
"\n",
|
|
|
|
" for k in range(0,K):\n",
|
|
|
|
" Pc[k,:] = np.divide(Pc[k,:],index[k])\n",
|
|
|
|
"\n",
|
|
|
|
" # print(Pc)\n",
|
2022-11-29 12:15:07 +01:00
|
|
|
" indice = points[:,-1]\n",
|
2022-11-28 11:40:47 +01:00
|
|
|
" points = points[:,:-1]\n",
|
2022-11-29 12:15:07 +01:00
|
|
|
" return Pc, indice, points\n"
|
2022-11-28 11:40:47 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 6,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"colors=['red', 'green','yellow','blue','purple', 'orange']\n",
|
|
|
|
"def visualisation(points, index, Pc=[0,0], K=1):\n",
|
|
|
|
" if(points.shape[1]==2):\n",
|
|
|
|
" # for k in range(0,K):\n",
|
|
|
|
" for n in range(0,len(points)):\n",
|
|
|
|
" plt.plot(points[n,0], points[n,1], 'o', color=colors[int(index[n])])\n",
|
|
|
|
" plt.plot(Pc[:,0],Pc[:,1],'r+')\n",
|
|
|
|
" plt.grid(True)\n",
|
|
|
|
" plt.axis([min(mean)-1,max(mean)+1,min(mean)-1,max(mean)+1])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 7,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def img_2_mat(my_img):\n",
|
|
|
|
" mat = my_img.reshape(my_img.shape[0]*my_img.shape[1],my_img.shape[2])\n",
|
|
|
|
" return mat"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 8,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def mat_2_img(mat,my_img):\n",
|
|
|
|
" img_seg = mat.reshape(my_img.shape[0], my_img.shape[1], my_img.shape[2])\n",
|
|
|
|
" return img_seg"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 9,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def kmeans_image(path_image, K):\n",
|
|
|
|
" my_img = io.imread(path_image)\n",
|
|
|
|
" imgplot = plt.imshow(my_img)\n",
|
|
|
|
" Mat = img_2_mat(my_img)\n",
|
|
|
|
" \n",
|
|
|
|
" Pc, index, clusters = kmeans(Mat, K)\n",
|
|
|
|
"\n",
|
2022-11-29 12:15:07 +01:00
|
|
|
" for k in range(Mat.shape[0]):\n",
|
|
|
|
" Mat[k,:] = np.floor(Pc[index[k],:])\n",
|
2022-11-28 11:40:47 +01:00
|
|
|
"\n",
|
|
|
|
" img_seg = mat_2_img(Mat, my_img)\n",
|
|
|
|
"\n",
|
|
|
|
" io.imsave(path_image.split('.')[0] + \"_%d.jpg\" % K, img_seg)\n",
|
|
|
|
" imgplot = plt.imshow(img_seg)\n",
|
|
|
|
" return Pc, index, img_seg\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 10,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2022-11-29 12:15:07 +01:00
|
|
|
"points, mean = gen_points(mean,sd,nb,dim,clusters)\n",
|
2022-11-28 11:40:47 +01:00
|
|
|
"# print(points.shape)\n",
|
|
|
|
"# print(points.mean(axis=0))\n",
|
|
|
|
"# print(points)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 11,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"dist = distance(points,points)\n",
|
|
|
|
"# print(dist)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 12,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"Pc, index, clusters = kmeans(points,K=K)\n",
|
|
|
|
"# print(index)\n",
|
|
|
|
"# print(clusters)\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 13,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
2022-12-22 19:38:17 +01:00
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAzZUlEQVR4nO2df3QcZ3nvP4+EIkcRKMTG25wQrZIS6OWiEpBP8G1zb+TIPScxddL2pvcqdxMcwFVtLiVum9NSdEtsDqK01xQHQgDjAA7aE9ELgSTUORzsWAZ6MWBzkyghTWISSSRN5cQGJYoS2ZGe+8fMSqPVzP6cnZ1dPZ9z5mh35p2ZZ0e733nneZ73eUVVMQzDMOqfhmobYBiGYUSDCb5hGMYywQTfMAxjmWCCbxiGsUwwwTcMw1gmvKbaBgSxatUq7ejomH//0ksvcdZZZ1XPoAKIu41xtw/ib6PZBydfPskzLzzDqdlTnNF4Bue97jzOOfOcgve3a1ge+ew7evTo86r6Bt+NqhrLpaurS70cPHhQ407cbYy7farxt3G52zf40KC2DLQo25lfWgZadPChwYKPsdyvYbnksw84ogG6WrZLR0RWiMhPRORBEXlERHb4tLlBRJ4TkQfcZXO55zUMI3r6D/QzfXp60brp09P0H+ivkkVGMYThw58BLlfVtwMXA1eIyFqfdl9X1YvdZU8I5zUMI2LGJ8eLWm8UT3okTceuDhp2NNCxq4P0SDq0Y5ct+O5TxJT7tsldbPiuY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b90iNpdj6+k7HJMQAUnRf9THyGaU4G7W8+fMPwUGvzwOYbWPW+u9/HqVnnCX9scoz33f0+oH6qP1a
|
2022-11-28 11:40:47 +01:00
|
|
|
"text/plain": [
|
2022-12-08 22:12:07 +01:00
|
|
|
"<Figure size 432x288 with 1 Axes>"
|
2022-11-28 11:40:47 +01:00
|
|
|
]
|
|
|
|
},
|
2022-12-08 22:12:07 +01:00
|
|
|
"metadata": {
|
|
|
|
"needs_background": "light"
|
|
|
|
},
|
2022-11-28 11:40:47 +01:00
|
|
|
"output_type": "display_data"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"visualisation(clusters, index, Pc, K=K)\n",
|
|
|
|
"# print(Pc)\n",
|
|
|
|
"# print(mean)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-22 19:38:17 +01:00
|
|
|
"execution_count": 14,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
2022-12-22 19:38:17 +01:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"ename": "KeyboardInterrupt",
|
|
|
|
"evalue": "",
|
|
|
|
"output_type": "error",
|
|
|
|
"traceback": [
|
|
|
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
|
|
|
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
|
|
|
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_20188\\152532697.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mPc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mimg_seg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkmeans_image\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath_image\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpath_image\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mK\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m250\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
|
|
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_20188\\1699160287.py\u001b[0m in \u001b[0;36mkmeans_image\u001b[1;34m(path_image, K)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mMat\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mimg_2_mat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_img\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mPc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mclusters\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkmeans\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mMat\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mK\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mMat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
|
|
|
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_20188\\1194225063.py\u001b[0m in \u001b[0;36mkmeans\u001b[1;34m(points, K)\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mK\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 30\u001b[1;33m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mclust\u001b[0m\u001b[1;33m==\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 31\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpoints\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 32\u001b[0m \u001b[1;31m# print(points)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
|
|
|
"\u001b[1;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mappend\u001b[1;34m(*args, **kwargs)\u001b[0m\n",
|
|
|
|
"\u001b[1;32mc:\\Users\\Sasa\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\numpy\\lib\\function_base.py\u001b[0m in \u001b[0;36mappend\u001b[1;34m(arr, values, axis)\u001b[0m\n\u001b[0;32m 4815\u001b[0m \u001b[0mvalues\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4816\u001b[0m \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndim\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4817\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mconcatenate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4818\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4819\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
|
|
|
|
"\u001b[1;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mconcatenate\u001b[1;34m(*args, **kwargs)\u001b[0m\n",
|
|
|
|
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAUgAAAD8CAYAAAAVOD3kAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOz9V5Bt2XnfCf6+tdZ2x6bPa+qWr0LB0YAQPdUUKU91a1rq0UxPTPc8TISe5n30PE8dMW8T45rR0THT6lCL1IhSSyIpigYASQCCLaCAKpS/99Y16c1x2661vnnY5xaKTk2QBAoQ8n8j82aec3KfE5n7/Pdn/t//E1XlCle4whWu8Edh3u8XcIUrXOEK3624IsgrXOEKV/gTcEWQV7jCFa7wJ+CKIK9whStc4U/AFUFe4QpXuMKfgCuCvMIVrnCFPwHfNoIUkb8pIq+JyJsi8o++Xc9zhStc4QrfLsi3QwcpIhZ4HfhrwH3gC8B/qaqv/IU/2RWucIUrfJvw7YogfxR4U1XfVtUW+KfA3/02PdcVrnCFK3xb4L5Nx70J3HvP9/eBH/uTHryzs6NPPvnkt+mlXOEKfxD6h74SBIX15/fcJfKnOgrI+hZ99z7BrB+j73nMf+h4V3g/8aUvfelUVXf/8O3fLoL8X4SI/EPgHwI8/vjjfPGLX3y/XsoV/iPEo9KRrEnuD5SSoiKPyEoiKgoEolpUHWIAVZBI1IgRUBUUxYgBDI8OZzUiWFTXnErPqyqPCNMjov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jYI6g48ducnZMpHEJcy2U95+5VNc3/8QLzzzcazZQNaNMJGr9Pr9xBVBXuFPjRADd++/STaynM7eZl49oG7
|
|
|
|
"text/plain": [
|
|
|
|
"<Figure size 432x288 with 1 Axes>"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"metadata": {
|
|
|
|
"needs_background": "light"
|
|
|
|
},
|
|
|
|
"output_type": "display_data"
|
|
|
|
}
|
|
|
|
],
|
2022-11-28 11:40:47 +01:00
|
|
|
"source": [
|
2022-12-08 22:12:07 +01:00
|
|
|
"Pc, index, img_seg = kmeans_image(path_image=path_image, K=250)"
|
2022-11-28 11:40:47 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-05 18:14:46 +01:00
|
|
|
"execution_count": null,
|
2022-11-28 11:40:47 +01:00
|
|
|
"metadata": {},
|
2022-12-08 22:12:07 +01:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"(103230,)\n",
|
|
|
|
"[[252.69560225 251.67998184 249.86355505]\n",
|
|
|
|
" [ 69.54698482 50.76095445 17.63930586]\n",
|
|
|
|
" [228.4210873 174.61140875 69.5397857 ]\n",
|
|
|
|
" [154.87751731 83.70097198 45.64258942]\n",
|
|
|
|
" [ nan nan nan]]\n",
|
|
|
|
"[[[252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]]\n",
|
|
|
|
"\n",
|
|
|
|
" [[252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]]\n",
|
|
|
|
"\n",
|
|
|
|
" [[252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]]\n",
|
|
|
|
"\n",
|
|
|
|
" ...\n",
|
|
|
|
"\n",
|
|
|
|
" [[252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]]\n",
|
|
|
|
"\n",
|
|
|
|
" [[252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]]\n",
|
|
|
|
"\n",
|
|
|
|
" [[252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" ...\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]\n",
|
|
|
|
" [252 251 249]]]\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2022-11-28 11:40:47 +01:00
|
|
|
"source": [
|
2022-12-08 22:12:07 +01:00
|
|
|
"print(index.shape)\n",
|
|
|
|
"print(Pc)\n",
|
|
|
|
"print(img_seg)"
|
2022-11-28 11:40:47 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
2022-12-08 22:12:07 +01:00
|
|
|
"display_name": "Python 3.9.4 64-bit",
|
2022-11-28 11:40:47 +01:00
|
|
|
"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",
|
2022-12-22 19:38:17 +01:00
|
|
|
"version": "3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]"
|
2022-11-28 11:40:47 +01:00
|
|
|
},
|
|
|
|
"orig_nbformat": 4,
|
|
|
|
"vscode": {
|
|
|
|
"interpreter": {
|
2022-12-08 22:12:07 +01:00
|
|
|
"hash": "2ef431f6525756fa8a44688585fa332ef3b2e5fcfe8fe75df35bbf7028a8b511"
|
2022-11-28 11:40:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|