QGIS Raster Analysis – Raster Clustering using QGIS

qgisrasterspatial-cluster

I'm looking for a way to convert a classified raster into polygons based on spatial clusters within each class. For the clusters to be considered as valid I need them to consist of a minimum percentage of cells from one of the classes.

For example: An area made up of 70 % (or more) cells of class "1" will be considered as a cluster of class "1" even though the area is mixed up with 30 % cells beloning to other classes. The clustering analysis therefore should be based on the distance between cells of the same class.

Another option could be to base the clustering on a minimum number of cells within a certain class, along with a definition of a maximum search area.

For example: Within a specified area there should be 100 cells of "class 1" for it to be considered a cluster.
 

Most tools related to clustering seem to work only for vectors. I´ve looked at the SAGA-tool Cluster-analysis but it didn't really fit my purpose. Any ideas for solving this or which other tools that may be helpful?

Best Answer

If you prefer to address this issue in "the raster logic", then there are a few filters that you could consider. The best choice will depend on the spatial distribution of your pixels of each class inside your "background" values, but here are two potential solutions :

if your patches that you want to remove are relatively large, then you should use "sieve" (raster > analysis > sieve in QGIS 3.2, which is based on gdal_sieve.py).

The gdal_sieve.py script removes raster polygons smaller than a provided threshold size (in pixels) and replaces replaces them with the pixel value of the largest neighbour polygon. The result can be written back to the existing raster band, or copied into a new file.

If you have something like a "salt and pepper" effect (many isolated pixels of different classes, but just a few pixels per small pathes, then you should use a majority filter (e.g. going to the additionnal tools from GRASS > raster > r.neighbors > select "mode" option ). Note that this filter will (slightly) affect your boundaries.

r.neighbors - Makes each cell category value a function of the category values assigned to the cells around it, and stores new cell values in an output raster map layer

You will find the same filers (Majority filter, sieving classes ) and other ones (morphology) in the SAGA tools (SAGA > raster filter) if you prefer