Пример #1
0
def find_signs(image_path):
    image = cv2.imread(image_path)
    signs = analyze_signs_from_image(
        image, method=DETECT_BY_CONTOUR) + analyze_signs_from_image(
            image, method=DETECT_BY_SEGMENTS)
    signs = remove_fully_contained_signs(signs)
    rectangle_signs = []
    for coordinates in signs:
        wrapped = straighten_signs.four_point_transform(image, coordinates)
        rectangle_signs.append(cv2.cvtColor(wrapped, cv2.COLOR_BGR2RGB))
    signs_with_text = []
    for sign in rectangle_signs:
        if not get_text_from_image(sign) == "":
            signs_with_text.append(sign)
            imshow(sign, "Sign")
    return signs_with_text
Пример #2
0
def find_contour(thresh):
    contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)
    print("Found " + str(len(contours)) + " contours.") if DEBUG else None

    large_contours = []
    for c in contours:
        area = cv2.contourArea(c)
        image_area = thresh.shape[0] * thresh.shape[1]
        if MIN_SQUARE_AREA_RATIO * image_area < area < MAX_SQUARE_AREA_RATIO * image_area:
            large_contours.append(c)
    if DEBUG:
        copy = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
        cv2.drawContours(copy, large_contours, -1, (0, 0, 255), 4)
        imshow(copy, "contour")
        print(str(len(large_contours)) + " of them are large enough.")
    return large_contours
Пример #3
0
def analyze_signs_from_image(image, method):
    processed = preprocess_image(image)
    imshow(processed, "Processed")
    masks_dict = colors.get_masks(processed)
    squares = []
    for color in COLORS:
        current_channel = masks_dict[color]
        processed = process_mask(current_channel, color=color)
        if method == DETECT_BY_LINES:
            signs = edge_detection.detect_squares_by_lines(processed)
        elif method == DETECT_BY_SEGMENTS:
            signs = edge_detection.detect_squares_by_segments(processed)
        elif method == DETECT_BY_CONTOUR:
            contour = contour_detection.find_contour(processed)
            signs = contour_detection.get_squares(contour)
        else:
            print("Does not recognize method")
            raise AttributeError
        if not len(signs) == 0:
            squares.append(signs)
    if len(squares) == 0:
        return []
    return [item for sublist in squares for item in sublist]
Пример #4
0
def process_mask(image, color='Image'):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    imshow(gray, color)

    open_width = int(np.ceil(image.shape[0] * OPEN_RATIO) // 2 * 2 + 1)
    open_height = int(np.ceil(image.shape[1] * OPEN_RATIO) // 2 * 2 + 1)
    open_kernel = np.ones((open_width, open_height), np.uint8)
    img_open = cv2.morphologyEx(gray, cv2.MORPH_OPEN, open_kernel)
    imshow(img_open, color + ' opening')

    close_width = int(np.ceil(image.shape[0] * CLOSING_RATIO) // 2 * 2 + 1)
    close_height = int(np.ceil(image.shape[1] * CLOSING_RATIO) // 2 * 2 + 1)
    closing_kernel = np.ones((close_width, close_height), np.uint8)
    img_no_line_gaps = cv2.morphologyEx(img_open, cv2.MORPH_CLOSE,
                                        closing_kernel)
    imshow(img_no_line_gaps, color + " closing")

    return img_no_line_gaps
Пример #5
0
def analyze_image(image):
    imshow(image, "Original")
    signs_list = analyze_signs_from_image(image, method=DETECT_BY_SEGMENTS)
    image = visualize.show_squares(image, signs_list)
    imshow(image, "Final")
    return image