示例#1
0
def find_pipe(binary):
    _, contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL,
                                     cv.CHAIN_APPROX_SIMPLE)
    number_of_object = 1
    percent_pipe = 0.8
    result = []
    pipe = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)

    for cnt in contours:
        (x, y), (w, h), angle = rect = cv.minAreaRect(cnt)
        w, h = max(w, h), min(w, h)
        if not is_verticle_pipe(cnt, percent_pipe, rect):
            continue

        box = cv.boxPoints(rect)
        box = np.int64(box)
        if DEBUG['not-only-res']:
            cv.drawContours(pipe, [box], 0, (0, 255, 255), 2)
        result.append([int(x), int(y), int(h), int(w), angle])

    if DEBUG['not-only-res']:
        lib.publish_result(pipe, 'bgr', PUBLIC_TOPIC + 'mask/pipe')
    result = sorted(result, key=itemgetter(3), reverse=True)

    if len(result) <= number_of_object:
        return result, len(result)
    return result[:1], 1
示例#2
0
def get_mat():
    mat = get_mask("green", blur=5)
    kernel = lib.get_kernel(101, 101)
    mat = cv.erode(mat.copy(), kernel)
    mat = cv.dilate(mat.copy(), kernel)
    lib.publish_result(mat, 'gray', public_topic + 'mask/mat/unprocessed')
    contours = cv.findContours(mat, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)[1]

    hmm = sorted([cnt for cnt in contours if cv.contourArea(cnt) > 500],
                 key=cv.contourArea,
                 reverse=True)
    print(len(hmm))
    if len(hmm) == 0:
        return np.array([])
    # max_cnt = max(hmm, key=cv.contourArea)
    rect = cv.minAreaRect(hmm[0])
    box = cv.boxPoints(rect)
    box = np.int64(box)
    himg, wimg = mat.shape[:2]
    print(rect)
    mat_mask = np.zeros((himg, wimg), np.uint8)
    temp = np.zeros((himg, wimg), np.uint8)
    cv.drawContours(mat_mask, [box], 0, (255), -1)
    cv.drawContours(temp, hmm, -1, (255), -1)
    lib.publish_result(temp, 'gray', public_topic + 'temp')
    return mat_mask
示例#3
0
def temp_function(cnt):
    himg, wimg = image.display.shape[:2]
    mat_mask = np.zeros((himg, wimg), np.uint8)
    (x, y), radius = cv.minEnclosingCircle(cnt)
    center = (int(x), int(y))
    radius = int(radius)
    cv.circle(mat_mask, center, radius, (255), -1)
    lib.publish_result(mat_mask, 'gray', public_topic + 'mask/enclose')
示例#4
0
def rm_bg(mask):
    image.get_hsv()
    upper = np.array([179, 225, 185], dtype=np.uint8)
    lower = np.array([50, 54, 48], dtype=np.uint8)
    bg = cv.inRange(image.hsv, lower, upper)
    bg = cv.bitwise_not(bg)
    obj = cv.bitwise_and(mask, bg)
    lib.publish_result(obj, 'gray', PUBLIC_TOPIC + '/rm_bg')
    return obj
def get_mat():
    mat = get_mask("green")
    lib.publish_result(mat, 'gray', public_topic + 'mask/mat/unprocessed')
    contours = cv.findContours(mat, cv.RETR_EXTERNAL,
                               cv.CHAIN_APPROX_SIMPLE)[1]
    max_cnt = max(contours, key=cv.contourArea)
    rect = cv.minAreaRect(max_cnt)
    box = cv.boxPoints(rect)
    box = np.int64(box)
    himg, wimg = mat.shape[:2]
    mat_mask = np.zeros((himg, wimg), np.uint8)
    cv.drawContours(mat_mask, [box], 0, (255), -1)
    return mat_mask
def find_drum(color, return_option):
    global mask_gripper
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)
    himg, wimg = image.display.shape[:2]
    drum_mask = get_mask(color)
    obj = get_obj(drum_mask, color)
    state = obj != []
    if state == 0:
        lib.print_result("CANNOT FOUND DRUM", ct.RED)
        lib.publish_result(drum_mask, 'gray', public_topic + 'mask/drum')
        lib.publish_result(image.display, 'bgr', public_topic + 'image_result')
        return message()
    lib.print_result("FOUND DRUM", ct.GREEN)
    cv.circle(image.display, lib.most_point(obj, 'right'), 5, (255, 255, 0),
              -1)
    cv.circle(image.display, lib.most_point(obj, 'left'), 5, (0, 255, 255), -1)
    cx1, cy1, cx2, cy2, area = get_cx(obj, return_option=return_option)
    forward, backward, left, right = get_excess(obj)
    lib.publish_result(drum_mask, 'gray', public_topic + 'mask/drum')
    lib.publish_result(image.display, 'bgr', public_topic + 'display')
    return message(state=state,
                   cx1=cx1,
                   cy1=cy1,
                   cx2=cx2,
                   cy2=cy2,
                   forward=forward,
                   backward=backward,
                   left=left,
                   right=right,
                   area=area)
示例#7
0
def find_near_flare():
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)

    image.renew_display()
    pre_process = lib.pre_process(image.bgr, 'flare')
    mask = get_mask()

    kernel_box = lib.get_kernel(ksize=(7, 7))
    kernel_vertical = lib.get_kernel(ksize=(1, 25))
    vertical = cv.erode(mask.copy(), kernel_vertical)
    vertical = cv.dilate(vertical.copy(), kernel_box)
    kernel_erode = lib.get_kernel(ksize=(3, 13))
    vertical = cv.erode(vertical.copy(), kernel_erode)

    mask = vertical

    cx, cy, area, obj, box = get_obj(mask)

    mode = area != 0
    color = ct.PURPLE
    if mode == 0:
        lib.print_result("NOT FOUND " + color + "(NEAR)", ct.RED)
        lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'near/display')
        lib.publish_result(mask, 'gray', PUBLIC_TOPIC + 'near/mask')
        return message()
    if mode == 1:
        cv.circle(image.display, (int(cx), int(cy)), 3, (0, 255, 255), -1)
        cv.drawContours(image.display, [box], 0, (0, 255, 0), 2)
        lib.print_result("FOUND " + color + "(NEAR)", ct.GREEN)
        lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'near/display')
        lib.publish_result(mask, 'gray', PUBLIC_TOPIC + 'near/mask')
        return message(cx=cx, cy=cy, area=area, state=mode)
    return message(state=0)
示例#8
0
def get_mask(color, shade=None, blur=5):
    image.get_hsv()
    blur = cv.medianBlur(image.hsv, blur)
    _, s, _ = cv.split(blur)

    foregroud_mask = s

    # foregroud_mask = cv.threshold(cv.line(img,(0,0),(wimg,0),(0),5)
    # s, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1]

    lib.publish_result(foregroud_mask, 'gray', public_topic + 'fg_mask')

    if color == "blue":
        # upper = np.array([161, 197, 195], dtype=np.uint8)
        # lower = np.array([49, 9, 63], dtype=np.uint8)
        # upper = np.array([120, 255, 255], dtype=np.uint8)
        # lower = np.array([90, 160, 0], dtype=np.uint8)
        upper = np.array([123, 222, 255], dtype=np.uint8)
        lower = np.array([61, 120, 144], dtype=np.uint8)
    if color == "yellow":
        if shade == "dark":
            # upper = np.array([66, 255, 255], dtype=np.uint8)
            # lower = np.array([37, 98, 0], dtype=np.uint8)
            upper = np.array([47, 255, 255], dtype=np.uint8)
            lower = np.array([20, 17, 228], dtype=np.uint8)
        elif shade == "light":
            upper = np.array([60, 255, 255], dtype=np.uint8)
            lower = np.array([27, 160, 97], dtype=np.uint8)
    if color == "green":
        # upper = np.array([90, 255, 255], dtype=np.uint8)
        # lower = np.array([60, 160, 0], dtype=np.uint8)
        upper = np.array([72, 255, 255], dtype=np.uint8)
        lower = np.array([28, 0, 0], dtype=np.uint8)

    if color != 'red':
        himg, wimg = image.bgr.shape[:2]
        foregroud = cv.bitwise_and(image.hsv, image.hsv, mask=foregroud_mask)
        mask = cv.inRange(foregroud, lower, upper)
    if shade is not ['golf']:
        cv.rectangle(mask, (0, 0), (380, 360), (0), -1)
        cv.rectangle(mask, (0, 0), (wimg, himg - 10), (0), 10)

    if color == "red":
        # upper = np.array([161, 197, 195], dtype=np.uint8)
        # lower = np.array([49, 9, 63], dtype=np.uint8)
        upper = np.array([179, 255, 255], dtype=np.uint8)
        lower = np.array([120, 0, 0], dtype=np.uint8)
        mask = cv.inRange(image.hsv, lower, upper)
    return mask
示例#9
0
def get_contour(mask, request):
    if request in ['red', 'blue'] and not DEBUG['by-pass-mat']:
        mat_mask = get_mat()
        if mat_mask.shape != mask.shape:
            return cv.findContours(mask, cv.RETR_EXTERNAL,
                                   cv.CHAIN_APPROX_SIMPLE)[1]

        lib.publish_result(mat_mask, 'gray',
                           public_topic + 'mask/mat/processed')
        intersect = cv.bitwise_and(mat_mask, mask)
        return cv.findContours(intersect, cv.RETR_EXTERNAL,
                               cv.CHAIN_APPROX_SIMPLE)[1]

    elif request == 'golf' or DEBUG['by-pass-mat']:
        return cv.findContours(mask, cv.RETR_EXTERNAL,
                               cv.CHAIN_APPROX_SIMPLE)[1]
示例#10
0
def find_pipe(binary):
    _, contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL,
                                     cv.CHAIN_APPROX_SIMPLE)
    number_of_object = 2
    percent_pipe = 0.6
    result = []
    pipe = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)

    for cnt in contours:
        (x, y), (w, h), angle = rect = cv.minAreaRect(cnt)
        w, h = max(w, h), min(w, h)
        if not is_verticle_pipe(cnt, percent_pipe, rect):
            continue

        if DEBUG['print']:
            print('c1')

        box = cv.boxPoints(rect)
        box = np.int64(box)
        if DEBUG['not-only-res']:
            cv.drawContours(pipe, [box], 0, (0, 255, 255), 2)
        result.append([int(x), int(y), int(h), int(w), angle])

    if DEBUG['not-only-res']:
        lib.publish_result(pipe, 'bgr', PUBLIC_TOPIC + 'mask/pipe')
    result = sorted(result, key=itemgetter(3), reverse=True)

    if len(result) < number_of_object:
        return result, len(result)
    else:
        closest_pair = []
        min_dist = 2000
        for i in range(len(result)):
            for j in range(i + 1, len(result)):
                dist_y = abs(result[j][1] - result[i][1])
                dist_x = abs(result[j][0] - result[i][0])
                if dist_x >= 50 and dist_y < min_dist:
                    min_dist = dist_y
                    closest_pair = [result[i], result[j]]
        if closest_pair == []:
            return result[:1], 1
        else:
            return closest_pair, 2
示例#11
0
def find_pipe(binary, align):
    _, contours, _ = cv.findContours(
        binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    number_of_object = 2 if align == 'v' else 1
    percent_pipe = 0.4 if align == 'v' else 0.2
    topic = 'mask/pipe/vertical' if align == 'v' else 'mask/pipe/horizontal'
    color = (0, 255, 255) if align == 'v' else (255, 0, 255)
    pipe = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)
    result = []

    for cnt in contours:
        (x, y), (w, h), angle = rect = cv.minAreaRect(cnt)
        this_align = what_align(cnt)
        if not is_pipe(cnt, percent_pipe, rect) or not this_align == align:
            continue
        (w, h) = (min(w, h), max(w, h)) if align == 'v' else (max(w, h), min(w, h))
        box = cv.boxPoints(rect)
        box = np.int64(box)
        cv.drawContours(pipe, [box], 0, color, 2)
        result.append([int(x), int(y), int(w), int(h), angle])

    lib.publish_result(pipe, 'bgr', PUBLIC_TOPIC + topic)
    result = sorted(result, key=itemgetter(3) if align == 'v' else itemgetter(2), reverse=True)

    if len(result) <= number_of_object:
        return result, len(result)

    # find closet
    closest_pair = []
    min_dist = 2000
    for i in range(len(result)):
        for j in range(i+1, len(result)):
            dist_x = abs(result[j][0] - result[i][0])
            dist_y = abs(result[j][1] - result[i][1])
            if dist_x >= 50 and dist_y < min_dist:
                min_dist = dist_y
                closest_pair = [result[i], result[j]]

    if closest_pair == []:
        return result[:1], 1
    return closest_pair, 2
示例#12
0
def find_mat():
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)
    image.renew_display()
    himg, wimg = image.display.shape[:2]
    mat_mask = get_mask("green")
    contours = cv.findContours(mat_mask, cv.RETR_EXTERNAL,
                               cv.CHAIN_APPROX_SIMPLE)[1]
    processed_contours = []
    for cnt in contours:
        if cv.contourArea(cnt) < 1000:
            continue
        processed_contours.append(cnt)
    state = len(processed_contours)
    lib.publish_result(mat_mask, 'gray', public_topic + 'mask/mat')
    lib.publish_result(image.display, 'bgr', public_topic + 'display/mat')
    if state == 0:
        lib.print_result("CANNOT FOUND MAT", ct.RED)
        return message()
    lib.print_result("FOUND MAT", ct.GREEN)
    return message(state=1)
示例#13
0
def find_drum(color, return_option):
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)
    image.renew_display()
    himg, wimg = image.display.shape[:2]
    drum_mask = get_mask(color)
    obj = get_obj(drum_mask, color)
    state = obj != []
    if state == 0:
        lib.print_result("CANNOT FOUND DRUM", ct.RED)
        lib.publish_result(drum_mask, 'gray', public_topic + 'mask')
        lib.publish_result(image.display, 'bgr', public_topic + 'display')
        return message()
    temp_function(obj)
    lib.print_result("FOUND DRUM", ct.GREEN)
    cv.circle(image.display, lib.most_point(obj, 'right'), 10, (255, 255, 0),
              -1)
    cv.circle(image.display, lib.most_point(obj, 'left'), 10, (0, 255, 255),
              -1)
    cx1, cy1, cx2, cy2, area = get_cx(obj, return_option=return_option)
    t1 = lib.Aconvert(cx1, wimg)
    ty1 = -1.0 * lib.Aconvert(cy1, himg)
    t2 = lib.Aconvert(cx2, wimg)
    ty2 = -1.0 * lib.Aconvert(cy2, himg)
    forward, backward, left, right = get_excess(obj)
    cv.putText(image.display, "pt1 = ({:.2f},{:.2f})".format(t1, ty1),
               (0, himg - 100), cv.FONT_HERSHEY_SIMPLEX, 2, 255, 5)
    cv.putText(image.display, "pt2 = ({:.2f},{:.2f})".format(t2, ty2),
               (0, himg - 20), cv.FONT_HERSHEY_SIMPLEX, 2, 255, 5)
    lib.publish_result(drum_mask, 'gray', public_topic + 'mask')
    lib.publish_result(image.display, 'bgr', public_topic + 'display')
    return message(state=state,
                   cx1=cx1,
                   cy1=cy1,
                   cx2=cx2,
                   cy2=cy2,
                   forward=forward,
                   backward=backward,
                   left=left,
                   right=right,
                   area=area)
示例#14
0
def find_golf(objective):
    global last_time, his, start_shade, shade
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)
    gray = cv.cvtColor(image.bgr, cv.COLOR_BGR2GRAY)
    # r,c = gray.shape
    # mask_gripper = cv.resize(mask_gripper, (r,c))
    bg = cv.medianBlur(gray, 61)
    fg = cv.medianBlur(gray, 5)
    sub_sign = np.int16(fg) - np.int16(bg)
    sub_pos = np.clip(sub_sign.copy(), 0, sub_sign.copy().max())
    sub_neg = np.clip(sub_sign.copy(), sub_sign.copy().min(), 0)

    sub_pos = normalize(sub_pos)
    sub_neg = normalize(sub_neg)

    # cv.imshow('sub_pos',sub_pos)
    # cv.imshow('sub_neg',sub_neg)

    _, obj = cv.threshold(sub_pos, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
    print("obj", obj.shape)
    print("mask", mask_gripper.shape)
    obj = cv.bitwise_and(obj, obj, mask=mask_gripper)

    _, contours, _ = cv.findContours(obj.copy(), cv.RETR_EXTERNAL,
                                     cv.CHAIN_APPROX_NONE)
    display = cv.cvtColor(sub_neg.copy(), cv.COLOR_GRAY2BGR)
    r = 0
    circle = []
    for cnt in contours:
        area_cnt = cv.contourArea(cnt)
        (x, y), radius = cv.minEnclosingCircle(cnt)
        center = (int(x), int(y))
        radius = radius
        area_cir = math.pi * (radius**2)
        if area_cir <= 0 or area_cnt / area_cir < 0.8:
            continue
        cv.circle(display, center, int(radius), (255, 0, 0), -1)
        circle.append([x, y, radius])

    row, col = gray.shape
    if len(circle) > 0:
        circle = sorted(circle, key=itemgetter(2), reverse=True)
        circle = circle[0]
        x, y, radius = circle
        cv.circle(display, (int(x), int(y)), int(radius), (0, 0, 255), 2)
        diameter = 2. * radius
        pixel_per_cm = diameter / 4.3
        print("radius", radius)
        print("pixel_per_cm", pixel_per_cm)
        print("depth", depth)
        if True:
            x_distance_pixel = row / 2. - y
            y_distance_pixel = col / 2. - x
            print("row col", row, col)
            print("x y", x, y)
            print("x_distance_pixel y_distance_pixel:", x_distance_pixel,
                  y_distance_pixel)
            x_distance_cm = float(x_distance_pixel) / pixel_per_cm
            y_distance_cm = float(y_distance_pixel) / pixel_per_cm
            print("x_distance_cm:", x_distance_cm)
            print("y_distance_cm:", y_distance_cm)
            x_distance_meter = x_distance_cm / 100.
            y_distance_meter = y_distance_cm / 100.
            print("x_meter:", x_distance_meter)
            print("y_meter:", y_distance_meter)
            lib.publish_result(display, 'gray', public_topic + 'mask/golf')
            # lib.publish_result(image.bgr, 'bgr', public_topic+'image_result')
            return message(state=1,
                           cx1=y_distance_meter,
                           cy1=-x_distance_meter,
                           cx2=y_distance_meter,
                           cy2=-x_distance_meter,
                           forward=True,
                           backward=True,
                           left=True,
                           right=True,
                           area=0)
    lib.publish_result(display, 'gray', public_topic + 'mask/golf')

    return message(state=0,
                   cx1=y_distance_meter,
                   cy1=-x_distance_meter,
                   cx2=y_distance_meter,
                   cy2=-x_distance_meter,
                   forward=True,
                   backward=True,
                   left=True,
                   right=True,
                   area=0)
示例#15
0
def find_qualify_pole():
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)

    image.renew_display()
    log.update_time()
    image.get_gray()
    hsv = cv.cvtColor(image.bgr, cv.COLOR_BGR2HSV)
    hue, s, v = cv.split(hsv)
    # hue = cv.equalizeHist(hue)
    clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    cl1 = clahe.apply(hue)
    obj = lib.bg_subtraction(cl1, mode='neg', fg_blur_size=3)
    kernel_box = lib.get_kernel(ksize=(7, 7))

    kernel_vertical = lib.get_kernel(ksize=(1, 25))
    kernel_horizontal = lib.get_kernel(ksize=(25, 1))

    vertical = cv.erode(obj.copy(), kernel_vertical)
    vertical = cv.dilate(vertical.copy(), kernel_box)

    hori = cv.erode(obj.copy(), kernel_horizontal)
    hori = cv.dilate(hori.copy(), kernel_box)

    kernel_erode = lib.get_kernel(ksize=(3, 11))
    kernel_erode_hori = lib.get_kernel(ksize=(11, 3))
    vertical = cv.erode(vertical.copy(), kernel_erode)
    hori = cv.erode(hori.copy(), kernel_erode_hori)

    vertical_pipe, no_pipe_v = find_pipe(vertical)

    vertical_cx1 = []
    vertical_cx2 = []
    vertical_cy1 = []
    vertical_cy2 = []
    for res in vertical_pipe:
        x, y, w, h, angle = res
        cv.rectangle(image.display, (int(x - w / 2.), int(y - h / 2.)),
                     (int(x + w / 2.), int(y + h / 2.)), (108, 105, 255), 2)
        vertical_cx1.append((x - w / 2.))
        vertical_cx2.append((x + w / 2.))
        vertical_cy1.append((y - h / 2.))
        vertical_cy2.append((y + h / 2.))

    himg, wimg = obj.shape[:2]
    mode = no_pipe_v
    lib.print_result("NOT FOUND", ct.RED)
    lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'display')
    lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'mask/vertical')
    lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'mask')
    lib.publish_result(image.gray, 'gray', PUBLIC_TOPIC + 'gray')
    lib.publish_result(hue, 'gray', PUBLIC_TOPIC + 'hue')
    lib.publish_result(hori, 'gray', PUBLIC_TOPIC + 'hori')
    if mode == 0:

        return message()

    elif mode == 1:
        lib.print_result("FOUNG ONE POLE", ct.YELLOW)
    elif mode == 2:
        lib.print_result("FOUND", ct.GREEN)

    cx1 = min(vertical_cx2)
    cx2 = max(vertical_cx1)
    cy1 = max(vertical_cy1)
    cy2 = min(vertical_cy2)

    cx1, cx2 = min(cx1, cx2), max(cx1, cx2)
    cy1, cy2 = min(cy1, cy2), max(cy1, cy2)

    cx1, cx2 = max(cx1, 0), min(cx2, wimg)
    cy1, cy2 = max(cy1, 0), min(cy2, himg)

    cv.rectangle(image.display, (int(cx1), int(cy1)), (int(cx2), int(cy2)),
                 (0, 255, 0), 3)
    cv.circle(image.display, (int((cx1 + cx2) / 2), int((cy1 + cy2) / 2)), 3,
              (0, 255, 255), -1)

    area = 1.0 * abs(cx2 - cx1) * abs(cy2 - cy1) / (himg * wimg)
    lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'display')
    lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'mask/vertical')
    # lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'mask')
    lib.publish_result(image.gray, 'gray', PUBLIC_TOPIC + 'gray')
    lib.publish_result(hue, 'gray', PUBLIC_TOPIC + 'hue')
    # lib.publish_result(hori, 'gray', PUBLIC_TOPIC + 'hori')
    log.assume_pole(mode=mode, x=cx1, y=cy1)
    pos = log.assume_to_pos()
    log.save_data(state=mode, cx1=cx1, cx2=cx2, cy1=cy1, cy2=cy2)
    return message(state=mode,
                   cx1=cx1,
                   cy1=cy1,
                   cx2=cx2,
                   cy2=cy2,
                   pos=pos,
                   area=area)
示例#16
0
def find_pole():
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)

    image.renew_display()
    log.update_time()
    image.get_gray()
    obj = lib.bg_subtraction(image.gray, mode='neg', fg_blur_size=3)

    kernel_box = lib.get_kernel(ksize=(7, 7))

    kernel_vertical = lib.get_kernel(ksize=(1, 25))
    vertical = cv.erode(obj.copy(), kernel_vertical)
    vertical = cv.dilate(vertical.copy(), kernel_box)
    kernel_erode = lib.get_kernel(ksize=(3, 11))
    vertical = cv.erode(vertical.copy(), kernel_erode)

    vertical_pipe, no_pipe_v = find_pipe(vertical)

    vertical_cx1 = []
    vertical_cx2 = []
    vertical_cy1 = []
    vertical_cy2 = []
    for res in vertical_pipe:
        x, y, w, h, angle = res
        cv.rectangle(image.display, (int(x - w / 2.), int(y - h / 2.)),
                     (int(x + w / 2.), int(y + h / 2.)), (108, 105, 255), 2)
        vertical_cx1.append((x - w / 2.))
        vertical_cx2.append((x + w / 2.))
        vertical_cy1.append((y - h / 2.))
        vertical_cy2.append((y + h / 2.))

    himg, wimg = obj.shape[:2]
    mode = no_pipe_v
    if mode == 0:
        lib.print_result("NOT FOUND", ct.RED)
        lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'display')
        lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'mask/vertical')
        lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'mask')
        return message()
    elif mode == 1:
        lib.print_result("FOUNG ONE POLE", ct.YELLOW)
    elif mode == 2:
        lib.print_result("FOUND", ct.GREEN)

    cx1 = min(vertical_cx2)
    cx2 = max(vertical_cx1)
    cy1 = max(vertical_cy1)
    cy2 = min(vertical_cy2)

    cx1, cx2 = min(cx1, cx2), max(cx1, cx2)
    cy1, cy2 = min(cy1, cy2), max(cy1, cy2)

    cx1, cx2 = max(cx1, 0), min(cx2, wimg)
    cy1, cy2 = max(cy1, 0), min(cy2, himg)

    cv.rectangle(image.display, (int(cx1), int(cy1)), (int(cx2), int(cy2)),
                 (0, 255, 0), 3)
    cv.circle(image.display, (int((cx1 + cx2) / 2), int((cy1 + cy2) / 2)), 3,
              (0, 255, 255), -1)

    area = 1.0 * abs(cx2 - cx1) * abs(cy2 - cy1) / (himg * wimg)
    lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'display')
    lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'mask/vertical')
    lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'mask')
    log.assume_pole(mode=mode, x=cx1, y=cy1)
    pos = log.assume_to_pos()
    log.save_data(state=mode, cx1=cx1, cx2=cx2, cy1=cy1, cy2=cy2)
    return message(state=mode,
                   cx1=cx1,
                   cy1=cy1,
                   cx2=cx2,
                   cy2=cy2,
                   pos=pos,
                   area=area)
示例#17
0
def find_far_flare():
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)

    image.renew_display()
    pre_process = lib.pre_process(image.bgr, 'flare')
    gray = cv.cvtColor(pre_process.copy(), cv.COLOR_BGR2GRAY)
    hsv = cv.cvtColor(pre_process, cv.COLOR_BGR2HSV)
    h, s, v = cv.split(hsv)
    obj = lib.bg_subtraction(h, mode='neg')
    obj = rm_bg(obj)

    lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'far/temp')
    lib.publish_result(h, 'gray', PUBLIC_TOPIC + 'far/h_from_hsv')
    lib.publish_result(gray, 'gray', PUBLIC_TOPIC + 'far/gray')

    kernel_box = lib.get_kernel(ksize=(7, 7))
    kernel_vertical = lib.get_kernel(ksize=(1, 25))
    vertical = cv.erode(obj.copy(), kernel_vertical)
    vertical = cv.dilate(vertical.copy(), kernel_box)
    kernel_erode = lib.get_kernel(ksize=(3, 13))
    vertical = cv.erode(vertical.copy(), kernel_erode)

    # kernel = lib.get_kernel(ksize=(1,101))
    # vertical = cv.dilate(vertical, kernel)
    # vertical = cv.erode(vertical, kernel)

    vertical_pipe, no_v_pipe = find_pipe(vertical)
    mode = no_v_pipe
    color = ct.CYAN
    if mode == 0:
        lib.print_result("NOT FOUND (FAR)", ct.RED)
        lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'far/display')
        lib.publish_result(vertical, 'gray',
                           PUBLIC_TOPIC + 'far/mask/vertical')
        lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'far/mask')
        return message()
    x, y, w, h, angle = vertical_pipe[0]
    cv.rectangle(image.display, (int(x - w / 2.), int(y - h / 2.)),
                 (int(x + w / 2.), int(y + h / 2.)), (0, 255, 0), 2)
    vertical_x = [(x - w / 2.), (x + w / 2.)]
    vertical_y = [(y - h / 2.), (y + h / 2.)]

    himg, wimg = image.bgr.shape[:2]
    x1, x2 = max(min(vertical_x), 0), min(max(vertical_x), wimg)
    y1, y2 = max(min(vertical_y), 0), min(max(vertical_y), himg)
    area = (x2 - x1) * (y2 - y1)

    if area > 7000 or area < 700:
        lib.print_result("NOT FOUND " + color + "(FAR)", ct.RED)
        lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'far/display')
        lib.publish_result(vertical, 'gray',
                           PUBLIC_TOPIC + 'far/mask/vertical')
        lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'far/mask')
        return message()

    lib.print_result("FOUND " + color + "(FAR)", ct.GREEN)
    cv.rectangle(image.display, (int(x1), int(y1)), (int(x2), int(y2)),
                 (0, 255, 0), 3)
    cv.circle(image.display, (int((x1 + x2) / 2), int((y1 + y2) / 2)), 3,
              (0, 255, 255), -1)
    lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'far/display')
    lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'far/mask/vertical')
    lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'far/mask')
    return message(cx=(x1 + x2) / 2., cy=(y1 + y2) / 2., area=area, state=1)
示例#18
0
def find_golf(objective):
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)
    image.renew_display()
    image.get_gray()
    himg, wimg = image.bgr.shape[:2]
    obj = lib.bg_subtraction(image.gray, mode='pos')
    drum_mask = get_mask("blue", shade='golf')
    cnt = get_obj(drum_mask, "blue")
    temp = np.zeros((himg, wimg), np.uint8)
    if len(cnt) > 0:
        (x, y), radius = cv.minEnclosingCircle(cnt)
        center = (int(x), int(y))
        radius = int(radius)
        cv.circle(temp, center, radius, (255), -1)
        # cv.drawContours(temp, np.array(cnt), 0, (255), -1)
    obj = cv.bitwise_and(obj, temp)
    contours = cv.findContours(obj.copy(), cv.RETR_EXTERNAL,
                               cv.CHAIN_APPROX_NONE)[1]
    circle = []
    for cnt in contours:
        area_cnt = cv.contourArea(cnt)
        (x, y), radius = cv.minEnclosingCircle(cnt)
        center = (int(x), int(y))
        area_cir = math.pi * (radius**2)
        if area_cir <= 0 or area_cnt / area_cir < 0.8:
            continue
        cv.circle(image.display, center, int(radius), lib.get_color('yellow'),
                  3)
        circle.append([x, y, radius])

    state = len(circle)

    if state == 0:
        lib.print_result("CANNOT FOUND GOLF", ct.RED)
        lib.publish_result(obj, 'gray', public_topic + 'golf/mask')
        lib.publish_result(image.display, 'bgr', public_topic + 'golf/display')
        return message()
    himg, wimg = image.display.shape[:2]
    lib.print_result("FOUND GOLF", ct.GREEN)
    circle = sorted(circle, key=itemgetter(2), reverse=True)
    circle = circle[0]
    x, y, radius = circle
    cv.circle(image.display, (int(x), int(y)), int(radius),
              lib.get_color('green'), 3)
    x1, x2 = max(x - radius, 0), min(x + radius, wimg)
    y1, y2 = max(y - radius, 0), min(y + radius, himg)
    area = math.pi * (radius**2)
    forward, backward, left, right = get_excess(obj)
    lib.publish_result(obj, 'gray', public_topic + 'golf/mask')
    lib.publish_result(image.display, 'bgr', public_topic + 'golf/display')
    return message(state=state,
                   cx1=x1,
                   cy1=y1,
                   cx2=x2,
                   cy2=y2,
                   forward=forward,
                   backward=backward,
                   left=left,
                   right=right,
                   area=area)
示例#19
0
def find_gate():
    if image.bgr is None:
        lib.img_is_none()
        return message(state=-1)
    
    pre_process = lib.pre_process(image.bgr,'gate')
    gray = cv.cvtColor(pre_process.copy(), cv.COLOR_BGR2GRAY)
    hsv = cv.cvtColor(pre_process,cv.COLOR_BGR2HSV)
    b,g,r = cv.split(pre_process)
    # equ = lib.equalize(gray)
    clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    equ = clahe.apply(b)
    lib.publish_result(equ, 'gray', PUBLIC_TOPIC + 'gray')
    obj = lib.bg_subtraction(gray,mode='neg')

    kernel_box = lib.get_kernel(ksize=(7, 7))

    kernel_vertical = lib.get_kernel(ksize=(1, 21))
    vertical = cv.erode(obj.copy(), kernel_vertical)
    vertical = cv.dilate(vertical.copy(), kernel_box)
    kernel_erode = lib.get_kernel(ksize=(3, 11))
    vertical = cv.erode(vertical.copy(), kernel_erode)
    
    # remove flare from vertical
    vertical = cv.bitwise_and(vertical,cv.bitwise_not(get_flare(pre_process)))

    kernel_horizontal = lib.get_kernel(ksize=(21, 1))
    horizontal = cv.erode(obj.copy(), kernel_horizontal)
    horizontal = cv.dilate(horizontal.copy(), kernel_box)
    kernel_erode = lib.get_kernel(ksize=(11, 3))
    horizontal = cv.erode(horizontal.copy(), kernel_erode)
    horizontal = cv.bitwise_and(horizontal,cv.bitwise_not(get_flare(pre_process)))


    vertical_pipe, no_pipe_v = find_pipe(vertical, 'v')
    print('v',no_pipe_v)
    horizontal_pipe, no_pipe_h = find_pipe(horizontal, 'h')
    print('h',no_pipe_h)

    # horizontal
    if no_pipe_h > 0:
        x, y, w, h, angle = horizontal_pipe[0]
        # cv.rectangle(display, (int(x - w / 2.), int(y - h / 2.)),
        #              (int(x + w / 2.), int(y + h / 2.)), (0, 255, 0), 2)
        horizontal_x = [(x - w / 2.), (x + w / 2.)]
        horizontal_y = [(y - h / 2.), (y + h / 2.)]

    vertical_x1 = []
    vertical_x2 = []
    vertical_y1 = []
    vertical_y2 = []
    for res in vertical_pipe:
        x, y, w, h, angle = res
        cv.rectangle(image.display, (int(x - w / 2.), int(y - h / 2.)),
                     (int(x + w / 2.), int(y + h / 2.)), (108, 105, 255), 2)
        vertical_x1.append((x - w / 2.))
        vertical_x2.append((x + w / 2.))
        vertical_y1.append((y - h / 2.))
        vertical_y2.append((y + h / 2.))
        
    himg, wimg = obj.shape[:2]
    state = no_pipe_v

    if no_pipe_v == 1:
        state == 1
    elif no_pipe_v == 2:
        state == 2
    else:
        state == 0
    
    if state == 0:
        lib.print_result("NOT FOUND", ct.RED)
        lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'display')
        lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'mask/vertical')
        lib.publish_result(horizontal, 'gray',
                           PUBLIC_TOPIC + 'mask/horizontal')
        lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'mask')
        return message()
    
    if state == 2:
        lib.print_result("FOUND GATE", ct.GREEN)
        x1 = max(vertical_x1)
        x2 = min(vertical_x2)
        y1 = min(vertical_y1)
        y2 = max(vertical_y2)
    
    if state == 1:
        lib.print_result("FOUND ONE V", ct.YELLOW)
        x1 = int(min(vertical_x1))
        x2 = int(max(vertical_x2))
        y1 = int(min(vertical_y1))
        y2 = int(max(vertical_y2))
        h = int(abs(x1-x2))
        cy = (y1+y2)/2

        # print((y1,h,x1,x2))
        gray = equ
        temp = int(max(y1-1.5*h,0))
        left_h = gray[temp:y1+h/2, 0:x1]
        right_h = gray[temp:y1+h/2, x2:wimg]
        low_left_h = gray[cy:cy+h/2, 0:x1]
        low_right_h = gray[cy:cy+h/2, x2:wimg]

        
        lib.publish_result(left_h, 'gray', PUBLIC_TOPIC + 'l')
        lib.publish_result(right_h, 'gray', PUBLIC_TOPIC + 'r')
        lib.publish_result(low_left_h, 'gray', PUBLIC_TOPIC + 'll')
        lib.publish_result(low_right_h, 'gray', PUBLIC_TOPIC + 'lr')
        # mode_left_h = get_mode(left_h.ravel())
        # mode_right_h = get_mode(right_h.ravel())
        # mode_low_left_h = get_mode(low_left_h.ravel())
        # mode_low_right_h = get_mode(low_right_h.ravel())
        
        mean_left_h = 1.0*get_mean(left_h.ravel())
        mean_right_h = 1.0*get_mean(right_h.ravel())
        mean_low_left_h = 1.0*get_mean(low_left_h.ravel())
        mean_low_right_h = 1.0*get_mean(low_right_h.ravel())

        diff_left = abs(mean_left_h-mean_low_left_h)
        diff_right = abs(mean_right_h-mean_low_right_h)
        
        print(mean_left_h,mean_low_left_h,mean_right_h,mean_low_right_h)

        # if(mean_left_h < mean_right_h):
        # print(abs(diff_left-diff_right))
        # if abs(diff_left-diff_right) < 500:
        #     pass
        if (diff_left > diff_right):
            x1, x2 = 0, min(x1,x2)
        else:
            x1, x2 = max(x1,x2), wimg
                
   
    right_excess = (x2 > 0.95*wimg)
    left_excess = (x1 < (0.05*wimg))
    if (right_excess and not left_excess):
        pos = 1
    elif (not right_excess and left_excess):
        pos = -1
    else:
        pos = 0

    cv.rectangle(image.display, (int(x1), int(y1)),
         (int(x2), int(y2)), (0, 255, 0), 3)
    cv.circle(image.display, (int((x1+x2)/2), int((y1+y2)/2)),
              3, (0, 255, 255), -1)
    area = 1.0*abs(x2-x1)*abs(y1-y2)/(himg*wimg)
    lib.publish_result(image.display, 'bgr', PUBLIC_TOPIC + 'display')
    lib.publish_result(vertical, 'gray', PUBLIC_TOPIC + 'mask/vertical')
    lib.publish_result(horizontal, 'gray', PUBLIC_TOPIC + 'mask/horizontal')
    lib.publish_result(obj, 'gray', PUBLIC_TOPIC + 'mask')
    return message(state=state, x1=x1, x2=x2, y1=y1, y2=y2, pos=pos, area=area)