4

I'm trying to classify 10cm-resolution images with the RF-package.

I do this to separate some species in the forest, seen on my images.

While I was searching for an answer, I found the following code, bit I don't understand why the images have to be transformed into point-shapes, so I'm looking for a way to perform a RF-classification just with Raster (*tif, *png).

The code I found (made by Mr. Evans):


    require(sp)
    require(rgdal)
    require(raster)
    require(randomForest)

# CREATE LIST OF RASTERS
    rlist=list.files(getwd(), pattern="img$", full.names=TRUE) 

# CREATE RASTER STACK
    xvars <- stack(rlist)      

# READ POINT SHAPEFILE TRAINING DATA
    sdata <- shapefile("inshape.shp")

# ASSIGN RASTER VALUES TO TRAINING DATA
    v <- data.frame(extract(xvars, sdata))

# RUN RF MODEL
## assumeing that sdata has a variable called 'train' with e.g. 0 and 1 values 
    rf.mdl <- randomForest(x=v, y=as.factor(sdata$train))
## or for regression:
    rf2 <- randomForest(x=v, y=sdata$train)        

# CHECK ERROR CONVERGENCE
    plot(rf.mdl)

# PLOT mean decrease in accuracy VARIABLE IMPORTANCE
    varImpPlot(rf.mdl, type=1)

# PREDICT MODEL
    predict(xvars, rf.mdl, filename="RfClassPred.img", type="response", 
        index=1, na.rm=TRUE, progress="window", overwrite=TRUE)

My Questions:

  1. At "Read Point Shapefile Training Data", why can't I use just raster? I don't get the "jump" to point-shape

  2. In my understanding, I need to create a raster-stack and some training-areas, which are imported as raster's as well. Then I perform rf-classification and plot the results.

Am I right?

closed as too broad by PolyGeo May 2 '17 at 21:51

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What do you want to achieve with your classification? – Curlew Aug 17 '14 at 18:44
  • I want to find areas of 2 tree species(homogeneous areas), I found in person. I marked them with a GPS and made training-areas out of it. Now i want to classify this image with RF. – steveomb Aug 17 '14 at 18:49
3

Your script is configured to take a point shapefile of training data and use that to train a Random Forest classifier. The screenshot shows the form the shapefile attribute table needs to take in order to be used as a training set.

enter image description here

The important fields here are 1) the XY coords, 2) the pixel values for each band at that XY coordinate, and 3) the class of vegetation (e.g. juniper, grass, etc). You can see that I also added an NDVI band into the raster stack, which can increase your classifier performance.

The following script is a simplified version of the one you posted, where I did all data manipulation in a GIS. The general workflow to produce the training data:

  1. Generate shapefile from your GPS data and make sure to calculate the XY coords in the attribute table in addition to the class (e.g. juniper).
  2. Sample all of the bands in the imagery and write to the shapefile attributes. You can accomplish this in ArcGIS using the Sample tool. A word of caution here: ArcGIS has, in the past, had bugs with the Sample tool. R's extract function in the raster package is considered a better alternative.
  3. Write attributes to a .CSV file.
  4. Read .CSV file into R and use as training data set for the RF classifier.

Of course, all of the principles of classifying remotely sensed data apply here: sample size matters, training data should be generated relatively equally among classes, sampling plots should be distributed throughout scene, high accuracy GPS devices are best...


require(sp)
require(rgdal)
require(raster)
require(randomForest)

# Set the working directory
setwd("C:/path/to/folder/containing/imagery")

# CREATE LIST OF RASTERS
rlist=list.files(getwd(), pattern="img$", full.names=TRUE) 

# CREATE RASTER STACK
rasters = stack(rlist)

intable = read.csv("C:/path/to/comma/separated/value/file.txt")

myrf = randomForest(factor(class) ~ band1 + band2 + band3 + band4 + ndvi, data = intable, ntree = 2500)

predict(rasters, myrf, filename="outFileName.img", type="response",
        index=1, na.rm=TRUE, progress="window", overwrite=TRUE) 
  • but how can points with 2 coordinates be training-areas? btw: good explanation – steveomb Aug 17 '14 at 20:08
  • Could you please elaborate a bit @user2999399? – Aaron Aug 17 '14 at 20:10
  • 1
    usually like in maximum-like.-classification, the classifier needs training-areas. these areas are small triangles or circles which are made to represent the class. when we now use points( just x and y coord.) how is the classifier able to see the pixel classes? does it use a small area around the points? – steveomb Aug 17 '14 at 20:15
  • 1
    One nice thing in the raster package is you can now assign names to the bands in a raster stack object that will be honored in the predict funciton. This means that if the band names do not match your training data names you can just assign the correct names to the raster object. – Jeffrey Evans May 28 '15 at 21:05
  • 1
    The idea behind using areas rather than points to train a classification is to control sample variance. This can be both a good or bad thing depending on the model and the sample. Using the R extract function you could define a focal window and a statistical function (eg., mean) to smooth the variation which, would functionally emulate region grow. – Jeffrey Evans May 28 '15 at 21:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.