|
@@ -1,38 +1,29 @@
|
|
import cv2
|
|
import cv2
|
|
import numpy as np
|
|
import numpy as np
|
|
|
|
|
|
-cv2.namedWindow('Image2', cv2.WINDOW_NORMAL)
|
|
|
|
|
|
+cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
|
|
|
|
|
|
-lower = 100
|
|
|
|
-upper = 200
|
|
|
|
|
|
+def chose(contour):
|
|
|
|
+ sm = cv2.arcLength(contour, True)
|
|
|
|
+ approx = cv2.approxPolyDP(contour, sm*0.025, True)
|
|
|
|
+ if len(approx) == 8: return 'circle'
|
|
|
|
|
|
-def lower_update(value):
|
|
|
|
- global lower
|
|
|
|
- lower = value
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def upper_update(value):
|
|
|
|
- global upper
|
|
|
|
- upper = value
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-cv2.createTrackbar("Lower", "Image2", lower, 255, lower_update)
|
|
|
|
-cv2.createTrackbar("Upper", "Image2", upper, 255, upper_update)
|
|
|
|
|
|
|
|
def recognize(image):
|
|
def recognize(image):
|
|
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
|
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
|
|
|
+ binary = np.zeros(hsv.shape[:-1])
|
|
|
|
|
|
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
- gray = cv2.GaussianBlur(gray, (7,7), 0)
|
|
|
|
- mask = cv2.Canny(gray, 55, 0)
|
|
|
|
- cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
-
|
|
|
|
- cv2.drawContours(hsv, cnts, -1, (0, 255, 0), 2)
|
|
|
|
|
|
+ binary[np.any(hsv > 190, axis=2)] = 1
|
|
|
|
|
|
- cv2.imshow('Image2', hsv)
|
|
|
|
|
|
+ cv2.imshow('Image', binary)
|
|
|
|
|
|
while cv2.waitKey(1) != ord('q'): continue
|
|
while cv2.waitKey(1) != ord('q'): continue
|
|
|
|
|
|
|
|
+ labels, labeledScreen = cv2.connectedComponents(binary)
|
|
|
|
+ cnts, hierrache = cv2.findContours(labels, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
+ for label in labels:
|
|
|
|
+ print(chose(label))
|
|
|
|
+
|
|
# image = image[t_min_loc1[1] + 20:t_min_loc2[1], t_min_loc1[0] + 20:t_min_loc2[0]]
|
|
# image = image[t_min_loc1[1] + 20:t_min_loc2[1], t_min_loc1[0] + 20:t_min_loc2[0]]
|
|
|
|
|
|
# hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
|
# hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
|
@@ -53,5 +44,5 @@ def recognize(image):
|
|
# return polys, (t_min_loc2[0] + 20, t_min_loc2[1] + 20)
|
|
# return polys, (t_min_loc2[0] + 20, t_min_loc2[1] + 20)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
- image = cv2.imread('falling_ball/image_board.png')
|
|
|
|
|
|
+ image = cv2.imread('falling_ball/image.png')
|
|
print(recognize(image))
|
|
print(recognize(image))
|