Пример #1
0
    def get_bot_front():
        max_area = 0
        min_area = 10000

        colorName = "front"
        upperColor = Color(colorName, 1)
        lowerColor = Color(colorName, 0)
        list_of_points = []
        Frame.capture_frame()
        resized = Frame.cut
        ratio = 1

        hsv = cv2.cvtColor(resized, cv2.COLOR_BGR2HSV)

        #t = cv2.getTrackbarPos('t','img')

        mask = cv2.inRange(hsv, lowerColor.get_array(), upperColor.get_array())

        res = cv2.bitwise_and(resized, resized, mask=mask)
        gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)

        T = mahotas.thresholding.otsu(blurred)
        b = blurred.copy()
        b[b > T] = 255
        b[b < 255] = 0

        cnts = cv2.findContours(b, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]

        area = 0
        cX = 0
        cY = 0
        for c in cnts:
            M = cv2.moments(c)
            if M["m00"] > 0:
                cX = int((M["m10"] / M["m00"] + 1e-7) * ratio)
                cY = int((M["m01"] / M["m00"] + 1e-7) * ratio)
                #shape = sd.detect(c)
                c = c.astype("float")
                c *= ratio
                c = c.astype("int")
                area = cv2.contourArea(c)
                #rad = getRadFromArea(area)
                if area > 300:
                    #cv2.drawContours(resized, [c], -1, (255,0,0), 2)
                    list_of_points.append((cX, cY))

        if len(list_of_points) > 0:
            bot_front = list_of_points[0]
            return bot_front
        else:
            return Bot.get_bot_front()
Пример #2
0
    def get_bot_back():
        max_area = 0
        min_area = 10000

        colour = "lgreen"
        f = open(colour + ".txt", "r")
        data = f.read().split(',')
        H = int(data[0])
        S = int(data[1])
        V = int(data[2])
        h = int(data[3])
        s = int(data[4])
        v = int(data[5])
        f.close()

        higher = np.array([H, S, V])
        lower = np.array([h, s, v])

        list_of_points = []
        Frame.capture_frame()
        resized = Frame.cut
        ratio = 1

        hsv = cv2.cvtColor(resized, cv2.COLOR_BGR2HSV)

        #t = cv2.getTrackbarPos('t','img')

        mask = cv2.inRange(hsv, lower, higher)

        res = cv2.bitwise_and(resized, resized, mask=mask)
        gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        T = mahotas.thresholding.otsu(blurred)
        b = blurred.copy()
        b[b > T] = 255
        b[b < 255] = 0

        cnts = cv2.findContours(b, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]

        area = 0
        cX = 0
        cY = 0
        for c in cnts:
            M = cv2.moments(c)
            if M["m00"] > 0:
                cX = int((M["m10"] / M["m00"] + 1e-7) * ratio)
                cY = int((M["m01"] / M["m00"] + 1e-7) * ratio)
                #shape = sd.detect(c)
                c = c.astype("float")
                c *= ratio
                c = c.astype("int")
                area = cv2.contourArea(c)
                #rad = getRadFromArea(area)
                if area > 300:
                    #cv2.drawContours(resized, [c], -1, (255,0,0), 2)
                    list_of_points.append((cX, cY))
                    if area > max_area:
                        max_area = area
                    elif area < min_area:
                        if __name__ == '__main__':
                            min_area = area

            k = cv2.waitKey(5) & 0xFF
            if k == 27:
                break
        if len(list_of_points) > 0:
            bot_back = list_of_points[0]
            return bot_back
        else:
            return Bot.get_bot_back()