Learn R Programming

deforestable (version 3.1.2)

classify: Classify parts of images as forest / non-forest

Description

Generic function classify dispatches methods according to the class of object Model. A chosen method takes raster object data and classifies parts of it as 1- forest or 0- non-forest.

Usage

classify(Model, ...)

# S3 method for ForestTrainParam classify(Model, data, n_pts, parallel = FALSE, progress = "text", ...)

# S3 method for ForestTrainNonParam classify(Model, data, n_pts, parallel = FALSE, progress = "text", ...)

Value

a black-and-white image of the terrain data where white represents forest and black is for non-forest.

Arguments

Model

trained model, e.g. by train

...

additional parameters passed to methods

data

raster object. read_data_raster

n_pts

size of sub-frames into which data is split

parallel

Boolean. Whether to use parallel setup

progress

progress bar. Works only when parallel=FALSE. Could be set to 'text' or 'none'

Methods (by class)

  • classify(ForestTrainParam): Method for the class ForestTrainParam

  • classify(ForestTrainNonParam): Method for the class ForestTrainNonParam

Details

Both classify.ForestTrainParam and classify.ForestTrainNonParam use parameter n_pts to split images into square sub-frames of the size n_pts. Those sub-frames are classified independently and all pixels from a sub-frame are tagged according to its classification result. When the image contained by data is of dimensions that are not divisible by n_pts, it is truncated from the right and the bottom to to make the largest divisible one. Thus, the result of classification can be of a different size than the original image.

Examples

Run this code
library(deforestable)
n_pts <- 20

# Choosing folders with training data
Forestdir <- system.file('extdata/Forest/', package = "deforestable")
Nonforestdir <- system.file('extdata/Non-forest/', package = "deforestable")

#### Read the target image ####
tg_dir <- system.file('extdata/', package = "deforestable")
test_image <- read_data_raster('smpl_1.jpeg', dir = tg_dir)


# Simple training of the non-parametric model
Model_nonP_tr <- train(model='fr_Non-Param', Forestdir=Forestdir, Nonforestdir=Nonforestdir,
                       train_method='train', parallel=FALSE)

res <- classify(data=test_image, Model=Model_nonP_tr,
                n_pts=n_pts, parallel=FALSE, progress = 'text')

tmp_d <- tempdir(); tmp_d
jpeg::writeJPEG(image=res, target=paste(tmp_d,'Model_nonP_tr.jpeg', sep='/'))

Run the code above in your browser using DataLab