Browse Source

更新 'coffee_linescan/coffee_linescan1-1-1(1time1000pic).py'

fatwolf 3 years ago
parent
commit
b1bca44e9d
1 changed files with 142 additions and 142 deletions
  1. 142 142
      coffee_linescan/coffee_linescan1(1time1000pic).py

+ 142 - 142
coffee_linescan/coffee_linescan1(1time1000pic).py

@@ -1,142 +1,142 @@
-import cv2
-import numpy as np
-import matplotlib.pyplot as plt
-import imutils
-from collections import defaultdict
-#import datetime
-
-#start = datetime.datetime.now()
-import time
-start = time.process_time()
-def makepic():
-    count = 11
-    for x in range(count):
-        img = np.ones((1, 60), dtype=np.uint8)
-        img[0, range(0, 20)] = 250
-        img[0, range(20, 40)] = 250
-        img[0, range(40, 60)] = 250
-        filename = 'make' + str(x) + '.bmp'
-        #print(filename)
-        cv2.imwrite(filename, img)
-
-    for x in range(11, 18):
-        img = np.ones((1, 60), dtype=np.uint8)
-        img[0, range(0, 20)] = 250
-        img[0, range(20, 40)] = 30
-        img[0, range(40, 60)] = 250
-        filename = 'make' + str(x) + '.bmp'
-        #print(filename)
-        cv2.imwrite(filename, img)
-
-    count = 968
-    for x in range(18, count):
-        img = np.ones((1, 60), dtype=np.uint8)
-        img[0, range(0, 20)] = 250
-        img[0, range(20, 40)] = 30
-        img[0, range(40, 60)] = 250
-        filename = 'make' + str(x) + '.bmp'
-        #print(filename)
-        cv2.imwrite(filename, img)
-
-    for x in range(968, 1000):
-        img = np.ones((1, 60), dtype=np.uint8)
-        img[0, range(0, 20)] = 250
-        img[0, range(20, 40)] = 250
-        img[0, range(40, 60)] = 250
-        filename = 'make' + str(x) + '.bmp'
-        #print(filename)
-        cv2.imwrite(filename, img)
-
-
-
-
-def detectionpic():
-    image = cv2.imread("make0.bmp")
-    image2 = cv2.imread("make1.bmp")
-    image_v = cv2.vconcat([image, image2])  # 垂直組合
-
-    count = 998
-    for x in range(count):
-        # 讀取圖檔
-        filename = 'make' + str(int(x * 1)) + '.bmp'
-        #print(filename)
-        image = cv2.imread(filename)
-        image_v = cv2.vconcat([image_v, image])
-        #cv2.imwrite("total.bmp", image_v)
-    # 讀取圖檔
-    filename = 'total.bmp'
-    image = cv2.imread(filename)
-    # 轉為灰階
-    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-
-    # 模糊化圖片
-    blurred = cv2.GaussianBlur(gray, (11, 11), 0)
-    #cv2.imshow('Blur',blurred)
-    #cv2.waitKey(0)
-
-    # Canny 尋找邊緣
-    edged = cv2.Canny(blurred, 30, 150)
-    #cv2.imshow('Edged',edged)
-    #cv2.waitKey(0)
-
-    # findContour 確定輪廓
-    _,contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-    img2 = image.copy()
-    cv2.drawContours(img2, contours, -1, (0, 255, 0), 2)
-    #cv2.imshow('contour',img2)
-    #cv2.imwrite('check.bmp',img2)
-    #cv2.waitKey(0)
-
-    cnt = contours[0]
-
-    #contourArea 計算面積
-    area = cv2.contourArea(cnt)  #計算面積
-    perimeter = cv2.arcLength(cnt,True)    #計算周長
-
-    if area < 19119:
-        print('破損豆噴調',area)
-    else:
-        pass
-    print('面積:',area, '周長:',perimeter)  #印出結果
-    '''
-    p = 0
-    d = 0
-    tt = np.array(gray[0])
-    print(tt)
-    count = len(tt)
-    print(len(tt))
-    
-    for i in tt[:]:
-        if i >= 150:
-            p = p + 1
-        elif i <= 130:
-            d = d + 1
-    print('要得豆子像素:',p)
-    print('壞的豆子像素:',d)
-
-    coffeebean = (d / count)*100
-    print('壞豆佔全部百分比:',coffeebean)
-    if coffeebean > 60:
-        record = record + 1
-        print('紀錄次數:', record)
-    else:
-        pass
-    if record == 50:
-        j = 1
-        # record=0
-        print('噴嘴啟動')
-        # break
-        # continue
-'''
-makepic()
-detectionpic()
-#end = datetime.datetime.now()
-#print("執行時間:", end - start)
-# 結束測量
-end = time.process_time()
-# 輸出結果
-print("執行時間:%f 秒" % (end - start))
-
-
-
-
+import cv2
+import numpy as np
+import matplotlib.pyplot as plt
+import imutils
+from collections import defaultdict
+#import datetime
+
+#start = datetime.datetime.now()
+import time
+start = time.process_time()
+def makepic():
+    count = 11
+    for x in range(count):
+        img = np.ones((1, 60), dtype=np.uint8)
+        img[0, range(0, 20)] = 250
+        img[0, range(20, 40)] = 250
+        img[0, range(40, 60)] = 250
+        filename = 'make' + str(x) + '.bmp'
+        #print(filename)
+        cv2.imwrite(filename, img)
+
+    for x in range(11, 18):
+        img = np.ones((1, 60), dtype=np.uint8)
+        img[0, range(0, 20)] = 250
+        img[0, range(20, 40)] = 30
+        img[0, range(40, 60)] = 250
+        filename = 'make' + str(x) + '.bmp'
+        #print(filename)
+        cv2.imwrite(filename, img)
+
+    count = 968
+    for x in range(18, count):
+        img = np.ones((1, 60), dtype=np.uint8)
+        img[0, range(0, 20)] = 250
+        img[0, range(20, 40)] = 30
+        img[0, range(40, 60)] = 250
+        filename = 'make' + str(x) + '.bmp'
+        #print(filename)
+        cv2.imwrite(filename, img)
+
+    for x in range(968, 1000):
+        img = np.ones((1, 60), dtype=np.uint8)
+        img[0, range(0, 20)] = 250
+        img[0, range(20, 40)] = 250
+        img[0, range(40, 60)] = 250
+        filename = 'make' + str(x) + '.bmp'
+        #print(filename)
+        cv2.imwrite(filename, img)
+
+
+
+
+def detectionpic():
+    image = cv2.imread("make0.bmp")
+    image2 = cv2.imread("make1.bmp")
+    image_v = cv2.vconcat([image, image2])  # 垂直組合
+
+    count = 998
+    for x in range(count):
+        # 讀取圖檔
+        filename = 'make' + str(int(x * 1)) + '.bmp'
+        #print(filename)
+        image = cv2.imread(filename)
+        image_v = cv2.vconcat([image_v, image])
+        #cv2.imwrite("total.bmp", image_v)
+    # 讀取圖檔
+    filename = 'total.bmp'
+    image = cv2.imread(filename)
+    # 轉為灰階
+    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+
+    # 模糊化圖片
+    blurred = cv2.GaussianBlur(gray, (11, 11), 0)
+    #cv2.imshow('Blur',blurred)
+    #cv2.waitKey(0)
+
+    # Canny 尋找邊緣
+    edged = cv2.Canny(blurred, 30, 150)
+    #cv2.imshow('Edged',edged)
+    #cv2.waitKey(0)
+
+    # findContour 確定輪廓
+    _,contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
+    img2 = image.copy()
+    cv2.drawContours(img2, contours, -1, (0, 255, 0), 2)
+    #cv2.imshow('contour',img2)
+    #cv2.imwrite('check.bmp',img2)
+    #cv2.waitKey(0)
+
+    cnt = contours[0]
+
+    #contourArea 計算面積
+    area = cv2.contourArea(cnt)  #計算面積
+    perimeter = cv2.arcLength(cnt,True)    #計算周長
+
+    if area < 19119:
+        print('破損豆噴調',area)
+    else:
+        pass
+    print('面積:',area, '周長:',perimeter)  #印出結果
+    '''
+    p = 0
+    d = 0
+    tt = np.array(gray[0])
+    print(tt)
+    count = len(tt)
+    print(len(tt))
+    
+    for i in tt[:]:
+        if i >= 150:
+            p = p + 1
+        elif i <= 130:
+            d = d + 1
+    print('要得豆子像素:',p)
+    print('壞的豆子像素:',d)
+
+    coffeebean = (d / count)*100
+    print('壞豆佔全部百分比:',coffeebean)
+    if coffeebean > 60:
+        record = record + 1
+        print('紀錄次數:', record)
+    else:
+        pass
+    if record == 50:
+        j = 1
+        # record=0
+        print('噴嘴啟動')
+        # break
+        # continue
+'''
+makepic()
+detectionpic()
+#end = datetime.datetime.now()
+#print("執行時間:", end - start)
+# 結束測量
+end = time.process_time()
+# 輸出結果
+print("執行時間:%f 秒" % (end - start))
+
+
+
+