def process_image(i):
        path = df["path"].iloc[i]
        img = open_colour_image(path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        contour = find_eye(img)
        x, y, w, h = cv2.boundingRect(contour)
        img = img[y : y + h, x : x + w]
        img = pad_to_square(img, w, h, [BLACK, BLACK, BLACK])

        img = cv2.resize(img, (512, 512))

        scale = 512
        img = cv2.addWeighted(
            img, 4, cv2.GaussianBlur(img, (0, 0), scale / 30), -4, 128
        )

        # Remove outer 10% boundary effects
        b = np.zeros(img.shape)
        b = cv2.circle(
            b,
            (img.shape[1] // 2, img.shape[0] // 2),
            int(scale * 0.9) // 2,
            (1, 1, 1),
            -1,
            8,
            0,
        )
        img = img * b + 128 * (1 - b)

        img = np.transpose(img, (2, 0, 1))
        grp_images[i, ...] = img[None]
예제 #2
0
def process_image(
    image_name: str,
    retina_path: Path,
    img_output_path: Path,
    img_transformed_output_path: Path,
):
    retina_img = open_colour_image(retina_path / image_name)
    contour = find_eye(retina_img)

    # Find bounding box.
    x, y, w, h = cv2.boundingRect(contour)

    # Crop around bounding box.
    retina_img = retina_img[y : y + h, x : x + w]

    # Pad to square.
    retina_img = pad_to_square(retina_img, w, h, [BLACK, BLACK, BLACK])

    # Resize to 1280 x 1280.
    retina_img = cv2.resize(retina_img, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    transformed_retina_img = transform(retina_img)

    new_name = change_suffix(image_name, ".png")

    write_image(retina_img, img_output_path / new_name)
    write_image(transformed_retina_img, img_transformed_output_path / new_name)
예제 #3
0
def main():
    name = "od_transformed"
    out_dir = Path("data/eophtha/od")
    out_dir.mkdir(parents=True, exist_ok=True)
    model = load_binary_segmentation_model(name)
    root_path = Path("/vol/bitbucket/js6317/individual-project/data/e_optha")
    path_1 = root_path / "e_optha_MA" / "healthy"
    path_2 = root_path / "e_optha_MA" / "MA"
    path_3 = root_path / "e_optha_EX" / "EX"
    path_4 = root_path / "e_optha_EX" / "healthy"

    suffixes = [".JPG", ".jpg", ".PNG", ".png", ".JPEG", ".jpeg"]
    files_1 = [f for f in path_1.glob("**/*") if f.is_file() and f.suffix in suffixes]
    files_2 = [f for f in path_2.glob("**/*") if f.is_file() and f.suffix in suffixes]
    files_3 = [f for f in path_3.glob("**/*") if f.is_file() and f.suffix in suffixes]
    files_4 = [f for f in path_4.glob("**/*") if f.is_file() and f.suffix in suffixes]
    files = files_1 + files_2 + files_3 + files_4
    for file in tqdm(files):
        img = open_colour_image(file)

        contour = find_eye(img)
        # Find bounding box.
        x, y, w, h = cv2.boundingRect(contour)

        # Crop around bounding box.
        img = img[y : y + h, x : x + w]

        # Pad to square.
        img = pad_to_square(img, w, h, [BLACK, BLACK, BLACK])

        # Resize to 1280 x 1280.
        img = cv2.resize(img, (1280, 1280), interpolation=cv2.INTER_NEAREST)
        transformed_image = transform(img)

        predicted = predict_od(model, transformed_image)

        image_path = Path(*file.parts[-2:])
        new_name = "_".join(image_path.parts)
        new_name = change_suffix(new_name, ".png")
        cv2.imwrite(f"data/eophtha/od/{new_name}", predicted * 255.0)
def process_image(
    retina_path: Path,
    ex_path: Path,
    ma_path: Path,
    od_path: Path,
    label_output_path: Path,
    instance_output_path: Path,
    img_output_path: Path,
    img_transformed_output_path: Path,
    colour: bool,
):
    retina_img = open_colour_image(retina_path)
    image_path = Path(*retina_path.parts[-2:])

    height, width, _ = retina_img.shape
    img_size = (height, width)

    contour = find_eye(retina_img)

    ex_label = np.ones(img_size, dtype="uint8") * GRAY_CLASS["EX"]
    ma_label = np.ones(img_size, dtype="uint8") * GRAY_CLASS["MA"]

    ex_img = open_binary_mask(ex_path /
                              change_suffix(str(image_path), f"_EX.png"),
                              img_size=img_size)
    ma_img = open_binary_mask(ma_path /
                              change_suffix(str(image_path), f".png"),
                              img_size=img_size)

    mask = np.ones(img_size, dtype="uint8") * WHITE

    fill_contours(mask, [contour], GRAY_CLASS["RETINA"])
    overlay_label(mask, ex_img, ex_label)
    overlay_label(mask, ma_img, ma_label)

    inst = np.ones(img_size, dtype="uint8") * WHITE

    # Find bounding box.
    x, y, w, h = cv2.boundingRect(contour)

    # Crop around bounding box.
    mask = mask[y:y + h, x:x + w]
    inst = inst[y:y + h, x:x + w]
    retina_img = retina_img[y:y + h, x:x + w]

    # Pad to square.
    mask = pad_to_square(mask, w, h, WHITE)
    inst = pad_to_square(inst, w, h, WHITE)
    retina_img = pad_to_square(retina_img, w, h, [BLACK, BLACK, BLACK])

    # Resize to 1280 x 1280.
    mask = cv2.resize(mask, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    inst = cv2.resize(inst, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    retina_img = cv2.resize(retina_img, (1280, 1280),
                            interpolation=cv2.INTER_NEAREST)
    transformed_retina_img = transform(retina_img)

    new_name = "_".join(image_path.parts)
    new_name = change_suffix(new_name, ".png")

    od_img = open_binary_mask(od_path / new_name)

    od_label = np.ones((1280, 1280), dtype="uint8") * GRAY_CLASS["OD"]
    overlay_label(mask, od_img, od_label)

    bg_label = np.ones((1280, 1280), dtype="uint8") * GRAY_CLASS["RETINA"]
    overlay_label(inst, od_img, bg_label)

    if colour:
        mask = colour_labels_numpy(mask)

    write_image(mask, label_output_path / new_name)
    write_image(inst, instance_output_path / new_name)
    write_image(retina_img, img_output_path / new_name)
    write_image(transformed_retina_img, img_transformed_output_path / new_name)
예제 #5
0
def process_image(
    image_name: str,
    retina_path: Path,
    ex_path: Path,
    he_path: Path,
    ma_path: Path,
    se_path: Path,
    od_path: Path,
    ex_label: np.ndarray,
    he_label: np.ndarray,
    ma_label: np.ndarray,
    se_label: np.ndarray,
    od_label: np.ndarray,
    bg_label: np.ndarray,
    label_output_path: Path,
    instance_output_path: Path,
    img_output_path: Path,
    img_transformed_output_path: Path,
    colour: bool,
):
    retina_img = open_colour_image(retina_path / image_name)
    contour = find_eye(retina_img)

    ex_img = open_binary_mask(ex_path / change_suffix(image_name, "_EX.tif"))
    he_img = open_binary_mask(he_path / change_suffix(image_name, "_HE.tif"))
    ma_img = open_binary_mask(ma_path / change_suffix(image_name, "_MA.tif"))
    se_img = open_binary_mask(se_path / change_suffix(image_name, "_SE.tif"))
    od_img = open_binary_mask(od_path / change_suffix(image_name, "_OD.tif"))

    mask = np.ones((2848, 4288), dtype="uint8") * WHITE

    fill_contours(mask, [contour], GRAY_CLASS["RETINA"])
    overlay_label(mask, od_img, od_label)
    overlay_label(mask, ex_img, ex_label)
    overlay_label(mask, he_img, he_label)
    overlay_label(mask, ma_img, ma_label)
    overlay_label(mask, se_img, se_label)

    inst = np.ones((2848, 4288), dtype="uint8") * WHITE
    overlay_label(inst, od_img, bg_label)

    # Find bounding box.
    x, y, w, h = cv2.boundingRect(contour)

    # Crop around bounding box.
    mask = mask[y:y + h, x:x + w]
    inst = inst[y:y + h, x:x + w]
    retina_img = retina_img[y:y + h, x:x + w]

    # Pad to square.
    mask = pad_to_square(mask, w, h, WHITE)
    inst = pad_to_square(inst, w, h, WHITE)
    retina_img = pad_to_square(retina_img, w, h, [BLACK, BLACK, BLACK])

    # Resize to 1280 x 1280.
    mask = cv2.resize(mask, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    inst = cv2.resize(inst, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    retina_img = cv2.resize(retina_img, (1280, 1280),
                            interpolation=cv2.INTER_NEAREST)
    transformed_retina_img = transform(retina_img)

    new_name = change_suffix(image_name, ".png")

    if colour:
        mask = colour_labels_numpy(mask)

    write_image(mask, label_output_path / new_name)
    write_image(inst, instance_output_path / new_name)
    write_image(retina_img, img_output_path / new_name)
    write_image(transformed_retina_img, img_transformed_output_path / new_name)
def process_image(
    image_name: str,
    retina_path: Path,
    ex_path: Path,
    he_path: Path,
    ma_path: Path,
    se_path: Path,
    retina_mask: np.ndarray,
    retina_label: np.ndarray,
    ex_label: np.ndarray,
    he_label: np.ndarray,
    ma_label: np.ndarray,
    se_label: np.ndarray,
    label_output_path: Path,
    inst_output_path: Path,
    img_output_path: Path,
    img_transformed_output_path: Path,
    od_file_path: str,
    colour: bool,
):
    retina_img = open_colour_image(retina_path / image_name)
    contour = find_eye(retina_mask)

    ex_img = open_binary_mask(ex_path / image_name)
    he_img = open_binary_mask(he_path / image_name)
    ma_img = open_binary_mask(ma_path / image_name)
    se_img = open_binary_mask(se_path / image_name)

    img_size = (1152, 1500)
    mask = np.ones(img_size, dtype="uint8") * WHITE
    inst = np.ones(img_size, dtype="uint8") * WHITE

    thresh = int(MAX_VALUE * CONFIDENCE_THRESHOLD)
    overlay_label(mask, retina_mask, retina_label, thresh)
    draw_od(mask, inst, image_name, od_file_path)
    overlay_label(mask, ex_img, ex_label, thresh=thresh)
    overlay_label(mask, he_img, he_label, thresh=thresh)
    overlay_label(mask, ma_img, ma_label, thresh=thresh)
    overlay_label(mask, se_img, se_label, thresh=thresh)

    # Find bounding box.
    x, y, w, h = cv2.boundingRect(contour)

    # Crop around bounding box.
    mask = mask[y : y + h, x : x + w]
    inst = inst[y : y + h, x : x + w]
    retina_img = retina_img[y : y + h, x : x + w]

    # Pad to square.
    mask = pad_to_square(mask, w, h, WHITE)
    inst = pad_to_square(inst, w, h, WHITE)
    retina_img = pad_to_square(retina_img, w, h, [BLACK, BLACK, BLACK])

    # Resize to 1280 x 1280.
    mask = cv2.resize(mask, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    inst = cv2.resize(inst, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    retina_img = cv2.resize(retina_img, (1280, 1280), interpolation=cv2.INTER_NEAREST)
    transformed_retina_img = transform(retina_img)

    if colour:
        mask = colour_labels_numpy(mask)

    write_image(mask, label_output_path / image_name)
    write_image(inst, inst_output_path / image_name)
    write_image(retina_img, img_output_path / image_name)
    write_image(transformed_retina_img, img_transformed_output_path / image_name)