Skip to content

filtering module

Module for applying filters to image.

GaussianFilter(in_dem, sigma=1, out_file=None)

Applies a Gaussian filter to an image.

Parameters:

Name Type Description Default
in_dem str

File path to the input image.

required
sigma int

Standard deviation. Defaults to 1.

1
out_file str

File path to the output image. Defaults to None.

None

Returns:

Type Description

np.array: The numpy array containing the filtered image.

Source code in lidar/filtering.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def GaussianFilter(in_dem, sigma=1, out_file=None):
    """Applies a Gaussian filter to an image.

    Args:
        in_dem (str): File path to the input image.
        sigma (int, optional): Standard deviation. Defaults to 1.
        out_file (str, optional): File path to the output image. Defaults to None.

    Returns:
        np.array: The numpy array containing the filtered image.
    """
    print("Gaussian filtering ...")
    start_time = time.time()
    dem = rd.LoadGDAL(in_dem)
    no_data = dem.no_data
    projection = dem.projection
    geotransform = dem.geotransform

    gau = ndimage.gaussian_filter(dem, sigma=sigma)
    gau = np2rdarray(gau, no_data, projection, geotransform)
    print("Run time: {:.4f} seconds".format(time.time() - start_time))

    if out_file is not None:
        print("Saving dem ...")
        rd.SaveGDAL(out_file, gau)
        return out_file

    return gau

MeanFilter(in_dem, kernel_size=3, out_file=None)

Applies a mean filter to an image.

Parameters:

Name Type Description Default
in_dem str

File path to the input image.

required
kernel_size int

The size of the moving window. Defaults to 3.

3
out_file str

File path to the output image. Defaults to None.

None

Returns:

Type Description

np.array: The numpy array containing the filtered image.

Source code in lidar/filtering.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def MeanFilter(in_dem, kernel_size=3, out_file=None):
    """Applies a mean filter to an image.

    Args:
        in_dem (str): File path to the input image.
        kernel_size (int, optional): The size of the moving window. Defaults to 3.
        out_file (str, optional): File path to the output image. Defaults to None.

    Returns:
        np.array: The numpy array containing the filtered image.
    """
    print("Mean filtering ...")
    start_time = time.time()
    dem = rd.LoadGDAL(in_dem)
    no_data = dem.no_data
    projection = dem.projection
    geotransform = dem.geotransform

    weights = np.full((kernel_size, kernel_size), 1.0 / (kernel_size * kernel_size))
    mean = ndimage.filters.convolve(dem, weights)
    mean = np2rdarray(mean, no_data, projection, geotransform)
    print("Run time: {:.4f} seconds".format(time.time() - start_time))

    if out_file is not None:
        print("Saving dem ...")
        rd.SaveGDAL(out_file, mean)
        return out_file

    return mean

MedianFilter(in_dem, kernel_size=3, out_file=None)

Applies a median filter to an image.

Parameters:

Name Type Description Default
in_dem str

File path to the input image.

required
kernel_size int

The size of the moving window. Defaults to 3.

3
out_file str

File path to the output image. Defaults to None.

None

Returns:

Type Description

np.array: The numpy array containing the filtered image.

Source code in lidar/filtering.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def MedianFilter(in_dem, kernel_size=3, out_file=None):
    """Applies a median filter to an image.

    Args:
        in_dem (str): File path to the input image.
        kernel_size (int, optional): The size of the moving window. Defaults to 3.
        out_file (str, optional): File path to the output image. Defaults to None.

    Returns:
        np.array: The numpy array containing the filtered image.
    """
    print("Median filtering ...")
    start_time = time.time()
    dem = rd.LoadGDAL(in_dem)
    no_data = dem.no_data
    projection = dem.projection
    geotransform = dem.geotransform

    med = ndimage.median_filter(dem, size=kernel_size)
    med = np2rdarray(med, no_data, projection, geotransform)
    print("Run time: {:.4f} seconds".format(time.time() - start_time))

    if out_file is not None:
        print("Saving dem ...")
        rd.SaveGDAL(out_file, med)
        return out_file

    return med

np2rdarray(in_array, no_data, projection, geotransform)

Converts an numpy array to rdarray.

Parameters:

Name Type Description Default
in_array array

The input numpy array.

required
no_data float

The no_data value of the array.

required
projection str

The projection of the image.

required
geotransform str

The geotransform of the image.

required

Returns:

Name Type Description
object

The richDEM array.

Source code in lidar/filtering.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def np2rdarray(in_array, no_data, projection, geotransform):
    """Converts an numpy array to rdarray.

    Args:
        in_array (np.array): The input numpy array.
        no_data (float): The no_data value of the array.
        projection (str): The projection of the image.
        geotransform (str): The geotransform of the image.

    Returns:
        object: The richDEM array.
    """
    out_array = rd.rdarray(in_array, no_data=no_data)
    out_array.projection = projection
    out_array.geotransform = geotransform
    return out_array