coffee_linescan2-1(1time1000pic).py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import imutils
  5. from collections import defaultdict
  6. #import datetime
  7. #start = datetime.datetime.now()
  8. import time
  9. start = time.process_time()
  10. def makepic():
  11. count = 11
  12. for x in range(count):
  13. img = np.ones((1, 60), dtype=np.uint8)
  14. img[0, range(0, 20)] = 250
  15. img[0, range(20, 40)] = 250
  16. img[0, range(40, 60)] = 250
  17. filename = 'make' + str(x) + '.bmp'
  18. #print(filename)
  19. cv2.imwrite(filename, img)
  20. for x in range(11, 18):
  21. img = np.ones((1, 60), dtype=np.uint8)
  22. img[0, range(0, 20)] = 250
  23. img[0, range(20, 40)] = 30
  24. img[0, range(40, 60)] = 250
  25. filename = 'make' + str(x) + '.bmp'
  26. #print(filename)
  27. cv2.imwrite(filename, img)
  28. count = 968
  29. for x in range(18, count):
  30. img = np.ones((1, 60), dtype=np.uint8)
  31. img[0, range(0, 20)] = 250
  32. img[0, range(20, 40)] = 30
  33. img[0, range(40, 60)] = 250
  34. filename = 'make' + str(x) + '.bmp'
  35. #print(filename)
  36. cv2.imwrite(filename, img)
  37. for x in range(968, 1000):
  38. img = np.ones((1, 60), dtype=np.uint8)
  39. img[0, range(0, 20)] = 250
  40. img[0, range(20, 40)] = 250
  41. img[0, range(40, 60)] = 250
  42. filename = 'make' + str(x) + '.bmp'
  43. #print(filename)
  44. cv2.imwrite(filename, img)
  45. def detectionpic():
  46. image = cv2.imread("make0.bmp")
  47. image2 = cv2.imread("make1.bmp")
  48. image_v = cv2.vconcat([image, image2]) # 垂直組合
  49. count = 998
  50. for x in range(count):
  51. # 讀取圖檔
  52. filename = 'make' + str(int(x * 1)) + '.bmp'
  53. #print(filename)
  54. image = cv2.imread(filename)
  55. image_v = cv2.vconcat([image_v, image])
  56. #cv2.imwrite("total.bmp", image_v)
  57. # 讀取圖檔
  58. filename = 'total.bmp'
  59. image = cv2.imread(filename)
  60. # 轉為灰階
  61. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  62. # 模糊化圖片
  63. blurred = cv2.GaussianBlur(gray, (11, 11), 0)
  64. #cv2.imshow('Blur',blurred)
  65. #cv2.waitKey(0)
  66. # Canny 尋找邊緣
  67. edged = cv2.Canny(blurred, 30, 150)
  68. #cv2.imshow('Edged',edged)
  69. #cv2.waitKey(0)
  70. # findContour 確定輪廓
  71. _,contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  72. img2 = image.copy()
  73. cv2.drawContours(img2, contours, -1, (0, 255, 0), 2)
  74. #cv2.imshow('contour',img2)
  75. #cv2.imwrite('check.bmp',img2)
  76. #cv2.waitKey(0)
  77. cnt = contours[0]
  78. #contourArea 計算面積
  79. area = cv2.contourArea(cnt) #計算面積
  80. perimeter = cv2.arcLength(cnt,True) #計算周長
  81. if area < 19119:
  82. print('破損豆噴調',area)
  83. else:
  84. pass
  85. print('面積:',area, '周長:',perimeter) #印出結果
  86. '''
  87. p = 0
  88. d = 0
  89. tt = np.array(gray[0])
  90. print(tt)
  91. count = len(tt)
  92. print(len(tt))
  93. for i in tt[:]:
  94. if i >= 150:
  95. p = p + 1
  96. elif i <= 130:
  97. d = d + 1
  98. print('要得豆子像素:',p)
  99. print('壞的豆子像素:',d)
  100. coffeebean = (d / count)*100
  101. print('壞豆佔全部百分比:',coffeebean)
  102. if coffeebean > 60:
  103. record = record + 1
  104. print('紀錄次數:', record)
  105. else:
  106. pass
  107. if record == 50:
  108. j = 1
  109. # record=0
  110. print('噴嘴啟動')
  111. # break
  112. # continue
  113. '''
  114. makepic()
  115. detectionpic()
  116. #end = datetime.datetime.now()
  117. #print("執行時間:", end - start)
  118. # 結束測量
  119. end = time.process_time()
  120. # 輸出結果
  121. print("執行時間:%f 秒" % (end - start))