Skip to content

Data Processing

Basic data processing functions are provided in the data_processing module. These functions are used to preprocess data before training a model, or to post-process the output of a model.

Generic Functions

plantseg.dataprocessing.functional.dataprocessing.normalize_01(data: np.ndarray) -> np.ndarray

Normalize a numpy array between 0 and 1 and converts it to float32.

Parameters:

  • data (ndarray) –

    Input numpy array

Returns:

  • normalized_data ( ndarray ) –

    Normalized numpy array

Source code in plantseg/dataprocessing/functional/dataprocessing.py
164
165
166
167
168
169
170
171
172
173
174
def normalize_01(data: np.ndarray) -> np.ndarray:
    """
    Normalize a numpy array between 0 and 1 and converts it to float32.

    Args:
        data (np.ndarray): Input numpy array

    Returns:
        normalized_data (np.ndarray): Normalized numpy array
    """
    return (data - np.min(data)) / (np.max(data) - np.min(data) + 1e-12).astype('float32')

plantseg.dataprocessing.functional.dataprocessing.scale_image_to_voxelsize(image: np.ndarray, input_voxel_size: tuple[float, float, float], output_voxel_size: tuple[float, float, float], order: int = 0) -> np.ndarray

Scale an image from a given voxel size to another voxel size.

Parameters:

  • image (ndarray) –

    Input image to scale

  • input_voxel_size (tuple[float, float, float]) –

    Input voxel size

  • output_voxel_size (tuple[float, float, float]) –

    Output voxel size

  • order (int, default: 0 ) –

    Interpolation order, must be 0 for segmentation and 1, 2 for images

Returns:

  • scaled_image ( ndarray ) –

    Scaled image as numpy array

Source code in plantseg/dataprocessing/functional/dataprocessing.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def scale_image_to_voxelsize(
    image: np.ndarray,
    input_voxel_size: tuple[float, float, float],
    output_voxel_size: tuple[float, float, float],
    order: int = 0,
) -> np.ndarray:
    """
    Scale an image from a given voxel size to another voxel size.

    Args:
        image (np.ndarray): Input image to scale
        input_voxel_size (tuple[float, float, float]): Input voxel size
        output_voxel_size (tuple[float, float, float]): Output voxel size
        order (int): Interpolation order, must be 0 for segmentation and 1, 2 for images

    Returns:
        scaled_image (np.ndarray): Scaled image as numpy array
    """
    factor = compute_scaling_factor(input_voxel_size, output_voxel_size)
    return image_rescale(image, factor, order=order)

plantseg.dataprocessing.functional.dataprocessing.image_rescale(image: np.ndarray, factor: tuple[float, float, float], order: int) -> np.ndarray

Scale an image by a given factor in each dimension

Parameters:

  • image (ndarray) –

    Input image to scale

  • factor (tuple[float, float, float]) –

    Scaling factor in each dimension

  • order (int) –

    Interpolation order, must be 0 for segmentation and 1, 2 for images

Returns:

  • scaled_image ( ndarray ) –

    Scaled image as numpy array

Source code in plantseg/dataprocessing/functional/dataprocessing.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def image_rescale(image: np.ndarray, factor: tuple[float, float, float], order: int) -> np.ndarray:
    """
    Scale an image by a given factor in each dimension

    Args:
        image (np.ndarray): Input image to scale
        factor (tuple[float, float, float]): Scaling factor in each dimension
        order (int): Interpolation order, must be 0 for segmentation and 1, 2 for images

    Returns:
        scaled_image (np.ndarray): Scaled image as numpy array
    """
    if np.array_equal(factor, [1.0, 1.0, 1.0]):
        return image
    else:
        return zoom(image, zoom=factor, order=order)

plantseg.dataprocessing.functional.dataprocessing.image_median(image: np.ndarray, radius: int) -> np.ndarray

Apply median smoothing on an image with a given radius.

Parameters:

  • image (ndarray) –

    Input image to apply median smoothing

  • radius (int) –

    Radius of the median filter

Returns:

  • median_image ( ndarray ) –

    Median smoothed image as numpy array

Source code in plantseg/dataprocessing/functional/dataprocessing.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def image_median(image: np.ndarray, radius: int) -> np.ndarray:
    """
    Apply median smoothing on an image with a given radius.

    Args:
        image (np.ndarray): Input image to apply median smoothing
        radius (int): Radius of the median filter

    Returns:
        median_image (np.ndarray): Median smoothed image as numpy array
    """
    if image.shape[0] == 1:
        shape = image.shape
        median_image = median(image[0], disk(radius))
        return median_image.reshape(shape)
    else:
        return median(image, ball(radius))

plantseg.dataprocessing.functional.dataprocessing.image_gaussian_smoothing(image: np.ndarray, sigma: float) -> np.ndarray

Apply gaussian smoothing on an image with a given sigma.

Parameters:

  • image (ndarray) –

    Input image to apply gaussian smoothing

  • sigma (float) –

    Sigma value for gaussian smoothing

Returns:

  • smoothed_image ( ndarray ) –

    Gaussian smoothed image as numpy array

Source code in plantseg/dataprocessing/functional/dataprocessing.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def image_gaussian_smoothing(image: np.ndarray, sigma: float) -> np.ndarray:
    """
    Apply gaussian smoothing on an image with a given sigma.

    Args:
        image (np.ndarray): Input image to apply gaussian smoothing
        sigma (float): Sigma value for gaussian smoothing

    Returns:
        smoothed_image (np.ndarray): Gaussian smoothed image as numpy array
    """
    image = image.astype('float32')
    max_sigma = (np.array(image.shape) - 1) / 3
    sigma_array = np.minimum(max_sigma, np.ones(max_sigma.ndim) * sigma)
    return gaussianSmoothing(image, sigma_array)

plantseg.dataprocessing.functional.dataprocessing.image_crop(image: np.ndarray, crop_str: str) -> np.ndarray

Crop an image from a crop string like [:, 10:30:, 10:20]

Parameters:

  • image (ndarray) –

    Input image to crop

  • crop_str (str) –

    Crop string

Returns:

  • cropped_image ( ndarray ) –

    Cropped image as numpy array

Source code in plantseg/dataprocessing/functional/dataprocessing.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def image_crop(image: np.ndarray, crop_str: str) -> np.ndarray:
    """
    Crop an image from a crop string like [:, 10:30:, 10:20]

    Args:
        image (np.ndarray): Input image to crop
        crop_str (str): Crop string

    Returns:
        cropped_image (np.ndarray): Cropped image as numpy array
    """
    crop_str = crop_str.replace('[', '').replace(']', '')
    slices = tuple(
        (slice(*(int(i) if i else None for i in part.strip().split(':'))) if ':' in part else int(part.strip()))
        for part in crop_str.split(',')
    )
    return image[slices]

Segmentation Functions

plantseg.dataprocessing.functional.labelprocessing.relabel_segmentation(segmentation_image: np.ndarray) -> np.ndarray

Relabel contiguously a segmentation image, non-touching instances with same id will be relabeled differently. To be noted that measure.label is different from ndimage.label.

Parameters:

  • segmentation_image (ndarray) –

    segmentation image to relabel

Returns:

  • new_segmentation_image ( ndarray ) –

    relabeled segmentation image

Source code in plantseg/dataprocessing/functional/labelprocessing.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def relabel_segmentation(segmentation_image: np.ndarray) -> np.ndarray:
    """
    Relabel contiguously a segmentation image, non-touching instances with same id will be relabeled differently.
    To be noted that measure.label is different from ndimage.label.

    Args:
        segmentation_image (np.ndarray): segmentation image to relabel

    Returns:
        new_segmentation_image (np.ndarray): relabeled segmentation image

    """
    return measure.label(segmentation_image)

plantseg.dataprocessing.functional.labelprocessing.set_background_to_value(segmentation_image: np.ndarray, value: int = 0) -> np.ndarray

Set the largest segment (usually this is the background but not always) to a certain value.

Parameters:

  • segmentation_image (ndarray) –

    segmentation image to relabel

  • value (int, default: 0 ) –

    value to set the background to, default is 0

Returns:

  • new_segmentation_image ( ndarray ) –

    segmentation image with background set to value

Source code in plantseg/dataprocessing/functional/labelprocessing.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def set_background_to_value(segmentation_image: np.ndarray, value: int = 0) -> np.ndarray:
    """
    Set the largest segment (usually this is the background but not always) to a certain value.

    Args:
        segmentation_image (np.ndarray): segmentation image to relabel
        value (int): value to set the background to, default is 0

    Returns:
        new_segmentation_image (np.ndarray): segmentation image with background set to value
    """
    segmentation_image += 1
    idx, counts = np.unique(segmentation_image, return_counts=True)
    bg_idx = idx[np.argmax(counts)]
    return np.where(segmentation_image == bg_idx, value, segmentation_image)

Advanced Functions

Documentation in Progress

This page is under development.