[R-sig-Geo] Random forest for forest cover classification

Carlos Alberto Arnillas c@r|o@@|berto@@rn|||@@ @end|ng |rom gm@||@com
Tue Aug 13 17:30:35 CEST 2019


Hello Enoch
I think segmentation is a good idea, it will group pixels by similar
characteristics, creating polygons (don't forget to validate the
generated polygons!).
then you can do classification on those polygons using polygons properties
(including shape indices, but also the values obtained from the colours of
the image) using random forest.

Carlos Alberto

On Tue, Aug 13, 2019 at 8:41 AM Juan Pablo Carranza <carranzajuanp using gmail.com>
wrote:

> Dear Enoch, I strongly recomend to try an segmentation approach instead a
> classification. However, here's some code you can use:
>
> library(rgdal)
> library(raster)
> library(caret)
> library(kerndwd)
> library(sp)
> library(RColorBrewer)
> library(dplyr)
> library(leaflet)
> library(sf)
> img <- brick("******.tiff")
> names(img) <- paste0("B", c(1:*))
> plot(img)
> library(sf)
> sample <- st_read("******gpkg")
> sample  = as(sample, 'Spatial')
> sample$class = as.factor(sample$class) ; responseCol <- "class"
> pixels = data.frame(matrix(vector(), nrow = 0, ncol = length(names(img)) +
> 1))
>
> # A function to overlap de sample polygons on the image.
> for (i in 1:length(unique(sample[[responseCol]]))){
>   category <- unique(sample[[responseCol]])[i]
>   categorymap <- sample[sample[[responseCol]] == category,]
>   dataSet <- extract(img, categorymap)
>   dataSet <- dataSet[!unlist(lapply(dataSet, is.null))]
>   if(is(sample, "SpatialPointsDataFrame")){
>     dataSet <- cbind(dataSet, class = as.numeric(category))
>     pixeles <- rbind(pixeles, dataSet)
>   }
>   if(is(sample, "SpatialPolygonsDataFrame")){
>     dataSet <- lapply(dataSet, function(x){cbind(x, class =
> as.numeric(rep(category, nrow(x))))})
>     df <- do.call("rbind", dataSet)
>     pixels <- rbind(pixeles, df)
>   }
> }
>
> # Define a number of pixels to use in the training of the model. More
> pixels, more accuracy but more computational time.
> n <- 1000
>
> # Take a subsample in order to reduce computational times.
> spixeles <- pixeles[sample(1:nrow(pixeles), n ), ]
>
> # Train model.
> modFit_rf <- train(as.factor(class) ~ ., method = "nnet", data = spixeles)
> modFit_rf
>
> # Predict.
> beginCluster()
> preds_rf <- clusterR(img, raster::predict, args = list(model = modFit_rf))
> endCluster()
>
> # Mapping results
> maxpixels =  1e+06
> m <- leaflet() %>%
>   addProviderTiles('Esri.WorldImagery') %>%
>   addRasterImage(preds_rf , opacity = 0.6) %>%
>   addScaleBar(position = "topright") %>%
>   addMiniMap( position = "bottomleft")
> m
>
> # Export raster
> writeRaster(preds_rf, "estimacion.tif")
>
>
>
> *Juan Pablo Carranza*
> Mgter. en Administración Pública
> Lic. en Economía
>
>
>
>
> El mar., 13 ago. 2019 a las 5:50, Enoch Gyamfi Ampadu (<egampadu using gmail.com
> >)
> escribió:
>
> > Dear List,
> >
> > Please I am now learning R to apply to my analysis. I am doing a forest
> > cover classification using Random forest. So far I have stacked the
> image,
> > done atmospheric correction, and clipped out my study area. I have
> created
> > some training sites of the cover classes in ArcMap and imported into R as
> > well as the clipped image of my study sites. I have been doing try and
> > error with some online codes. I will be glad if you could provide me with
> > some codes to apply.
> >
> > Thank you.
> >
> > Best regards,
> >
> > Enoch
> >
> > --
> > *Enoch Gyamfi - Ampadu*
> >
> > *Geography & Environmental Sciences*
> >
> > *College of Agriculture, Engineering & Science*
> >
> > *University of KwaZulu-Natal, Westville Campus*
> >
> > *Private Bag X54001*
> > *Durban, South Africa **– 4000**.*
> > *Phone: +27 835 828255*
> >
> > *email: egampadu using gmail.com <egampadu using gmail.com>*
> >
> >
> > *skype: enoch.ampadu*
> > *The highest evidence of nobility is self-control*.
> >
> > *A simple act of kindness creates an endless ripple*.
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list