Main question towards the end. Details follow now:

I have a slope raster

Region:            Bern, Switzerland 
CRS:               EPSG:2056 - CH1903+ / LV95 - Projected
Extent:            2587757,1186900 : 2613583,1212068
Map units:         meters
Cell values:       degrees 
Theoretical range: 0-90
Range:             0-81
Pixel size:        2x2m 
File size:       ~ 500 MB

I ran the file through raster calc with the expression

( "filename" < 2 )

to obtain all cells with a slope of less than 2. I have now a raster with cell values of 0 and 1. Some areas of the raster are pretty homogenous, but some areas are veritable checkerboards where the slope of the original file is just around 2 and now alternates almost from cell to cell between 0 and 1.

portion of the described image in 100% zoom with homogenous and noisy portions

I need to polygonize the raster. So I tried the GDAL function polygonize from the QGIS toolbox, but had to kill the task after running for 25 minutes.

How can I generalize or simplify a noisy binary layer to reduce processing time when polygonizing?

3 Answers 3


Similar to what @WhiteboxDev suggested, another filter type that could be used is a sieve filter. Instead of looking at whether the 0s or 1s "win" for a given region, it looks at the number of neighbours a given pixel has, that matches its starting value, and chains those neighbours together.

For example, the following case:

0 0 0  
1 1 1  
0 0 0  

With a majority filter, this would end up as all 0, while a sieve filter, with threshold of 3 would keep the area as is. The strength of a sieve filter is that you can keep long narrow features, which may be desirable, for some applications - for example a binary classification of flooded areas, where a majority filter could end up removing well defined streams.

The filter can be found in GDAL and a UI for it is implemented in QGIS.

  • Very interesting. Do you have a reference (or a resource with a detailed description) for the Sieve Filter? I would like to include it as a tool in the WhiteboxTools software if I can. It sounds like it would be a useful addition. Oct 28, 2018 at 13:18
  • @WhiteboxDev might this doc page be what you're looking for? Otherwise there's always the possibility to just download the source from gdal homepage. After looking at the documentation, I figure this contribution is answering the question best. I just have to break the sieve filter's arms and legs to convince it to work for me the way I want it to. It's a hard nut to crack.
    – thymaro
    Oct 28, 2018 at 22:39

In addition to the use of a majority filter, there is another alternative approach that I will list below. This approach is more involved than simply filtering the raster and should only be used when it is important that the polygons retain their original shape and you are only concerned with reducing the overall number of polygon features. This approach involves removing the smaller, less significant features that can result when you threshold a continuous raster. Here is the input raster again:

enter image description here

First, you must clump the thresholded raster. Most GIS include a raster clumping tool but it is often called something different (r.Clump in GRASS; region group in ArcGIS; Clump in Whitebox). Clumping assigns a unique identifier value to each contiguous raster feature of uniform value. If the tool has the option to ignore a zero-valued background during clumping, it is best to do so, but if not, you can remove the background 'features' at the end of the analysis with a simple raster multiplication. You also usually have the option to consider or exclude diagonal neighbours during clumping; I would suggest including diagonals.

enter image description here

Now measure the area of each raster group to create an area raster:

enter image description here

Lastly, using the Raster Calculator, threshold the area raster and multiply it by the original thresholded image:

enter image description here

And now you have a version of the original thresholded image with all of the smaller insignificant groups removed.

enter image description here

In most applications, doing this will greatly reduce the overall number of features because the feature-area distribution is very heavily positively skewed (i.e. there are far more small features than large) and this will often greatly reduce the Raster-to-Vector analysis complexity.

  • This looks really useful, but please could you give a bit more detail about creating the area raster? (Ideally in QGIS) I can use 'Unique values report' or 'Zonal statistics', but these only give tables with the areas; could I then convert those to a raster again? There must be a simpler way? Thanks.
    – EcologyTom
    Jun 29, 2021 at 9:20

This application is exactly what a majority filter (a.k.a. a modal filter) is for. The majority filter passes a kernel of a user-defined size over a categorical raster and assigns the cell in the output image the most common value in the input raster neighbourhood. Here is a Boolean raster that was derived by thresholding a slope raster (much like your example):

enter image description here

Here is the result of running a 3x3 majority filter over the same raster:

enter image description here

The degree of polygon generalization is determined by the size of the filter, with larger filters resulting in more generalization. For example, here is the result of a 5x5 majority filter on the original thresholded raster:

enter image description here

And here's a 15x15:

enter image description here

Most GIS contain a majority (sometimes called mode) filter tool. Once you have an appropriate level of generalization, you may perform Raster-to-Vector conversion. Also, note that the majority filter will not only work with Boolean (dichotomous) data, but also any categorical (class) data. Unlike many types of convolution filters, it will preserve the original class values.

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.