完善后的color_detect()函数:

# detect cube color
def color_detect(self, img):
    # set the arrangement of color'HSV
    x = y = 0
    for mycolor, item in self.HSV.items():
        colorLower = np.array(item[0])
        colorUpper = 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, colorLower, colorUpper)
        # a etching operation on a picture to remove edge roughness
        erosion = cv2.erode(mask, np.ones((1, 1), np.uint8), iterations=2)
        # the image for expansion operation, its role is to deepen the color depth in the picture
        dilation = cv2.dilate(erosion, np.ones(
            (1, 1), np.uint8), iterations=2)
        # adds pixels to the image
        target = cv2.bitwise_and(img, img, mask=dilation)
        # the filtered image is transformed into a binary image and placed in binary
        ret, binary = cv2.threshold(dilation, 127, 255, cv2.THRESH_BINARY)
        # get the contour coordinates of the image, where contours is the coordinate value, here only the contour is detected
        contours, hierarchy = cv2.findContours(
            dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        if len(contours) > 0:
            # do something about misidentification
            boxes = [
                box
                for box in [cv2.boundingRect(c) for c in contours]
                if 110 < min(box[2], box[3]) and  max(box[2], box[3]) < 170
            ]
            if boxes:
                for box in boxes:
                    x, y, w, h = box
                # find the largest object that fits the requirements
                c = max(contours, key=cv2.contourArea)
                # get the lower left and upper right points of the positioning object
                x, y, w, h = cv2.boundingRect(c)

                # locate the target by drawing rectangle
                # cv2.rectangle(img, (x, y), (x+w, y+h), (153, 153, 0), 2)
                # cv2.rectangle(img, (x, y), (x+130, y+130), (255, 0, 255), 2)

                # calculate the rectangle center
                x, y = (x*2+w)/2, (y*2+h)/2
                # calculate the real coordinates of mycobot relative to the target
                if mycolor == "yellow":
                    self.color = 1
                elif mycolor == "red":
                    self.color = 0
                elif mycolor == "green":
                    self.color = 2
                elif mycolor == "blue":
                    self.color = 3
                elif mycolor == "purple":
                    self.color = 4

    if abs(x) + abs(y) > 0:
        return x, y
    else:
        return None

主要修改的地方是在HSV字典中添加了蓝色、绿色和紫色的范围,并在检测到相应颜色时将self.color赋值为相应数字。同时,也在boxes筛选时增加了限制条件,以避免误识别

完善color_detect现在只能识别黄色和红色能够实现识别蓝色绿色紫色class Detect_markerobject def __init__self superDetect_marker self__init__ selfmc = MyCobotusb_dev baudrate selfmcpower_on selfyolo

原文地址: https://www.cveoy.top/t/topic/egT6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录