10

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?

  • You could convert the raster to a vector and use the vector clustering tools. – csk May 4 '18 at 16:58
  • That´s an option, but I think it´s a lot easier to do the the analysis in raster format if possible. Conversion to vector would probably generate polygons where much detail get lost in the union of rastercells, or way too many points too handle (Big data set). – Spiran May 4 '18 at 20:41
  • 3
    I know this isn't a QGIS solution but could you use python or R to come up with a neighborhood function so that you create a new raster where it looks at each cell and if >70% of cells in the neighborhood belong to a class then that cell gets reclassified to that class? – Liam G May 5 '18 at 10:00
  • That could work for sure, thanks! I’m new to Python but its worth a try. – Spiran May 5 '18 at 10:31
5
+50

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

  • This was actually pretty much how I solved it. Basically by using the grass neigborhood analysis- tool together with some calculations on the side. Forgot to post solution though, big Thanks for your input radouxju. – Spiran Feb 6 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.