6

I have a raster of a land classification with about 8 land classes. I want to clean it up a bit by converting any isolated small groups of pixels e.g. 4 pixels of one value surrounded by pixels of another value - change value A to value B.

Is this possible as a tool or plugin in QGIS?

For example, in the image the final result would be solid green to the left, solid red to the right and blue patches at the top and bottom (because they are larger than a predetermined area, say 4 pixels)

enter image description here

3 Answers 3

7

You can give the SAGA processing tool "Majority Filter" from the processing toolbox a try.

Example before:

enter image description here

Example after (default settings used):

enter image description here

7

As answered by @MrXsquared, SAGA handles it efficiently in a much simpler way. Alternatively, another way to achieve this in QGIS version 3.x is:

  1. Make sure GRASS 7 plugin is installed.

  2. From Processing > Toolbox > Search for r.neighbors

  3. In the r.neighbors window, select the raster layer from the dropdown menu.

  4. Set the Neighborhood Operation to mode

  5. Set the Neighborhood Size to the desired filter size (e.g. 4) and click Run

A way to get this working in older QGIS version(v2.x):

  1. Make sure Raster Terrain Analysis plugin is installed.

  2. Open Raster calculater in QGIS to apply a majority filter. In QGIS Go to "Raster" > "Raster Analysis" > "Raster Calculator"or simply "Raster" > "Raster Calculator" (based on your QGIS version) and under expression section to apply a 4x4 Majority Filter to the raster, use the below expression:

    con(majority_filter(rastername, 4, 4), rastername, nodata())

Similarly in ArcGIS you can use "Majority Filter" tool, which is located in the "Spatial Analyst" toolbox which basically does the same thing by specifying the input layer, kernel size, and the output file name.

2
  • Are you sure this is correct? In QGIS, raster calculator is just under Raster (no .. / Raster Analysis) and both majority_filter and con give me syntax errors, and I cannot find either in the docs. Wouldn't be the first time there's super undocumented functionality but just checking...
    – Houska
    Commented Dec 18, 2022 at 12:13
  • Thanks for pointing this out, my knowledge of this comes from QGIS version 2.14.5 (I know it's pretty old, but i primarily use ArcGIS and use QGIS occassionally where ArcGIS fails). My machine has 'Raster Terrain Analysis' plugin installed which is responsible for the majority_filter expression to work correctly in Raster calculator which I realise is no longer available in QGIS 3x. Luckily there are other ways to get this done in 3.x so I have edited my answer to reflect that.
    – Viv
    Commented Dec 18, 2022 at 15:51
5

The gdal_sieve utility https://gdal.org/programs/gdal_sieve.html is made for this purpose.

gdal_sieve.py script removes raster polygons smaller than a provided threshold size (in pixels) and 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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