Unsupervised classification of Google maps imagery in R

Research · September 2015with206 Reads
DOI: 10.13140/RG.2.1.2092.8484
2015-09-13 T 20:21:13 UTC
Abstract
Unsupervised classification of Google maps imagery
9/13/2015 Unsupervised Google Maps image classification | metvurst
https://metvurst.wordpress.com/2015/05/19/unsupervised-google-maps-image-classification-16/ 1/6
UnsupervisedGoogleMapsimageclassification
PostedonMay19,2015
This is a guest post by Florian Detsch
Prerequisites
Required packages
First, we need to (install and) load some packages required for data processing and visualization. The below code
is mainly based on the Rsenal package, which is a steadily developing, unofficial R library maintained by the
Environmental Informatics working group at Philipps-Universität Marburg, Germany. It is hosted on GitHub and
features a couple of functions to prepare true-color (satellite) imagery for unsupervised image classification.
Focal matrices
Google Maps imagery
Rsenal features a built-in dataset (data(gmap_hel)) that shall serve as a basis for our unsupervised classification
approach. The image is originated from Google Maps and has been downloaded via dismo::gmap. We previously
employed OpenStreetMap::openmap to retrieve BING satellite images of the area. However, massive cloud obscuration
prevented any further processing. As seen from the Google Maps image shown below, the land surface is
dominated by small to medium-sized shrubs (medium brown) with smaller proportions of tall bushes (dark
brown) and bare soil (light brown). Also included are shadows (dark brown to black), which are typically located
next to tall vegetation.
1
2
3
4
5
# library(devtools)
# install_github("environmentalinformatics-marburg/Rsenal")
lib <- c("Rsenal", "cluster", "rasterVis", "RColorBrewer")
jnk <- sapply(lib, function(x) library(x, character.only = TRUE))
1
2
3
4
5
6
7
8
9
10
11
## 3-by-3 focal matrix (incl. center)
mat_w3by3 <- matrix(c(1, 1, 1,
1, 1, 1,
1, 1, 1), nc = 3)
## 5-by-5 focal matrix (excl. center)
mat_w5by5 <- matrix(c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1), nc = 5)
1
2
data(gmap_hel, package = "Rsenal")
plotRGB(gmap_hel)
9/13/2015 Unsupervised Google Maps image classification | metvurst
https://metvurst.wordpress.com/2015/05/19/unsupervised-google-maps-image-classification-16/ 2/6
Additional input layers
To gather further information on the structural properties of the land surface, a number of artificial layers is
calculated from the red, green and blue input bands including
focal means and standard deviations,
a visible vegetation index and
a shadow mask.
5-by-5 focal mean per band
5-by-5 focal standard deviation per band
Visible vegetation index
In addition to focal means and standard deviations, we calculate a so-called visible vegetation index (VVI), thus
taking advantage of the spectral properties of vegetation in the visible spectrum of light to distinguish between
vegetated and non-vegetated surfaces. The VVI is included in Rsenal (vvi) and mainly originates from the red and
green input bands.
1
2
3
4
gmap_hel_fcmu <- lapply(1:nlayers(gmap_hel), function(i) {
focal(gmap_hel[[i]], w = mat_w5by5, fun = mean, na.rm = TRUE, pad = TRUE)
})
gmap_hel_fcmu <- stack(gmap_hel_fcmu)
1
2
3
4
gmap_hel_fcsd <- lapply(1:nlayers(gmap_hel), function(i) {
focal(gmap_hel[[i]], w = mat_w5by5, fun = sd, na.rm = TRUE, pad = TRUE)
})
gmap_hel_fcsd <- stack(gmap_hel_fcsd)
1gmap_hel_vvi <- vvi(gmap_hel)
9/13/2015 Unsupervised Google Maps image classification | metvurst
https://metvurst.wordpress.com/2015/05/19/unsupervised-google-maps-image-classification-16/ 3/6
Shadow mask
We finally create a shadow mask to distinguish between shadow and non-shadow pixels during post-classification
image processing. The algorithm is based on the YCbCr color space and is applied to each of the 3 visible bands in
a slightly modified form. For further details, the reader is kindly referred to the original article by Deb and Suny
(2014). Briefly, the algorithm incorporates a transformation step of the initial RGB raster stack to the YCbCr color
space followed by an iterative threshold calculation to distinguish between shadow and non-shadow pixels. Both
the color space transformation (rgb2YCbCr) and the subsequent shadow mask algorithm (rgbShadowMask) are included
in Rsenal. To get rid of noise (i.e. isolated shadow pixels), we additionally apply a 3-by-3 modal value filter after
the actual shadow detection algorithm.
1
2
3
4
5
6
## shadow detection
gmap_hel_shw <- rgbShadowMask(gmap_hel)
## modal filter
gmap_hel_shw <- focal(gmap_hel_shw, w = mat_w3by3,
fun = modal, na.rm = TRUE, pad = TRUE)
9/13/2015 Unsupervised Google Maps image classification | metvurst
https://metvurst.wordpress.com/2015/05/19/unsupervised-google-maps-image-classification-16/ 4/6
Image classification via kmeans()
The unsupervised image classification is finally realized via kmeans clustering following a nice tutorial by Devries,
Verbesselt and Dutrieux (2015). We focus on separating the 3 major land-cover types depicted above, namely
bare soil (class 'A'),
small to medium-sized vegetation (class 'B') and
tall vegetation (class 'C').
After inserting the classification results into a separate raster template (rst_tmp), an additional class for shadow
(tagged '0') is created through multiplication with the initially created shadow mask.
The clusters are initialized randomly, and hence, each land-cover type will be assigned a different ID when
running the code repeatedly (which renders automated image creation impossible). Consequently, visual
inspection of rst_tmp is required to assign classes 'A', 'B' and 'C' to the respective feature IDs of the previously
ratified raster. In our case, bare soil is represented by '1', small vegetation by '3', and tall vegetation by '2'. Note
that shadow will always be associated with '0' due to multiplication.
1
2
3
4
5
6
7
8
## assemble relevant raster data
gmap_hel_all <- stack(gmap_hel, gmap_hel_fcmu, gmap_hel_fcsd, gmap_hel_vvi)
## convert to matrix
mat_hel_all <- as.matrix(gmap_hel_all)
## k-means clustering with 3 target groups
kmn_hel_all <- kmeans(mat_hel_all, centers = 3, iter.max = 100, nstart = 10)
1
2
3
4
5
6
7
8
## raster template
rst_tmp <- gmap_hel[[1]]
## insert values
rst_tmp[] <- kmn_hel_all$cluster
## apply shadow mask
rst_tmp <- rst_tmp * gmap_hel_shw
1rat_tmp <- ratify(rst_tmp)
9/13/2015 Unsupervised Google Maps image classification | metvurst
https://metvurst.wordpress.com/2015/05/19/unsupervised-google-maps-image-classification-16/ 5/6
Visualization
Finally, the classified raster can nicely be visualized using the 'YlGn' color palette from COLORBREWER 2.0 along
with levelplot from the rasterVis package.
Share this:
Press This Twitter 1Facebook 7More
ThisentrywaspostedinR.Bookmarkthepermalink.
2
3
4
5
rat <- rat_tmp@data@attributes[[1]]
rat$Class <- c("S", "C", "A", "B")
rat <- rat[order(rat$Class), , ]
levels(rat_tmp) <- rat
1
2
3
4
ylgn <- brewer.pal(3, "YlGn")
names(ylgn) <- c("A", "B", "C")
ylgnbl <- c(ylgn, c("S" = "black"))
levelplot(rat_tmp, col.regions = ylgnbl)
About these ads
Occasionally, some of your visitors may see an advertisement here.
Tell me more | Dismiss this message
 
Reblog Like
Bethefirsttolikethis.
 
Related
Introducing Rsenal - magic R functions for
things various
reading raster data using library(parallel) Reot: Empirical Orthogonal Teleconnections
in R
In "R"
In "R"
In "climatology"
  • [Show abstract] [Hide abstract] ABSTRACT: Background and aimsTropical alpine ecosystems are identified as the most vulnerable to global environmental change, yet despite their sensitivity they are among the least studied ecosystems in the world. Despite its important role in constraining potential changes to the carbon balance, soil nitrogen (N) turnover and plant availability in high latitude and high altitude ecosystems is still poorly understood. Methods Here we present a first time study on a tropical alpine Helichrysum ecosystem at Mt. Kilimanjaro, Tanzania, which lies at an altitude of 3880 m. Vegetation composition is characterized and major gross N turnover rates are investigated using the 15N pool dilution method for three different vegetation cover types. In addition greenhouse gas exchange (CO2, N2O and CH4) was manually measured using static chambers. ResultsGross N turnover rates and soil CO2 and N2O emissions were generally lower than values reported for temperate ecosystems, but similar to tundra ecosystems. Gross N mineralization, NH4+ immobilization rates, and CO2 emissions were significantly higher on densely vegetated plots than on sparsely vegetated plots. Relative soil N retention was high and increased with vegetation cover, which suggests high competition for available soil N between microbes and plants. Due to high percolation rates, irrigation/rainfall has no impact on N turnover rates and greenhouse gas (GHG) emissions. While soil N2O fluxes were below the detection limit at all plots, soil respiration rates and CH4 uptake rates were higher at the more densely vegetated plots. Only soil respiration rates followed the pronounced diurnal course of air and soil temperature. Conclusion Overall, our data show a tight N cycle dominated by closely coupled ammonification-NH4+-immobilization, which is little prone to N losses. Warming could enhance vegetation cover and thus N turnover; however, only narrower C:N ratios due to atmospheric nitrogen deposition may open the N cycle of Helichrysum ecosystems.
    Article · Aug 2016
Data
November 2014
    The product provides information on the percentage coverage of selected land cover classes in the vicinity of research plots belonging to the DFG Kilimanjaro research group. The coverage has been extracted from a Terra-ASTER based land cover classification. The data set is based on the work of sub-project 1 (Climate Dynamics of the Kilimanjaro Region and Remote Sensing) as part of the DFG... [Show full abstract]
    Technical Report
    January 2016
      Description We provide a set of functions to retrieve information about GIMMS NDVI3g files currently available online; download and rearrange the bimonthly datasets according to creation time; import downloaded files from native binary (ENVI) format directly into R based on the widely applied 'raster' package; extract accompanying quality flags and perform quality control; calculate monthly... [Show full abstract]
      Article
      September 2017 · Environmental Monitoring and Assessment · Impact Factor: 1.68
        Future climate characteristics of the southern Kilimanjaro region, Tanzania, are mainly determined by local land-use and global climate change. Reinforcing increasing dryness throughout the twentieth century, ongoing land transformation processes emphasize the need for a proper understanding of the regional-scale water budget and possible implications on related ecosystem functioning and... [Show full abstract]
        Technical Report
        January 2016
          Description We provide a set of functions to retrieve information about GIMMS NDVI3g files currently available online; download and rearrange the bimonthly datasets according to creation time; import downloaded files from native binary (ENVI) format directly into R based on the widely applied 'raster' package; extract accompanying quality flags and perform quality control; calculate monthly... [Show full abstract]
          Discover more