Last active
July 6, 2021 03:02
-
-
Save franzalex/a95e227cab9b146a6092 to your computer and use it in GitHub Desktop.
Random Forest image classification in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Random Forest image classification | |
# Adapted from http://gis.stackexchange.com/a/57786/12899 | |
#' Performs a random forest classification of the specified raster. | |
#' | |
#' @param rasterPath The full path to the raster file to be classified. | |
#' @param samplesPath The full path to the .SHP file of the training samples. | |
#' @param samplesCol The column in the training samples shape file that contains the class names. | |
#' @param classifiedPath The full path to the output classified image. | |
#' | |
#' @author Franz Alex Gaisie-Essilfie | |
randomForest.classify <- function(rasterPath, samplesPath, | |
samplesCol="CLASS", classifiedPath=NA){ | |
# Install the required packages that aren't already installed | |
toInstall <- setdiff(c("randomForest", "sp", "rgdal", "raster", "tcltk"), | |
rownames(installed.packages())) | |
if (length(toInstall)) install.packages(toInstall) | |
# Add required libraries | |
require(randomForest) | |
require(sp) | |
require(rgdal) | |
require(raster) | |
# Ensure we have a valid path to the training samples | |
if (is.na(samplesPath) || !file.exists(samplesPath)){ | |
shapeFileFilter <- matrix(c("All Files", "*.*", "Shape Files", "*.shp"), ncol=2, byrow=T) | |
samplesPath <- tcltk::tk_choose.files(caption="Select training samples shape file", | |
filters=shapeFileFilter, multi=F) | |
} | |
if (length(samplesPath) == 0 || !file.exists(samplesPath)) | |
stop("Could not locate training samples.") | |
# Load the training samples | |
samples <- readOGR(dirname(samplesPath), | |
tools::file_path_sans_ext(basename(samplesPath))) | |
# Ensure we have a valid path to the unclassified image | |
if (is.na(rasterPath) || !file.exists(rasterPath)){ | |
rasterFilter <- matrix(c("All Files", "*.*", | |
"TIFF Image", "*.tif", | |
"ERDAS IMAGINE", "*.img"), ncol=2, byrow=T) | |
rasterPath <- tcltk::tk_choose.files(caption="Select unclassified image", | |
filters=rasterFilter, multi=F) | |
} | |
if (length(rasterPath) == 0 || !file.exists(rasterPath)) | |
stop("Could not locate raster file for classification.") | |
# Read the image to be classified | |
multispectral <- stack(rasterPath) | |
# Use sample points to extract raster signatures | |
samples@data <- data.frame(samples@data, extract(multispectral, samples)) | |
# Create RandomForest model | |
rf.mdl <- randomForest(x=samples@data[, names(multispectral)], | |
y=samples@data[, samplesCol], | |
ntree=501, proximity=T, importancce=T) | |
# Ensure we have a valid path to the classified image | |
if (is.na(classifiedPath) || file.exists(classifiedPath)){ | |
rasterFilter <- paste('{"All Files" {"*.*"}}', | |
'{"TIFF Image" {"*.tif"}} ', | |
'{"ERDAS IMAGINE" {"*.img"}}', | |
sep=" ") | |
default <- file.path(dirname(rasterPath), | |
paste(tools::file_path_sans_ext(basename(rasterPath)), | |
"classified", tools::file_ext(rasterPath), sep=".")) | |
classifiedPath <- tcltk::tkgetSaveFile(title="Save classified image as", | |
initialfile=default, | |
filetypes=rasterFilter) | |
if (nchar(classifiedPath <- as.character(classifiedPath)) == 0) | |
stop("No raster file selected for saving classified image.") | |
} | |
# Predict classified raster | |
predict(multispectral, rf.mdl, filename=classifiedPath, type="response", | |
na.rm=T, overwrite=T, progress="text", datatype="INT2U") | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setwd("C:/data/") # set the working directory to the directory where the data is stored | |
randomForest.classify("multispectral.img", # file name of the unclassified image | |
"trainingSamples.shp", # point shape file of the locations of the training samples | |
"Class_Name", # the column in the training samples shape file which contain the class names | |
"classifiedImage.img" # file name of the classified image | |
) |
i'm sorry, i don't undestand ... classifiedPath... where i find it? because, you use that only in the ultimate script... and if i use that, R tell me that don't find classifiedPath... i need to know what is it that...
The classifiedimage
parameter is the path to the file that the output should be saved to. You can specify just the file name or the full path (including drive and parent directory). If you specify a relative path (i.e. just the file name), R will save the file in the current working directory. Specifying a relative path (i.e. drive, directory, file name), then R will save the file to the the path you specified.
I hope I clarified the issue for you.
You can do that by modifying lines 58 to 60 (
https://gist.github.com/franzalex/a95e227cab9b146a6092#file-randomforest-sample-r-L58-L60)
as you require.
For example, to set up a different number of trees, change *ntree=501* on
line 60 to the value you desire.
Hope this helps.
…On Wed, 7 Apr 2021, 14:48 githubArian, ***@***.***> wrote:
***@***.***Arian* commented on this gist.
------------------------------
Sorry, but where do I change the randomForest parameters like the number
of trees, for example?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/a95e227cab9b146a6092#gistcomment-3696773>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABZVXAUY5GLKZ7O53XVBTNDTHRWCJANCNFSM4M7T5UJQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ensure we have a valid path to the classified image
if (is.na(classifiedPath) || file.exists(classifiedPath)){
rasterFilter <- paste('{"All Files" {"."}}',
'{"TIFF Image" {".tif"}} ',
'{"ERDAS IMAGINE" {".img"}}',
sep=" ")
default <- file.path(dirname(rasterPath),
paste(tools::file_path_sans_ext(basename(rasterPath)),
"classified", tools::file_ext(rasterPath), sep="."))
classifiedPath <- tcltk::tkgetSaveFile(title="Save classified image as",
initialfile=default,
filetypes=rasterFilter)
if (nchar(classifiedPath <- as.character(classifiedPath)) == 0)
stop("No raster file selected for saving classified image.")
}
Predict classified raster
predict(multispectral, rf.mdl, filename=classifiedPath, type="response",
na.rm=T, overwrite=T, progress="text")
}
i'm sorry, i don't undestand ... classifiedPath... where i find it? because, you use that only in the ultimate script... and if i use that, R tell me that don't find classifiedPath... i need to know what is it that...