Beispiel #1
0
def remove_background(
    *,
    image: Image,
    smooth_kernel_size: int = 2,
    threshold_value: int = 100,
    background_kernel_size: int = 5,
    debug: bool = False
) -> None:
    """Remove the background milimeter pattern."""
    if debug:
        debug_path = get_debug_path("remove_background")
        save(np.ndarray, debug_path, "input")

    image.invert()              # We want the main features to be white
    image.blur(smooth_kernel_size)
    image.threshold(threshold_value)        # Removes most of the milimiter markers

    remove_structured_background(
        image_array=image.image,
        background_kernel_size=(background_kernel_size, background_kernel_size),
        smoothing_kernel_size=(smooth_kernel_size, smooth_kernel_size),
        debug=debug
    )
    if debug:
        save(np.ndarray, debug_path, "output")

    image.invert()
    image.checkpoint("preprocessed")
Beispiel #2
0
def prepare_lines(image):
    if debug:
        debug_path = get_debug_path("prepare_lines")
        save(np.ndarray, debug_path, "input")

    image.bgr_to_gray()
    image.checkpoint("resampled")

    # TODO: I can move remove_background in here
    remove_background(image=image, smooth_kernel_size=3)
    features = extract_contours(image=image, blur_kernel_size=3)
    if debug:
        save(np.ndarray, debug_path, "remove_background")

    image.invert()
    return features
Beispiel #3
0
def run(input_image_path: Path,
        output_directory: Path,
        name: str,
        *,
        debug: bool = False,
        compute_scale: bool = True,
        kernel_length: int = 9,
        num_iterations: int = 4,
        blur_size: int = 9,
        thresh_val: float = 175,
        dx: float = 2.5e-4,
        dy: float = 2.5e-4):
    image = read_image(input_image_path)
    image_list = split_image(image,
                             kernel_length=kernel_length,
                             blur_kernel_size=blur_size,
                             threshold_value=thresh_val,
                             num_iterations=num_iterations)

    if debug:
        debug_path = get_debug_path("split_image")
        save(image.image, debug_path, "input")

    scale_dict = {}
    for i, image in enumerate(image_list):
        image.bgr_to_gray()
        # TODO: debug each split
        rectangles = markers(image,
                             kernel_length=kernel_length,
                             blur_kernel_size=blur_size,
                             threshold_value=thresh_val,
                             num_iterations=num_iterations,
                             debug=debug)
        if len(rectangles) >= 2:
            axis, scale = get_axis(image, rectangles)
            image.set_axis(axis)
            image.set_scale(scale)
            image.reset_image()
            resample(image, step_x=dx, step_y=dy)
            image.checkpoint()
        else:
            print(f"skipping split {i}")
            continue

        if compute_scale:  # Recompute scale
            image.bgr_to_gray()
            rectangles = markers(image,
                                 kernel_length=kernel_length,
                                 blur_kernel_size=blur_size,
                                 threshold_value=thresh_val,
                                 num_iterations=num_iterations,
                                 debug=debug)
            if len(rectangles) < 2:
                logger.info(
                    f"Cannot compute scale of split {i} -- using average of previous scales"
                )
                if len(scale_dict) > 0:
                    scale_dict[i] = sum(scale_dict.values()) / len(scale_dict)
            else:
                axis, scale = get_axis(image, rectangles)
                image.set_axis(axis)
                image.set_scale(scale)
                scale_dict[i] = scale

    output_directory.mkdir(exist_ok=True, parents=True)

    for i, image in enumerate(image_list):
        logger.info(f"Scale: {image.scale}")
        save_image(output_directory / f"{name}_split{i}.png", image)
        # dump_image(output_directory / f"split{i}_{identifier}.pkl", image)

    with open(output_directory / "scales.txt", "a") as output_handle:
        for case, scale in scale_dict.items():
            output_handle.write(f"Case: {case}, Scale: {scale}\n")
Beispiel #4
0
def run(
    *,
    input_image_path: Path,
    output_directory: Path,
    identifier: str,
    scale: float = None,
    debug: bool = False,
):
    """Segment the contours from a black and white image and save the segmented lines."""
    image = read_image(input_image_path)
    if debug:
        debug_path = get_debug_path("remove_background")
        save(image.image, debug_path, "input")
    image.bgr_to_gray()

    if debug:
        save(image.iamge, debug_path, "match_contours")
    contours = get_contours(image=image)
    features = match_contours(matcher=get_graph_matcher(), contours=contours)

    if debug:
        debug_path = get_debug_path("extract_contours_bw")
        save(image.image, debug_path, "input")

    output_directory.mkdir(exist_ok=True, parents=True)

    for i, c in enumerate(features):
        tmp_image = image.copy_image()
        filter_contours(image_array=tmp_image, contours=[c])
        clipped_contour = tmp_image[~np.all(tmp_image == 0, axis=1)]
        save_image(output_directory / f"{identifier}_trace{i}.png",
                   Image(clipped_contour))

    ############################
    ### Make annotated image ###
    ############################

    fig, ax = plt.subplots(1, figsize=(15, 10), dpi=500)
    tmp_image = image.image_orig

    color_iterator = itertools.cycle(mtableau_brg())

    for i, c in enumerate(features):
        color = next(color_iterator)
        tmp_image = cv2.drawContours(tmp_image, features, i,
                                     color_to_256_RGB(color), cv2.FILLED)
        polygon = Polygon(c.reshape(-1, 2))
        x0, y0, x1, y1 = polygon.bounds

        ann = ax.annotate(
            f"Contour {i}",
            xy=(x1, y1),  # (x0, y1)
            xycoords="data",
            xytext=(0, 35),
            textcoords="offset points",
            size=10,
            bbox=dict(
                boxstyle="round",
                fc=color  # normalised color
            ))

    ax.imshow(tmp_image)
    ax.set_title("A digitised paper strip")
    if scale is not None:  # multiply by distance between black sqaures?
        ax.set_xticklabels(
            ["{:.1f} cm".format(15 * i / scale) for i in ax.get_xticks()])
        ax.set_yticklabels(
            ["{:.1f} cm".format(15 * i / scale) for i in ax.get_yticks()])
    ax.set_ylabel("Voltage")
    ax.set_xlabel("Time")
    fig.savefig(output_directory / f"{identifier}_annotated.png", dpi=500)
Beispiel #5
0
def extract_contours(
    *,
    image: Image,
    blur_kernel_size: int = 3,
    dilate_kernel_size: int = 3,
    num_dilate_iterations: int= 3,
    debug: bool = True
) -> tp.List[np.ndarray]:
    """Segment the EEG traces.

    Use a series of convolutions, filtering and edge tracking to extract the contours.

    blur_kernel_size:
        Used in conjunction with thresholding to binarise the image.
    dilate_kernel_size:
        Dilation is used with filtering to be aggressive in removing elements.
    num_dilate_iterations:
        THe number of dilate iterations.
    """
    if debug:
        debug_path = get_debug_path("extract_contours")
        save(np.ndarray, debug_path, "input")
    # Remove initial guess at contours. This should leave the text blocks.
    image.invert()
    contours = get_contours(image=image)
    features = match_contours(matcher=get_graph_matcher(approximation_tolerance=1e-2), contours=contours)
    remove_contours(image, features)
    if debug:
        save(np.ndarray, debug_path, "remove_contours")

    # Remove all the remaining text blobs
    image.blur(blur_kernel_size)
    image.threshold(100)
    image.morph(cv2.MORPH_DILATE, (dilate_kernel_size, dilate_kernel_size), num_dilate_iterations)
    contours = get_contours(image=image, min_size=6)

    # Compute all the bounding boxes and filter based on the aspect ratio?
    features = match_contours(
        matcher=get_bounding_rectangle_matcher(min_solidity=0.7),
        contours=contours
    )
    filter_contours(image_array=image.image, contours=features)
    image.morph(cv2.MORPH_DILATE, (dilate_kernel_size, dilate_kernel_size), num_dilate_iterations)
    image_mask = image.copy_image()

    image.reset_image("preprocessed")
    filter_image(image_array=image.image, binary_mask=image_mask == 255)
    image.checkpoint("preprocessed")
    if debug:
        save(np.ndarray, debug_path, "filter_contours1")

    # Match the remaining graph candidates, and remove everything else
    image.invert()
    image.blur(blur_kernel_size)
    image.threshold()
    image.morph(cv2.MORPH_DILATE, (dilate_kernel_size, dilate_kernel_size), num_dilate_iterations)

    contours = get_contours(image=image)
    features = match_contours(matcher=get_graph_matcher(), contours=contours)
    filter_contours(image_array=image.image, contours=features)
    if debug:
        save(np.ndarray, debug_path, "filter_contours2")

    image.reset_image("resampled")

    # TODO: Why invert? Has something to do with the fill color in filter_contours
    image.invert()
    filter_contours(image_array=image.image, contours=features)
    image.invert()
    if debug:
        save(np.ndarray, debug_path, "filter_contours3")

    image.blur(blur_kernel_size)
    image.threshold(100)

    image.invert()
    contours = get_contours(image=image)
    features = match_contours(matcher=get_graph_matcher(), contours=contours)
    return features
Beispiel #6
0
def run(
    input_image_path: Path,
    output_directory: Path,
    identifier: str,
    scale: float = None,
    debug:bool
):
    """Remove the background, segment the contours and save the segmented lines as pngs."""
    image = read_image(input_image_path)
    if debug:
        debug_path = get_debug_path("prepare_lines")
        save(np.ndarray, debug_path, "input")

    image.bgr_to_gray()
    image.checkpoint("resampled")

    remove_background(image=image, smooth_kernel_size=3)
    features = extract_contours(image=image, blur_kernel_size=3)
    if debug:
        save(np.ndarray, debug_path, "remove_background")
    image.invert()


    image.reset_image("resampled")
    image.invert()

    output_directory.mkdir(exist_ok=True, parents=True)

    for i, c in enumerate(features):
        tmp_image = image.copy_image()
        filter_contours(image_array=tmp_image, contours=[c])
        clipped_contour = tmp_image[~np.all(tmp_image == 0, axis=1)]
        save_image(output_directory / f"{identifier}_trace{i}.png", Image(clipped_contour))

    ##################
    ### Make image ###
    ##################

    tmp_image = np.ones((*image.image.shape, 3), dtype=np.uint8)
    tmp_image[:] = (255, 255, 255)      # White

    fig, ax = plt.subplots(1)
    ax.imshow(tmp_image)

    color_iterator = itertools.cycle(mtableau_brg())

    for i, c in enumerate(features):
        color = next(color_iterator)
        tmp_image = cv2.drawContours(tmp_image, features, i, color_to_256_RGB(color), cv2.FILLED)
        polygon = Polygon(c.reshape(-1, 2))
        x0, y0, x1, y1 = polygon.bounds

        tmp_image2 = np.zeros(tuple(map(math.ceil, (y1, x1))), dtype=np.uint8)
        tmp_image2 = cv2.drawContours(tmp_image2, features, i, 255, cv2.FILLED)

        ann = ax.annotate(
            f"Contour {i}",
            xy=(x0, y1),
            xycoords="data",
            xytext=(0, 35),
            textcoords="offset points",
            size=10,
            bbox=dict(
                boxstyle="round",
                fc=color       # normalised color
            )
        )

    ax.imshow(tmp_image)
    ax.set_title("A digitised paper strip")
    if scale is not None:       # multiply by distance between black sqaures?
        ax.set_xticklabels(["{:.1f} cm".format(15*i/scale) for i in ax.get_xticks()])
        ax.set_yticklabels(["{:.1f} cm".format(15*i/scale) for i in ax.get_yticks()])
    ax.set_ylabel("Voltage")
    ax.set_xlabel("Time")
    fig.savefig(output_directory / f"{identifier}_annotated.png")
Beispiel #7
0
def run(*,
        input_image_path: Path,
        output_directory: Path,
        identifier: str,
        smooth_kernel_size: int = 3,
        threshold_value: int = 100,
        background_kernel_size=5,
        scale: float = None,
        debug: bool = False,
        blue_color_filter: tp.Sequence[int] = None,
        red_color_filter: tp.Sequence[int] = None,
        horisontal_kernel_length: int = None,
        x_interval: tp.Tuple[int, int] = None):
    """Remove the background, segment the contours and save the segmented lines as pngs."""
    image = read_image(input_image_path)

    if debug:
        debug_path = get_debug_path("prepare_lines")
        save(image.image, debug_path, "input")

    if blue_color_filter is not None:
        lower = tuple(map(int, blue_color_filter[:3]))
        upper = tuple(map(int, blue_color_filter[3:]))
        print(lower, upper)
        blue_mask = cv2.inRange(image.image, lower, upper)
        image.image[blue_mask == 255] = 255
    if red_color_filter is not None:
        lower = tuple(map(int, red_color_filter[:3]))
        upper = tuple(map(int, red_color_filter[3:]))
        red_mask = cv2.inRange(image.image, lower, upper)
        image.image[red_mask == 255] = 255

    image.invert()
    image.bgr_to_gray()
    image.checkpoint("resampled")

    if horisontal_kernel_length is not None:
        horisontal_kernel = cv2.getStructuringElement(
            cv2.MORPH_RECT, (horisontal_kernel_length, 1))

        x0, x1 = x_interval
        if x1 == -1:  # Set -1 to be inclusive last element
            x1 = None
        detected_lines = cv2.morphologyEx(image.image[x0:x1, :],
                                          cv2.MORPH_OPEN,
                                          horisontal_kernel,
                                          iterations=1)

        # findContours returns (contours, hierarchy)
        contours = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[0]
        for c in contours:
            cv2.drawContours(image.image[x0:x1, :], [c], -1, 0, -10)

    remove_background(image=image,
                      smooth_kernel_size=smooth_kernel_size,
                      threshold_value=threshold_value,
                      background_kernel_size=background_kernel_size,
                      debug=debug)
    image.checkpoint("remove_background")

    if debug:
        save(image.image, debug_path, "match_contours")
    contours = get_contours(image=image)
    features = match_contours(matcher=get_graph_matcher(), contours=contours)
    if features is None:  # In case of a blank image
        return

    quality_control_image = image.draw(features, show=False)
    cv2.imwrite(str(output_directory / f"QC_{identifier}.png"),
                quality_control_image)

    if debug:
        save(image.image, debug_path, "remove_background")

    # image.reset_image("resampled")
    image.reset_image("remove_background")
    # image.invert()

    output_directory.mkdir(exist_ok=True, parents=True)

    for i, c in enumerate(features):
        tmp_image = image.copy_image()
        filter_contours(image_array=tmp_image, contours=[c])
        clipped_contour = tmp_image[~np.all(tmp_image == 0, axis=1)]
        save_image(output_directory / f"{identifier}_trace{i}.png",
                   Image(clipped_contour))

    ############################
    ### Make annotated image ###
    ############################

    fig, ax = plt.subplots(1, figsize=(15, 10), dpi=500)
    tmp_image = image.image_orig

    color_iterator = itertools.cycle(mtableau_brg())

    for i, c in enumerate(features):
        color = next(color_iterator)
        tmp_image = cv2.drawContours(tmp_image, features, i,
                                     color_to_256_RGB(color), cv2.FILLED)
        polygon = Polygon(c.reshape(-1, 2))
        x0, y0, x1, y1 = polygon.bounds

        ann = ax.annotate(
            f"Contour {i}",
            xy=((x0 + x1) / 2, y1),  # (x0, y1)
            size=5,
            color=color,
            arrowprops=dict(arrowstyle='->',
                            connectionstyle="arc3,rad=-0.5",
                            color=color))

    ax.imshow(tmp_image)
    ax.set_title("A digitised paper strip")
    if scale is not None:  # multiply by distance between black sqaures?
        ax.set_xticklabels(
            ["{:.1f} cm".format(15 * i / scale) for i in ax.get_xticks()])
        ax.set_yticklabels(
            ["{:.1f} cm".format(15 * i / scale) for i in ax.get_yticks()])
    ax.set_ylabel("Voltage")
    ax.set_xlabel("Time")
    fig.savefig(output_directory / f"{identifier}_annotated.png", dpi=500)