From a60a94aea05f92dad9ee042a2d49c77543e0e666 Mon Sep 17 00:00:00 2001 From: wangWking <842749351@qq.com> Date: Thu, 17 Nov 2022 14:01:46 +0800 Subject: [PATCH] fix color --- .../scripts/advance_detect_obj_color.py | 12 +-- .../scripts/advance_detect_obj_color.py | 9 +- .../scripts/advance_detect_obj_color.py | 92 +++---------------- 3 files changed, 25 insertions(+), 88 deletions(-) diff --git a/mycobot_ai/ai_mecharm_270/scripts/advance_detect_obj_color.py b/mycobot_ai/ai_mecharm_270/scripts/advance_detect_obj_color.py index ebd2336..ba75b97 100644 --- a/mycobot_ai/ai_mecharm_270/scripts/advance_detect_obj_color.py +++ b/mycobot_ai/ai_mecharm_270/scripts/advance_detect_obj_color.py @@ -77,7 +77,7 @@ class Object_detect(Movement): self.HSV = { "yellow": [np.array([11, 115, 70]), np.array([40, 255, 245])], "red": [np.array([0, 43, 46]), np.array([8, 255, 255])], - "green": [np.array([35, 43, 46]), np.array([77, 255, 255])], + "green": [np.array([35, 43, 35]), np.array([90, 255, 255])], "blue": [np.array([100, 43, 46]), np.array([124, 255, 255])], "cyan": [np.array([78, 43, 46]), np.array([99, 255, 255])], } @@ -321,15 +321,15 @@ class Object_detect(Movement): def color_detect(self, img): # set the arrangement of color'HSV x = y = 0 + gs_img = cv2.GaussianBlur(img, (3, 3), 0) # 高斯模糊 + # transfrom the img to model of gray + hsv = cv2.cvtColor(gs_img, cv2.COLOR_BGR2HSV) + for mycolor, item in self.HSV.items(): # print("mycolor:",mycolor) redLower = np.array(item[0]) redUpper = np.array(item[1]) - # transfrom the img to model of gray - hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) - # print("hsv",hsv) - # wipe off all color expect color in range mask = cv2.inRange(hsv, item[0], item[1]) @@ -376,7 +376,7 @@ class Object_detect(Movement): self.color = 0 elif mycolor == "green": self.color = 1 - elif mycolor == "cyan": + elif mycolor == "cyan" or mycolor == "blue": self.color = 2 else: self.color = 3 diff --git a/mycobot_ai/ai_mycobot_280/scripts/advance_detect_obj_color.py b/mycobot_ai/ai_mycobot_280/scripts/advance_detect_obj_color.py index 0809e89..da893b8 100644 --- a/mycobot_ai/ai_mycobot_280/scripts/advance_detect_obj_color.py +++ b/mycobot_ai/ai_mycobot_280/scripts/advance_detect_obj_color.py @@ -316,11 +316,13 @@ class Object_detect(Movement): def color_detect(self, img): # set the arrangement of color'HSV x = y = 0 + gs_img = cv2.GaussianBlur(img, (3, 3), 0) # 高斯模糊 + # transfrom the img to model of gray + hsv = cv2.cvtColor(gs_img, cv2.COLOR_BGR2HSV) + for mycolor, item in self.HSV.items(): redLower = np.array(item[0]) redUpper = np.array(item[1]) - # transfrom the img to model of gray - hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # wipe off all color expect color in range mask = cv2.inRange(hsv, item[0], item[1]) # a etching operation on a picture to remove edge roughness @@ -365,7 +367,7 @@ class Object_detect(Movement): elif mycolor == "green": self.color = 1 - elif mycolor == "cyan": + elif mycolor == "cyan" or mycolor == "blue": self.color = 2 else: @@ -400,7 +402,6 @@ if __name__ == "__main__": _, frame = cap.read() # deal img frame = detect.transform_frame(frame) - if _init_ > 0: _init_ -= 1 continue diff --git a/mycobot_ai/ai_mypalletizer_260/scripts/advance_detect_obj_color.py b/mycobot_ai/ai_mypalletizer_260/scripts/advance_detect_obj_color.py index 6d3e902..4f79979 100644 --- a/mycobot_ai/ai_mypalletizer_260/scripts/advance_detect_obj_color.py +++ b/mycobot_ai/ai_mypalletizer_260/scripts/advance_detect_obj_color.py @@ -73,33 +73,15 @@ class Object_detect(Movement): # set cache of real coord self.cache_x = self.cache_y = 0 + # set color HSV self.HSV = { "yellow": [np.array([11, 115, 70]), np.array([40, 255, 245])], "red": [np.array([0, 43, 46]), np.array([8, 255, 255])], - "green": [np.array([70, 100, 100]), np.array([90, 255, 255])], # [77, 255, 255] + "green": [np.array([35, 43, 35]), np.array([90, 255, 255])], # [77, 255, 255] "blue": [np.array([100, 43, 46]), np.array([124, 255, 255])], "cyan": [np.array([78, 43, 46]), np.array([99, 255, 255])], # np.array([78, 43, 46]), np.array([99, 255, 255]) } - - # set color HSV - # self.HSV = { - # # "yellow": [np.array([11, 115, 70]), np.array([40, 255, 245])], - # "yellow": [np.array([22, 93, 70]), np.array([45, 255, 245])], - # "red": [np.array([0, 43, 46]), np.array([8, 255, 255])], - # # "green": [np.array([35, 43, 46]), np.array([77, 255, 255])], - # "green": [np.array([35, 55, 30]), np.array([88, 255, 255])], - # "blue": [np.array([100, 43, 46]), np.array([124, 255, 255])], - # # "blue": [np.array([-10, 100, 100]), np.array([10, 255, 255])], - # "cyan": [np.array([78, 43, 46]), np.array([99, 255, 255])], - # } - # self.HSV = { - # # "yellow": [np.array([11, 85, 70]), np.array([59, 255, 245])], - # "yellow": [np.array([22, 93, 0]), np.array([45, 255, 245])], - # "red": [np.array([0, 43, 46]), np.array([8, 255, 255])], - # "green": [np.array([35, 43, 35]), np.array([90, 255, 255])], - # # "blue": [np.array([100, 43, 46]), np.array([124, 255, 255])], - # "cyan": [np.array([78, 43, 46]), np.array([99, 255, 255])], - # } + # use to calculate coord between cube and mypal260 self.sum_x1 = self.sum_x2 = self.sum_y2 = self.sum_y1 = 0 # The coordinates of the grab center point relative to the mypal260 @@ -328,14 +310,14 @@ class Object_detect(Movement): def color_detect(self, img): # set the arrangement of color'HSV x = y = 0 + gs_img = cv2.GaussianBlur(img, (3, 3), 0) # 高斯模糊 + # transfrom the img to model of gray + hsv = cv2.cvtColor(gs_img, cv2.COLOR_BGR2HSV) + for mycolor, item in self.HSV.items(): # print("mycolor:",mycolor) redLower = np.array(item[0]) redUpper = np.array(item[1]) - - # transfrom the img to model of gray - hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) - # print("hsv",hsv) # wipe off all color expect color in range mask = cv2.inRange(hsv, item[0], item[1]) @@ -380,64 +362,18 @@ class Object_detect(Movement): # calculate the real coordinates of mypal260 relative to the target if mycolor == "red": self.color = 0 - break - elif mycolor == "blue": - self.color = 2 - break + elif mycolor == "green": self.color = 1 - break - elif mycolor == "cyan": + + elif mycolor == "cyan" or mycolor == "blue": self.color = 2 - break - - elif mycolor == "yellow": - self.color = 3 - break - - # elif mycolor == "green": - # self.color = 1 - # if mycolor == "red": - # self.color = 0 - # break - # elif mycolor == "green": - # self.color = 1 - # print('green') - # break - # elif mycolor == "cyan": - # self.color = 2 - # break - # elif mycolor == "blue": - # self.color = 2 - # break - # elif mycolor == "yellow": - # self.color = 3 - # break - # if mycolor == "yellow": - - # self.color = 3 - # print(mycolor) - # break - - # elif mycolor == "red": - # self.color = 0 - # print(mycolor) - # break - - # elif mycolor == "cyan" or mycolor == "blue": - # self.color = 2 - # print(mycolor) - # break - - # # elif mycolor == "blue": - # # self.color =2 - # # break - # else: - # self.color = 1 - # print(mycolor) - # # break + else: + self.color = 3 + + if abs(x) + abs(y) > 0: return x, y else: