def get_patches_from_sample(sample, patch_size, step, keep_foreground_only: bool = True, keep_labels: bool = True): slices = SliceBuilder(sample.x.shape, patch_size=patch_size, step=step).build_slices() patches = list() for slice in slices: if keep_labels: center_coordinate = CenterCoordinate(sample.x[tuple(slice)], sample.y[tuple(slice)]) patches.append(Patch(slice, 0, center_coordinate)) else: patches.append(Patch(slice, 0, None)) if keep_foreground_only: return np.array( list( filter(lambda patch: patch.center_coordinate.is_foreground, patches))) else: return patches
def get_filtered_patches(image, label, patch_size, step): slices = SliceBuilder(image.shape, patch_size=patch_size, step=step).build_slices() patches = list() for slice in slices: center_coordinate = CenterCoordinate(image[tuple(slice)], label[tuple(slice)]) patches.append(Patch(image[tuple(slice)], 0, center_coordinate)) return list(filter(lambda patch: patch.center_coordinate.is_foreground, patches))