经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
机器学习——图像训练
来源:cnblogs  作者:抿嘴唇  时间:2018/10/16 9:35:13  对本文有异议

图片经过处理后图片会变成黑白无色彩的图像,但可以大概观察到图片中主体的轮廓信息,而还原后的图片的主体对象会被保留,图片中其他内容会变模糊,,主体对象得以突出,通过机器学习完成对图片的信息的提取,图片信息可以保存到本地像素查询本或数据库中

导入类库

  1. 1 import numpy as np
  2. 2 import cv2
  3. 3 import matplotlib.pyplot as plt
  4. 4 from sklearn.cluster import KMeans
  5. 5 from sklearn.utils import shuffle
  6. 6 from time import time
  7. 7 from skimage import io

提取和存储图像数据

  1. 1 n_colors = 64
  2. 2 # 读取图片像素数据
  3. 3 tiger = cv2.imread("tiger.jpg")
  4. 4 # print('tiger >>>>',tiger)
  5. 5 login = cv2.imread('login.png')
  6. 6 # 将图片像素数据标准化为0-1的数据并保存至数组中
  7. 7 china = np.array(tiger, dtype=np.float64) / 255
  8. 8 # print('china >>>>',china)
  9. 9 w, h, d = original_shape = tuple(china.shape)
  10. 10 print('original_shape >>>>', original_shape)
  11. 11 print('w,h,d >>>>', w, h, d) # w:层数,h行数,d列数
  12. 12
  13. 13 image_array = np.reshape(china, (w * h, d))
  14. 14 # 每个点作为一个样本,维数为3,将三维数组china化为二维数组,列数不变,行数变为原行数乘层数
  15. 15 # print('image_array >>>>', image_array)
  16. 16 print('image_array shape>>>>', image_array.shape)

训练图像数据

  1. 1 t0 = time()
  2. 2 # 将所有点打乱顺序,取前1000个点
  3. 3 # 不使用所有点主要是为了训练模型的速度
  4. 4 image_array_sample = shuffle(image_array, random_state=0)[:1000]
  5. 5
  6. 6 # 训练图片像素数据,将像素数据分为64类
  7. 7 # random_state = 0用来重现同样的结果,不设置则每次都是不同的随机结果
  8. 8 kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(image_array_sample)
  9. 9 print("done in %0.3fs." % (time() - t0)) # 查看训练时间

预测

  1. 1 print("Predicting color indices on the full image (k-means)")
  2. 2 t0 = time()
  3. 3 # 预测数据分类,image_array(921600,3)二维数组预测完毕后的结果labels是一维数组
  4. 4 labels = kmeans.predict(image_array)
  5. 5 print("done in %0.3fs." % (time() - t0)) # 查看预测时间
  6. 6 print(labels)
  7. 7 print(labels.shape)
  8. 8 # 将labels从一维数组化为二维数组
  9. 9 labels = labels.reshape(w, h)
  10. 10 print(labels)
  11. 11 print(labels.shape)

保存像素查询本和处理后的图像

  1. 1 def save_compress_data():
  2. 2 np.save('codebook_tiger.npy', kmeans.cluster_centers_)
  3. 3 io.imsave('compressed_tiger.png', labels)

还原图像

  1. 1 def recreate_image(codebook, labels, w, h):
  2. 2 # Recreate the (compressed) image from the code book & labels
  3. 3 # 每个像素查询码本(对应0~63),取得其对应的像素值
  4. 4 d = codebook.shape[1]
  5. 5 image = np.zeros((w, h, d))
  6. 6 label_idx = 0
  7. 7 for i in range(w):
  8. 8 for j in range(h):
  9. 9 image[i][j] = codebook[labels[i, j]]
  10. 10 label_idx += 1
  11. 11 print('还原出的图像 >>>>', image)
  12. 12 return image

执行代码

  1. 1 # save_compress_data()
  2. 2 centers = np.load('codebook_tiger.npy') # 像素查询码本
  3. 3 c_image = io.imread('compressed_tiger.png') # 这张图片里的像素已经过分类
  4. 4 print('像素查询本 >>>>', centers)
  5. 5 print(centers.shape)
  6. 6 print(centers.shape[1]) # 0是行数,1是列数
  7. 7 print('压缩的图像 >>>>', c_image)
  8. 8 print(c_image.shape)
  9. 9
  10. 10 cv2.imshow("new", recreate_image(centers, c_image, w, h))
  11. 11 cv2.waitKey(0)
  1. '''
  2. 取出压缩后图像的每一个数据即像素分类id(labels[i, j]),在像素查询本中查找该分类对应的三位像素一行数据(codebook[labels[i, j]]),
  3. 赋予新的image对象(无需指定列数,三位像素即3列)
  4. original_shape >>>> (720, 1280, 3)
  5. w,h,d >>>> 720 1280 3
  6. image_array shape>>>> (921600, 3)
  7. done in 0.583s.
  8. Predicting color indices on the full image (k-means)
  9. done in 0.740s.
  10. [10 10 10 ... 60 60 60]
  11. (921600,)
  12. [[10 10 10 ... 42 8 42]
  13. [10 10 10 ... 42 42 8]
  14. [10 10 10 ... 42 42 8]
  15. ...
  16. [ 3 3 36 ... 60 60 60]
  17. [ 3 3 36 ... 60 60 60]
  18. [ 3 3 36 ... 60 60 60]]
  19. (720, 1280)
  20. 像素查询本 >>>> [[0.26901961 0.27607843 0.35686275]
  21. [0.67189542 0.66887883 0.66747109]
  22. [0.09971989 0.09271709 0.10028011]
  23. [0.38901961 0.38184874 0.39383754]
  24. [0.83529412 0.8285205 0.83333333]
  25. [0.4402852 0.57468806 0.71764706]
  26. [0.46849673 0.49947712 0.47712418]
  27. [0.19622926 0.26651584 0.24494721]
  28. [0.30539216 0.45 0.60441176]
  29. [0.14444444 0.50359477 0.22581699]
  30. [0.64325609 0.63850267 0.62923351]
  31. [0.75148874 0.74524328 0.7405955 ]
  32. [0.18221289 0.18585434 0.20770308]
  33. [0.55294118 0.69233512 0.8631016 ]
  34. [0.09656863 0.28039216 0.43823529]
  35. [0.59155354 0.58924082 0.57797888]
  36. [0.53202614 0.38039216 0.7124183 ]
  37. [0.90756303 0.90364146 0.90140056]
  38. [0.3713555 0.46683717 0.54339301]
  39. [0.31215686 0.54352941 0.43372549]
  40. [0.23504902 0.32254902 0.30563725]
  41. [0.29694989 0.37342048 0.42461874]
  42. [0.19117647 0.32622549 0.4495098 ]
  43. [0.18431373 0.55757576 0.30516934]
  44. [0.70718954 0.76601307 0.83529412]
  45. [0.51328976 0.50544662 0.51568627]
  46. [0.36705882 0.23921569 0.61098039]
  47. [0.44416027 0.42983802 0.43631714]
  48. [0.10053476 0.16363636 0.23600713]
  49. [0.15022624 0.14434389 0.15806938]
  50. [0.40452489 0.50497738 0.59457014]
  51. [0.51265597 0.63030303 0.78324421]
  52. [0.29215686 0.30392157 0.30056022]
  53. [0.06876751 0.10294118 0.15644258]
  54. [0.0455243 0.05268542 0.06479113]
  55. [0.95294118 0.95294118 0.95803922]
  56. [0.34232026 0.35065359 0.33970588]
  57. [0.56254902 0.55431373 0.54196078]
  58. [0.2745098 0.49063181 0.27973856]
  59. [0.78676471 0.7814951 0.78112745]
  60. [0.21411765 0.20627451 0.43176471]
  61. [0.34196078 0.55843137 0.35803922]
  62. [0.36470588 0.5027451 0.66352941]
  63. [0.47189542 0.67973856 0.56078431]
  64. [0.49084967 0.55294118 0.63300654]
  65. [0.10889894 0.20301659 0.32488688]
  66. [0.65228758 0.78823529 0.92222222]
  67. [0.3372549 0.53411765 0.7427451 ]
  68. [0.71036415 0.70672269 0.69677871]
  69. [0.17019608 0.26431373 0.3696732 ]
  70. [0.39063181 0.44814815 0.40217865]
  71. [0.57303922 0.34656863 0.81617647]
  72. [0.28039216 0.37385621 0.49477124]
  73. [0.33440285 0.42816399 0.49411765]
  74. [0.43137255 0.61019608 0.81882353]
  75. [0.25743945 0.24705882 0.24313725]
  76. [0.27088989 0.34901961 0.36651584]
  77. [0.52990196 0.59215686 0.54166667]
  78. [0.39607843 0.55196078 0.51470588]
  79. [0.87511312 0.87179487 0.87722474]
  80. [0.41137255 0.46196078 0.46745098]
  81. [0.23627451 0.26470588 0.29362745]
  82. [0.1254902 0.44248366 0.18431373]
  83. [0.61265597 0.6197861 0.60463458]]
  84. (64, 3)
  85. 3
  86. 压缩的图像 >>>> [[10 10 10 ... 42 8 42]
  87. [10 10 10 ... 42 42 8]
  88. [10 10 10 ... 42 42 8]
  89. ...
  90. [ 3 3 36 ... 60 60 60]
  91. [ 3 3 36 ... 60 60 60]
  92. [ 3 3 36 ... 60 60 60]]
  93. (720, 1280)
  94. 还原出的图像 >>>> [[[0.64325609 0.63850267 0.62923351]
  95. [0.64325609 0.63850267 0.62923351]
  96. [0.64325609 0.63850267 0.62923351]
  97. ...
  98. [0.36470588 0.5027451 0.66352941]
  99. [0.30539216 0.45 0.60441176]
  100. [0.36470588 0.5027451 0.66352941]]
  101.  
  102. [[0.64325609 0.63850267 0.62923351]
  103. [0.64325609 0.63850267 0.62923351]
  104. [0.64325609 0.63850267 0.62923351]
  105. ...
  106. [0.36470588 0.5027451 0.66352941]
  107. [0.36470588 0.5027451 0.66352941]
  108. [0.30539216 0.45 0.60441176]]
  109.  
  110. [[0.64325609 0.63850267 0.62923351]
  111. [0.64325609 0.63850267 0.62923351]
  112. [0.64325609 0.63850267 0.62923351]
  113. ...
  114. [0.36470588 0.5027451 0.66352941]
  115. [0.36470588 0.5027451 0.66352941]
  116. [0.30539216 0.45 0.60441176]]
  117.  
  118. ...
  119.  
  120. [[0.38901961 0.38184874 0.39383754]
  121. [0.38901961 0.38184874 0.39383754]
  122. [0.34232026 0.35065359 0.33970588]
  123. ...
  124. [0.41137255 0.46196078 0.46745098]
  125. [0.41137255 0.46196078 0.46745098]
  126. [0.41137255 0.46196078 0.46745098]]
  127.  
  128. [[0.38901961 0.38184874 0.39383754]
  129. [0.38901961 0.38184874 0.39383754]
  130. [0.34232026 0.35065359 0.33970588]
  131. ...
  132. [0.41137255 0.46196078 0.46745098]
  133. [0.41137255 0.46196078 0.46745098]
  134. [0.41137255 0.46196078 0.46745098]]
  135.  
  136. [[0.38901961 0.38184874 0.39383754]
  137. [0.38901961 0.38184874 0.39383754]
  138. [0.34232026 0.35065359 0.33970588]
  139. ...
  140. [0.41137255 0.46196078 0.46745098]
  141. [0.41137255 0.46196078 0.46745098]
  142. [0.41137255 0.46196078 0.46745098]]]
  143.  
  144. '''

  

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号