You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

2-kmeans-color-vq.ipynb 480 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 用K-means进行颜色量化\n",
  8. "\n",
  9. "对给定的图像进行**像素矢量量化(VQ)**,将显示图像所需的颜色从96,615种减少到64种,同时保持整体外观质量。\n",
  10. "\n",
  11. "\n",
  12. "在本例中,像素在3D空间中表示,使用K-means找到64个颜色簇。在图像处理文献中,由K-means(聚类中心)得到的码本称为调色板。使用单个字节,最多可以寻址256种颜色,而RGB编码需要每个像素3个字节。例如,GIF文件格式就使用了这样一个调色板。\n",
  13. "\n"
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": 3,
  19. "metadata": {},
  20. "outputs": [],
  21. "source": [
  22. "%matplotlib inline\n",
  23. "import numpy as np\n",
  24. "import matplotlib.pyplot as plt\n",
  25. "from sklearn.cluster import KMeans\n",
  26. "from sklearn.metrics import pairwise_distances_argmin\n",
  27. "from sklearn.datasets import load_sample_image\n",
  28. "from sklearn.utils import shuffle\n",
  29. "from time import time"
  30. ]
  31. },
  32. {
  33. "cell_type": "code",
  34. "execution_count": 13,
  35. "metadata": {},
  36. "outputs": [
  37. {
  38. "name": "stdout",
  39. "output_type": "stream",
  40. "text": [
  41. "Fitting model on a small sub-sample of the data\n",
  42. " done in 0.239s.\n",
  43. "Predicting color indices on the full image (k-means)\n",
  44. " done in 0.012s.\n",
  45. "Predicting color indices on the full image (random)\n",
  46. "(273280,)\n",
  47. " done in 0.203s.\n"
  48. ]
  49. }
  50. ],
  51. "source": [
  52. "n_colors = 64\n",
  53. "\n",
  54. "# 加载图像\n",
  55. "img = load_sample_image(\"china.jpg\")\n",
  56. "\n",
  57. "# 转化为浮点数而不是默认的8位整数编码。\n",
  58. "# 除以255是重要的这样plt.imshow在浮点数(需要在[0-1]的范围)上的表现会很好 \n",
  59. "\n",
  60. "img = np.array(img, dtype=np.float64) / 255\n",
  61. "\n",
  62. "# 加载图像并转化成2D的numpy数组。\n",
  63. "w, h, d = original_shape = tuple(img.shape)\n",
  64. "assert d == 3\n",
  65. "image_array = np.reshape(img, (w * h, d))\n",
  66. "\n",
  67. "print(\"Fitting model on a small sub-sample of the data\")\n",
  68. "t0 = time()\n",
  69. "image_array_sample = shuffle(image_array, \n",
  70. " random_state=0)[:1000]\n",
  71. "#image_array_sample = image_array[:1000]\n",
  72. "kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(\n",
  73. " image_array_sample)\n",
  74. "print(\" done in %0.3fs.\" % (time() - t0))\n",
  75. "\n",
  76. "# 获得所有点的标签\n",
  77. "print(\"Predicting color indices on the full image (k-means)\")\n",
  78. "t0 = time()\n",
  79. "labels = kmeans.predict(image_array)\n",
  80. "print(\" done in %0.3fs.\" % (time() - t0))\n",
  81. "\n",
  82. "\n",
  83. "codebook_random = shuffle(image_array, \n",
  84. " random_state=0)[:n_colors + 1]\n",
  85. "print(\"Predicting color indices on the full image (random)\")\n",
  86. "t0 = time()\n",
  87. "# FIXME: 需要确认这个函数的具体操作,和手册的不一致\n",
  88. "labels_random = pairwise_distances_argmin(codebook_random,\n",
  89. " image_array,\n",
  90. " axis=0)\n",
  91. "print(labels_random.shape)\n",
  92. "print(\" done in %0.3fs.\" % (time() - t0))\n",
  93. "\n",
  94. "\n",
  95. "def recreate_image(codebook, labels, w, h):\n",
  96. " \"\"\"Recreate the (compressed) image from the code book & labels\"\"\"\n",
  97. " d = codebook.shape[1]\n",
  98. " image = np.zeros((w, h, d))\n",
  99. " label_idx = 0\n",
  100. " for i in range(w):\n",
  101. " for j in range(h):\n",
  102. " image[i][j] = codebook[labels[label_idx]]\n",
  103. " label_idx += 1\n",
  104. " return image"
  105. ]
  106. },
  107. {
  108. "cell_type": "code",
  109. "execution_count": 14,
  110. "metadata": {},
  111. "outputs": [
  112. {
  113. "data": {
  114. "text/plain": [
  115. "<matplotlib.image.AxesImage at 0x7f33032cde10>"
  116. ]
  117. },
  118. "execution_count": 14,
  119. "metadata": {},
  120. "output_type": "execute_result"
  121. },
  122. {
  123. "data": {

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。