I have recently completed a land use classification using over 100 ROIs for 5 different land cover classes and it has developed what looks like a highly specific spectral signature for each class. This has meant that the classification is almost like a series of disconnected coloured squares (see pic). It would be great if, for example, similar pixels surrounding or next to a different pixel could convert it to the same class so as to show more contiguous classes. Any ideas?

Green = forest Purple = sub canopy cultivation blue = degraded forest cyan = monoculture yellow = village

enter image description here

  • 1
    You should take a look at "Majority Filter" from SAGA, in QGis. Commented Jul 6, 2016 at 11:04
  • Thanks very much for your time in making the suggestion. I tried a majority filter and also a sieve analysis. the latter of which I will use for the input of a vectorise conversion. I found the Sieve function much easier to interpret and therefore get the results I wanted. Commented Jul 7, 2016 at 7:34
  • You should write up your findings and post them as an answer to your own question. This will allow you to mark that as the correct answer, which will close the question, instead of leaving it open as it is now. Commented Jul 7, 2016 at 8:39
  • Through "Processing" in QGIS you could vectorize the map and then run v.clean of GRASS GIS: grass.osgeo.org/grass72/manuals/v.clean.html#remove-small-areas
    – markusN
    Commented Oct 25, 2016 at 7:53

3 Answers 3


In QGIS I tried a majority filter (Processing>toolbox>geoalgorithms>filters) and also a sieve analysis (Raster>analysis). I found the Sieve function much easier to interpret and therefore get the results I wanted, especially when using the 4 or 8 connecting pixels function. This reduced a lot of the noise and gave a more interpretable sense of majority land covers within each area. I will use this output to create a generalised vector land cover map.


I've stumbled across this, with my specific situation being anomalous singleton pixels in my constructed classification layer. Based on pointers in the (older) answers and comments here, plus How to apply a Majority filter in GRASS GIS?, here's what worked for me with a combo of GRASS and Raster calculator in QGIS.

Starting point is my (noisy) classification layer (a small subset). The 2nd image highlights the anomalous singletons in black.

aci_class_combined singletons in black

A simple run of the QGIS/GDAL sieve algorithm, with 8-connectedness and threshold of 2, seems promising, but does some strange remappings, e.g. the pixel circled in red. The problem is the GDAL sieve maps removed pixels to the biggest neighbouring polygon, which may not be the most frequent neighbour. Here it's one pixel, but in a land cover/vegetation classification this happens too often.

GDAL sieve and anomaly

A majority filter gives a better remapping of the singletons, but unfortunately also remaps pixels that were on actual polygon boundaries, 2 examples again circled in red:

r.neighbors mode

The solution is then to combine the original raster plus the majority-filtered one on the singletons only. Here is the output:

Final output

The steps to do this are as follows, the original raster called aci_class_combined:

  1. Create aci_class_interspersion using grassr.neighbors method=interspersion, size=3. This will have value 101 for pixels which are singletons (and down to 1 for pixels all of whose neighbours are the same).

  2. Create aci_class_neighborsmode using grass r.neighbors method=mode, size=3. This majority filter smooths out the raster by replacing each cell with the mode of the cells around it.

  3. Finally, combine to get the desired output by creating a raster in the Raster Calculator with expression


If using grass natively rather than from withing QGIS, one could use the the r.neighbors method=interspersion output as a location mask in the method=mode invocation directly, but from QGIS it would require mucking around with conditionals and no-data values and the above was faster than figuring it out.)

Editing to add: With some further tinkering, the simple majority filter using grass r.neighbors can be overly aggressive, especially for singletons that end up having multiple neighbouring polygons with 2-3 adjacent pixels. For that step, using the SAGA Raster filter / Majority filter with a threshold of ~50% (4+ neighbours to change a pixel) is a more conservative option. Still good to incorporate its changes only on singletons as above.


i think you can use RStudio to reclassify multiple class to the class you wish and the syntax is very simple.

step 1. In (ArcGIS or QGIS) attribute table check the similar classes and write it in your note book.

step 2. Install R and RStudio and open it in your desktop

step 3. copy and paste the below script and change the required field and change the classes according to your need,. ps. in (r= 1) 1 is equal to class which need to be reclassified # 2 is equal to class which is the destination.

setwd("**set the working folder path here**")
r<- raster("***give your raster file path iside the inverted comma here***")

r[r=1] <- 2  

r[r=3] <- 2 

r[r=3] <- 4

r[r=5] <- 6

r[r=7] <- 8

r[r=9] <- 10  

writeRaster(r, filename="ReclassifiedLandcoverfile.tif", format="GTiff", overwrite=TRUE)
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. The asker makes no mention of using R and RStudio.
    – PolyGeo
    Commented Nov 17, 2017 at 9:36
  • Your code would not work because you already have overwritten value 3. This is why you use something like ifelse besides, this does not actually address the OP's question. Commented Jan 1, 2020 at 4:44

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.