Part 3: Image Filtering

WhiteboxTools offers numerous tools for filtering image data in the spatial domain. Most of these filters can be grouped into some of the more common types based on their functionality:

  1. Low-pass filters: These filters emphasize low-frequency, longer-range signals in the image and de-emphasize high-frequency, short-scale variations. They work to smooth the image and to reduce apparent noise. Examples of common low-pass filters include MeanFilter, GaussianFilter, and MedianFilter.

  2. Edge-preserving low-pass filters: Like other low-pass filters, this class of filters also aims to smooth images by emphasizing longer-range variation in the image. However, these filters also work to preserve the crispness of edges in the original image. Common examples include BilateralFilter, and EdgePreservingMeanFilter.

  3. High-pass filters: The opposite of a low-pass filter, these filters emphasize short-scale variation and de-emphasize longer-range signals. These tools are typically used to sharpen an image. The UnsharpMasking tool is a good example (despite its contrary name).

  4. Band-pass filters: These filters are used to isolate the variation in an image that lies between a lower and upper bound of specified ranges. The DiffOfGaussianFilter is a good example of this type of filter.

  5. Edge-detection filters: These filters are used to isolate the edge features within an image. Common examples include the SobelFilter and RobertsCrossFilter

Readings: Mather and Koch (2011), Computer Processing of Remotely-Sensed Images, pp 203-220

Image Smoothing and Noise Reduction

You may recall from the previous section that PCA is sometimes used to remove noise from multi-spectral image datasets. Low-pass and edge-preserving low-pass filters similarly are used for reducing the occurrence of noise within images, although, unlike PCA, these operations are carried out on a single band (or the individual RGB components). Many satellite images contains substantial speckle, i.e. white noise. Speckle refers to a high-frequency, short-spatial scale variation among neighouring pixels. To enhance the image and to improve its information content, it is necessary to remove this speckle. This is sometimes useful prior to image classification and other mapping applications where it can be good to reduce the within-patch variaibility (e.g. tonal variation, or texture, within agricultural fields) and to maximize the between-patch tonal distictions (e.g. the differences between adjoining agricultural fields).

Apply a 5 × 5 mean filter to the natural_colour_hsi.tif image created in Part 1 using WhiteboxTools' MeanFilter tool. To do so, we'll need to split this RGB composite image apart into its individual components, using the SplitColourComposite and then add them back together after the filtering operation:

from WBT.whitebox_tools import WhiteboxTools


wbt = WhiteboxTools()
wbt.work_dir = "/path/to/data/" # Update this

wbt.verbose = False

print("Break the image apart into its RGB components...")
# This will create three images: split_component_r.tif, split_component_g.tif, split_component_b.tif
# See help documentation for more details
wbt.split_colour_composite(
    i="natural_colour_hsi.tif",
    output="split_component.tif"
)

print("Filtering the component images...")
filter_size = 5

# Mean filter
wbt.mean_filter(
    i="split_component_r.tif",
    output="temp1.tif",
    filterx=filter_size,
    filtery=filter_size
)

wbt.mean_filter(
    i="split_component_g.tif",
    output="temp2.tif",
    filterx=filter_size,
    filtery=filter_size
)

wbt.mean_filter(
    i="split_component_b.tif",
    output="temp3.tif",
    filterx=filter_size,
    filtery=filter_size
)

print("Create a new composite...")
wbt.create_colour_composite(
    red="temp1.tif",
    green="temp2.tif",
    blue="temp3.tif",
    output="nat_clr_5x5mean.tif",
    enhance=False
)

print("All done!")

Once the script has sucessfully run, open the resulting nat_clr_5x5mean.tif image using the data visualization software and compare it to the original natural_colour_hsi.tif image.

3.1. Describe the impact of the mean filter on the image? How does the filter impact the variation of tone (texture) with the larger land-cover patches (e.g. fields)? How does it impact the edges between patches as well as other linear features, such as roads? (5 mark)

One of the key characteristics of all spatial filters used in image processing is the kernel size, i.e. the size of the roving window. Modify the script so that it applies a 7 × 7 mean filter and compare the output to that of the 5 × 5 (be sure to change the output file name when you modify the script or you will overwrite the first filtered image).

3.2. What was the impact of increasing the filter size? (1 mark)

Modify the script again, this time change the filter tool to perform a 7 × 7 MedianFilter (be sure to look at the help documentation description of tool parameters; use sig_digits=0) and a 7 × 7 EdgePreservingMeanFilter (use parameters threshold=40 and filter=7).

3.3. How do the median and edge-preserving mean filters compare, with respect to their ability to smooth patches while preserving edges and linear features, to the earlier 7 × 7 mean filter? (4 marks)

Edge Detection

Spatial convolution filters can be used for many common image processing tasks other than noise reduction. One common task is edge-dection, which is often used during automated mapping operations. Apply a 3 × 3 Sobel edge-detection filter to the natural_colour_hsi.tif image using the following script:

from WBT.whitebox_tools import WhiteboxTools


wbt = WhiteboxTools()
wbt.work_dir = "/path/to/data/" # Update this

wbt.verbose = False

wbt.sobel_filter(
    i="natural_colour_hsi.tif",
    output="sobel.tif",
    variant="3x3",
    clip=1.0
)

Notice, that unlike the previous filters, there is no need to split the input colour composite image apart before applying the Sobel filter. This tool will work well with the RGB composite input image. When the script has successfully completed, display the resulting image using your data visualization software.

Include a screenshot of the Sobel image with your Lab report (1 mark).

3.4. How well does the filter work to highlight edges between adjacent land-use patches and linear features? (2 mark)

Modify the Sobel script to run using the EdgePreservingMeanFilter colour composite image created in the previous lab part as the input image.

3.5. To what extent does the use of a previously filtered image improve the detection of edge and linear features in the image? (2 mark)

Typically, one would threshold the Sobel image (i.e. find all pixels greater than a threshold value) and then apply a line thinning method to further refine the mapped edges. But let's leave that for another day!