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.

colourDetection.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -- coding: UTF-8
  2. import cv2
  3. import matplotlib.pyplot as plt
  4. from sklearn.cluster import KMeans
  5. import os
  6. boundaries = [
  7. ([100,80,0],[240,220,110]), # yellow
  8. ([0,40,50],[110,180,250]), # blue
  9. ([0,60,0],[60,160,70]), # green
  10. ]
  11. color_attr = ["黄牌","蓝牌",'绿牌','白牌','黑牌']
  12. threhold_green = 13
  13. threhold_blue = 13
  14. threhold_yellow1 = 50
  15. threhold_yellow2 = 70
  16. # plt.figure()
  17. # plt.axis("off")
  18. # plt.imshow(image)
  19. # plt.show()
  20. import numpy as np
  21. def centroid_histogram(clt):
  22. numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
  23. (hist, _) = np.histogram(clt.labels_, bins=numLabels)
  24. # normalize the histogram, such that it sums to one
  25. hist = hist.astype("float")
  26. hist /= hist.sum()
  27. # return the histogram
  28. return hist
  29. def plot_colors(hist, centroids):
  30. bar = np.zeros((50, 300, 3), dtype="uint8")
  31. startX = 0
  32. for (percent, color) in zip(hist, centroids):
  33. endX = startX + (percent * 300)
  34. cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
  35. color.astype("uint8").tolist(), -1)
  36. startX = endX
  37. # return the bar chart
  38. return bar
  39. def search_boundaries(color):
  40. for i,color_bound in enumerate(boundaries):
  41. if np.all(color >= color_bound[0]) and np.all(color <= color_bound[1]):
  42. return i
  43. return -1
  44. def judge_color(color):
  45. r = color[0]
  46. g = color[1]
  47. b = color[2]
  48. if g - r >= threhold_green and g - b >= threhold_green:
  49. return 2
  50. if b - r >= threhold_blue and b - g >= threhold_blue:
  51. return 1
  52. if r- b > threhold_yellow2 and g - b > threhold_yellow2:
  53. return 0
  54. if r > 200 and b > 200 and g > 200:
  55. return 3
  56. if r < 50 and b < 50 and g < 50:
  57. return 4
  58. return -1
  59. def judge_plate_color(img):
  60. image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  61. image = image.reshape((image.shape[0] * image.shape[1], 3))
  62. clt = KMeans(n_clusters=2)
  63. clt.fit(image)
  64. hist = centroid_histogram(clt)
  65. index = np.argmax(hist)
  66. #print clt.cluster_centers_[index]
  67. #color_index = search_boundaries(clt.cluster_centers_[index])
  68. color_index = judge_color(clt.cluster_centers_[index])
  69. if color_index == -1:
  70. if index == 0:
  71. secound_index = 1
  72. else:
  73. secound_index = 0
  74. color_index = judge_color(clt.cluster_centers_[secound_index])
  75. if color_index == -1:
  76. print clt.cluster_centers_
  77. bar = plot_colors(hist, clt.cluster_centers_)
  78. # show our color bart
  79. plt.figure()
  80. plt.axis("off")
  81. plt.imshow(bar)
  82. plt.show()
  83. if color_index != -1:
  84. return color_attr[color_index],clt.cluster_centers_[index]
  85. else:
  86. return None,clt.cluster_centers_[index]