Bladeren bron

threshholding

jezvgg 11 maanden geleden
bovenliggende
commit
ea1ba373b2
1 gewijzigde bestanden met toevoegingen van 48 en 13 verwijderingen
  1. 48 13
      falling_ball/cv.py

+ 48 - 13
falling_ball/cv.py

@@ -1,15 +1,33 @@
 import cv2
 import numpy as np
+import matplotlib.pyplot as plt
 
 cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
 
 def chose(contour):
     sm = cv2.arcLength(contour, True)
     approx = cv2.approxPolyDP(contour, sm*0.025, True)
-    print(approx)
     if len(approx) == 7 or len(approx) == 8: return 'circle'
 
 
+lower = 100
+upper = 200
+
+
+def lower_update(value):
+    global lower
+    lower = value
+
+
+def upper_update(value):
+    global upper
+    upper = value
+
+
+cv2.createTrackbar("Lower", "Image", lower, 255, lower_update)
+cv2.createTrackbar("Upper", "Image", upper, 255, upper_update)
+
+
 def recognize(image):
     hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
     binary = np.zeros(hsv.shape[:-1])
@@ -17,21 +35,33 @@ def recognize(image):
     binary[np.any(hsv > 190, axis=2)] = 1
 
     contours = cv2.findContours(binary.astype('uint8'), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
+    corner_left_up, corner_right_down = None, None
     for i,contour in enumerate(contours):
-        # print(contour)
-        # print(chose(contour))
         if chose(contour) == 'circle':
-            cv2.drawContours(image, contours, i, (0, 255, 0), 10)
+            x = contour[:, 0, 0]
+            y = contour[:, 0, 1]
+            x_max, x_min, y_max, y_min = x.max(), x.min(), y.max(), y.min()
+            if not corner_left_up or corner_left_up[0] < x_min or corner_left_up[1] < y_min:
+                corner_left_up = (x_max, y_max)
+            elif not corner_right_down or corner_right_down[0] < x_min or corner_right_down[1] < y_min:
+                corner_right_down = (x_min, y_min)
 
-    cv2.imshow('Image', image)
-    while cv2.waitKey(1) != ord('q'): continue
+    shape = (corner_left_up[0]-corner_right_down[0], corner_left_up[1]-corner_right_down[1])
 
-    # image = image[t_min_loc1[1] + 20:t_min_loc2[1], t_min_loc1[0] + 20:t_min_loc2[0]]
+    image = image[corner_right_down[1]:corner_left_up[1],corner_right_down[0]:corner_left_up[0]]
 
-    # hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
-    # hsv = cv2.GaussianBlur(hsv, (9, 9), 0)
-    # _, thresh = cv2.threshold(hsv[:, :, 1], 27, 255, cv2.THRESH_BINARY)
+    # hsv = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2HSV)
+    # hsv = cv2.GaussianBlur(hsv, (1, 1), 0)
+    # _, thresh = cv2.threshold(hsv[:, :, 1], lower, upper, cv2.THRESH_BINARY)
     # cnts, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
+    # cv2.drawContours(image, cnts, -1, (0, 255, 0), 2)
+
+    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+
+    thresh = cv2.threshold(image, lower, upper, cv2.THRESH_BINARY)[0]
+    # thresh = cv2.adaptiveThreshold(image,upper,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,3,2)
+    binary = np.zeros(image.shape)
+    binary[image < thresh] = 1
 
     # polys = []
     # for cont in cnts:
@@ -43,8 +73,13 @@ def recognize(image):
     #     approx = approx[:, 0, :].tolist()
     #     polys.append(approx)
 
-    # return polys, (t_min_loc2[0] + 20, t_min_loc2[1] + 20)
+    cv2.imshow('Image', binary)
+    
+    cv2.waitKey(1)
+
+    # return polys, shape
 
 if  __name__ == "__main__":
-    image = cv2.imread('falling_ball/image.png')
-    print(recognize(image))
+    while True:
+        image = cv2.imread('falling_ball/image.png')
+        print(recognize(image))