Beispiel #1
0
def test_reinhard_normalise(_source_image, _norm_reinhard):
    """Test for Reinhard colour normalisation."""
    file_parent_dir = pathlib.Path(__file__).parent
    source_img = imread(pathlib.Path(_source_image))
    target_img = imread(file_parent_dir.joinpath("../data/target_image.png"))
    reinhard_img = imread(pathlib.Path(_norm_reinhard))

    norm = get_normaliser("reinhard")
    norm.fit(target_img)  # get stain information of target image
    transform = norm.transform(source_img)  # transform source image

    assert np.shape(transform) == np.shape(source_img)
    assert np.mean(
        np.absolute(reinhard_img / 255.0 - transform / 255.0)) < 1e-2
Beispiel #2
0
def test_macenko_normalise(_source_image, _norm_macenko):
    """Test for stain normalisation with stain matrix from Macenko et al."""
    file_parent_dir = pathlib.Path(__file__).parent
    source_img = imread(pathlib.Path(_source_image))
    target_img = imread(file_parent_dir.joinpath("../data/target_image.png"))
    macenko_img = imread(pathlib.Path(_norm_macenko))

    # init class with Macenko method
    norm = get_normaliser("macenko")
    norm.fit(target_img)  # get stain information of target image
    transform = norm.transform(source_img)  # transform source image

    assert np.shape(transform) == np.shape(source_img)
    assert np.mean(np.absolute(macenko_img / 255.0 - transform / 255.0)) < 1e-2
Beispiel #3
0
def test_ruifrok_normalise(_source_image, _norm_ruifrok):
    """Test for stain normalisation with stain matrix from Ruifrok and Johnston."""
    file_parent_dir = pathlib.Path(__file__).parent
    source_img = imread(pathlib.Path(_source_image))
    target_img = imread(file_parent_dir.joinpath("../data/target_image.png"))
    ruifrok_img = imread(pathlib.Path(_norm_ruifrok))

    # init class with Ruifrok & Johnston method
    norm = get_normaliser("ruifrok")
    norm.fit(target_img)  # get stain information of target image
    transform = norm.transform(source_img)  # transform source image

    assert np.shape(transform) == np.shape(source_img)
    assert np.mean(np.absolute(ruifrok_img / 255.0 - transform / 255.0)) < 1e-2
Beispiel #4
0
def test_custom_normalise(_source_image, _norm_ruifrok):
    """Test for stain normalisation with user-defined stain matrix."""
    file_parent_dir = pathlib.Path(__file__).parent
    source_img = imread(pathlib.Path(_source_image))
    target_img = imread(file_parent_dir.joinpath("../data/target_image.png"))
    custom_img = imread(pathlib.Path(_norm_ruifrok))

    # init class with custom method - test with ruifrok stain matrix
    stain_matrix = np.array([[0.65, 0.70, 0.29], [0.07, 0.99, 0.11]])
    norm = get_normaliser("custom", stain_matrix=stain_matrix)
    norm.fit(target_img)  # get stain information of target image
    transform = norm.transform(source_img)  # transform source image

    assert np.shape(transform) == np.shape(source_img)
    assert np.mean(np.absolute(custom_img / 255.0 - transform / 255.0)) < 1e-2
def test_get_patch_extractor(_source_image, _patch_extr_csv):
    """Test get_patch_extractor returns the right object."""
    input_img = misc.imread(pathlib.Path(_source_image))
    locations_list = pathlib.Path(_patch_extr_csv)
    points = patchextraction.get_patch_extractor(
        input_img=input_img,
        locations_list=locations_list,
        method_name="point",
        patch_size=(200, 200),
    )

    assert isinstance(points, patchextraction.PointsPatchExtractor)

    with pytest.raises(MethodNotSupported):
        points.merge_patches()

    fixed_window = patchextraction.get_patch_extractor(
        input_img=input_img,
        method_name="fixedwindow",
        patch_size=(200, 200),
    )

    assert isinstance(fixed_window, patchextraction.FixedWindowPatchExtractor)

    variable_window = patchextraction.get_patch_extractor(
        input_img=input_img,
        method_name="variablewindow",
        patch_size=(200, 200),
    )

    assert isinstance(variable_window, patchextraction.VariableWindowPatchExtractor)

    with pytest.raises(MethodNotSupported):
        patchextraction.get_patch_extractor("unknown")
def test_patch_extractor(_source_image):
    """Test base class patch extractor."""
    input_img = misc.imread(pathlib.Path(_source_image))
    patches = patchextraction.PatchExtractor(input_img=input_img, patch_size=(20, 20))
    next_patches = iter(patches)
    assert next_patches.n == 0
def test_fixed_window_patch_extractor(_patch_extr_vf_image):
    """Test FixedWindowPatchExtractor for VF."""
    input_img = pathlib.Path(_patch_extr_vf_image)

    stride = (20, 20)
    patch_size = (200, 200)
    img = misc.imread(input_img)

    img_h = img.shape[1]
    img_w = img.shape[0]

    num_patches_img_h = int(math.ceil((img_h - patch_size[1]) / stride[1] + 1))
    num_patches_img_w = int(math.ceil(((img_w - patch_size[0]) / stride[0] + 1)))
    num_patches_img = num_patches_img_h * num_patches_img_w
    iter_tot = 0

    img_patches = np.zeros(
        (num_patches_img, patch_size[1], patch_size[0], 3), dtype=img.dtype
    )

    for h in range(num_patches_img_h):
        for w in range(num_patches_img_w):
            start_h = h * stride[1]
            end_h = (h * stride[1]) + patch_size[1]
            start_w = w * stride[0]
            end_w = (w * stride[0]) + patch_size[0]
            if end_h > img_h:
                start_h = img_h - patch_size[1]
                end_h = img_h

            if end_w > img_w:
                start_w = img_w - patch_size[0]
                end_w = img_w

            img_patches[iter_tot, :, :, :] = img[start_h:end_h, start_w:end_w, :]

            iter_tot += 1

    patches = patchextraction.get_patch_extractor(
        input_img=input_img,
        method_name="fixedwindow",
        patch_size=patch_size,
        resolution=0,
        units="level",
        stride=stride,
    )

    assert np.all(img_patches[0] == patches[0])

    img_patches_test = []
    for patch in patches:
        img_patches_test.append(patch)

    img_patches_test = np.array(img_patches_test)

    assert np.all(img_patches == img_patches_test)

    # Test for integer (single) patch_size and stride input
    patches = patchextraction.get_patch_extractor(
        input_img=input_img,
        method_name="fixedwindow",
        patch_size=patch_size[0],
        resolution=0,
        units="level",
        stride=stride[0],
    )

    assert np.all(img_patches[0] == patches[0])

    img_patches_test = []
    for patch in patches:
        img_patches_test.append(patch)

    img_patches_test = np.array(img_patches_test)

    assert np.all(img_patches == img_patches_test)