Filtering and clumping

In this section, and the following one, we move on from the subject of changing raster geometry to the subject of relations between neighboring raster cell values. These relations can be summarized in the form of a new raster using a variety of methods. In this section, we will introduce two such methods: focal filtering and clumping.

Focal filtering involves assigning in each cell of a raster (the focal cell) the result of a function, whose input is the set of values from a neighborhood of cells surrounding the focal one (including itself). The neighborhood size is predetermined (for example, a neighborhood of 3*3 cells is commonly used), and the input raster is scanned in a moving window manner until complete coverage has been reached. There are many appropriate functions that can be implemented in filters for various purposes. For example, using the mean function (also known as a low-pass filter) makes an image look smoother, while using a function that finds the most common value (also known as a majority filter) is useful for reducing noise in a categorical raster.

The focal function is used to apply a focal filter on a RasterLayer object. The three major parameters of this function are as follows:

  • The RasterLayer object to be filtered (x)
  • A matrix defining the neighborhood and the cell weights (w)
  • A function to be applied on the neighborhood (fun)

The w argument should be a matrix defining the neighborhood. It should have odd dimensions (such as a 3*3 or 7*5), since its center defines the focal cell position. The matrix values define the weights that are applied on cell values before these are transferred to the focal function. However, using weights is optional—a matrix where all values are equal to 1 would imply having no weights. Weights of 0 can also be used to create a non-symmetric and/or non-rectangular window. The fun argument should be a function to be applied on the neighborhood values (the default is sum).

For the filtering example, we will return to l_rec, a reclassified NDVI raster (0 for NDVI≤0.2 and 1 for NDVI>0.2) based on the Landsat image obtained on October 4, 2000 (refer to Chapter 4, Working with Rasters, for more information). This raster shows the locations of the more densely vegetated areas (such as planted pine forests); however, the image is quite grained, with 0 values appearing within the otherwise continuous high-vegetation (1) zones.

To increase continuity, we will buffer the NDVI>0.2 zone by converting all cells having at least one immediate 1 neighbor to 1, thus reducing gaps of 0s between or within vegetated areas. This operation can be carried out by applying a focal filter with a 3*3 window and the max function with no weights. With max, each cell gets the maximal value of its 3*3 neighborhood. Thus, a 0 cell either retains its value (if all nine neighbors are 0) or converts to 1 (in case at least one of its neighbors is 1). All 1 cells retain their value, since they have at least one 1 neighbor (the cell itself). Since we do not need weights, we will use a 3*3 matrix consisting of plain 1s for the w argument. This can be created as follows:

> matrix(1, nrow = 3, ncol = 3)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1

Therefore, the complete expression that applies the filter on l_rec is as follows:

> l_rec_focal = focal(l_rec,
+ w = matrix(1, nrow = 3, ncol = 3),
+ fun = max)

The result is assigned to l_rec_focal; comparing it with the original l_rec raster shows the buffering of vegetated areas that took place (refer to the next screenshot).

Our second example of an operation concerning cell neighbors is clumping. In clumping, we are interested in the detection of cell patches that have the same value. For example, you may wish to learn how many patches there are, what their size distribution is, what properties they have with respect to the values of other rasters, and so on. Clumping can be performed with function clump, which returns a new raster where a unique ID is assigned to every connected patch of cells that have the same value in the input raster (except for values of 0 or NA, which are ignored and used as background). The operation is only useful with categorical rasters—in continuous rasters, all (or most) cells usually have unique values. Thus, no patches larger than one cell can be formed.

The clump function accepts a RasterLayer object and returns a raster where each patch of similar-valued cells has a unique ID. Specifying gaps=FALSE will force these IDs to be consecutive integers, starting with 1 and going to n (where n is the total number of patches). For example, let's clump the l_rec_focal regions of 1:

> l_rec_focal_clump = clump(l_rec_focal, gaps = FALSE)

The result l_rec_focal_clump is a raster where 0 regions were converted to NA (and used as background), while all cells that are part of a continuous patch of 1s received a unique ID. Checking out how many unique non-NA values l_rec_focal_clump contains (for example, with the expression max(l_rec_focal_clump[],na.rm=TRUE)) will show that we have 507 separate NDVI>0.2 patches. Had we skipped the filtering step and performed the clumping on l_rec, we would have got 1,258 patches.

Note

The igraph package is required for running clump, so make sure it is installed.

Let's visualize the filtering and clumping results along with the original raster:

> plot(l_rec, main = "Original image")
> plot(l_rec_focal, main = "Filtered")
> plot(l_rec_focal_clump, main = "Clumped")

The following screenshot shows the plots of l_rec, l_rec_focal, and l_rec_focal_clump, respectively from left to right:

Filtering and clumping

We can see that filtering has thickened the NDVI>0.2 areas, while clumping has assigned IDs (going from 1 to 507) in each patch. In the next chapter, we will continue working with l_rec_focal_clump to see how these patches can be converted to polygons and how patches of interest can be isolated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229