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

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