coffee_linescan1(1000time1pic).py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import imutils
  5. #import datetime
  6. #start = datetime.datetime.now()#
  7. import time
  8. start = time.process_time()
  9. count = 1000
  10. record = 0
  11. for x in range(count):
  12. img = np.ones((1, 60), dtype=np.uint8)
  13. img[0, range(0, 20)] = 30
  14. img[0, range(20, 40)] = 125
  15. img[0, range(40, 60)] = 250
  16. filename = 'make' + str(x) + '.bmp'
  17. print(filename)
  18. cv2.imwrite(filename, img)
  19. # 讀取圖檔
  20. image = cv2.imread(filename)
  21. # 轉為灰階
  22. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  23. p = 0
  24. d = 0
  25. tt = np.array(gray[0])
  26. count = len(tt)
  27. for i in tt[:]:
  28. if i >= 150:
  29. p = p + 1
  30. elif i <= 130:
  31. d = d + 1
  32. print('要得豆子像素:', p)
  33. print('壞的豆子像素:', d)
  34. coffeebean = (d / count) * 100
  35. print('壞豆佔全部百分比:', coffeebean)
  36. if coffeebean > 60:
  37. record = record + 1
  38. print('紀錄次數:', record)
  39. if record == 50:
  40. j = 1
  41. print('噴嘴啟動')
  42. # 結束測量
  43. end = time.process_time()
  44. # 輸出結果
  45. print("執行時間:%f 秒" % (end - start))