Category Archives: raster processing

Decision tree classifier in QGIS using GRASS

Hi there guys!

Maybe you’ve already needed to perform a decision tree classification in some point in your GIS life. You might have used ENVI’s decision tree to do the job, right? But, what about QGIS? Can we achieve the same results using QGIS processing? Definitely yes!!!

Today I’ll gonna show how this can be simple.

Imagine you want to classify this image:

image1

Using this one to help filter the land cover:

image2

On ENVI, this can be done with a decision tree like this one (one of my co-workers made it):

decision_tree

Kinda easy right? But, what about QGIS? Let’s take a look!

In QGIS we have the powerful processing toolbox. Inside it we have the awesome GRASS to use as much as we want. To accomplish this job we will use r.mapcalculator (it provides a GUI frontend to r.mapcalc – https://grass.osgeo.org/grass73/manuals/r.mapcalc.html).

Ok, we can see that the decision tree uses several conditions to discover what pixels it should classify in a specific class. This conditions are translated to GRASS using this:

if(x,a,b)               a if x not zero, b otherwise

Where “x” is a condition to be followed.

So, to classify water (let’s say pixel value 0) to a class with value 5 and classify everything else to 255 we need to make this expression if(x,5,255), where x is the condition (e.g. image==0).

Quite easy, right?

Moving forward, with more levels (like in the decision tree above), we need to use nested if conditions (i.e if conditions inside another if condition).

So, the next step is to open the processing toolbox in QGIS and search for r.mapcalculator to open the following dialog:

r-mapcalulator

We can choose several images (A, B, and so on…). In my case, I’ll choose Raster Layer A and Raster Layer B (the first two images in this article). Inside the formula I’ll insert my nested if conditions taken from my decision tree, as follows:

(if((A==0),6,if((A>=0 && A<=4),if((B==1),3,4),if((A>=3 && A<=6),if((B==2),1,2),if((A>=6 && A<=12),if((B==3),8,7),if((A>=12),if((B==1),10,9),5))))))

With this set, I just need to to click on “Run” and wait for my classified image. By the way, the process is quite fast thanks to GRASS…

As we can see, the results are identical (another point for QGIS and GRASS for the awesome results they deliver together!!!). Below, on the right side we have the QGIS’ result and on the left side we have ENVI’s result (I made a small subset to make it easier to check both results).

What do you guys think? Pretty nice, right?