Majority Filter (Spatial Analyst)
Summary
Replaces cells in a raster based on the majority of their contiguous neighboring cells.
Illustration
Usage
The Majority Filter tool must satisfy two criteria before a replacement can occur: the number of neighboring cells of a similar value must be large enough (either by being the majority of, or half of, all the cells), and those cells must be contiguous about the center of the filter kernel. The second criteria concerning the spatial connectivity of the cells minimizes the corruption of cellular spatial patterns.
-
The use of FOUR for the number of neighbors will retain the corners of rectangular regions. The use of EIGHT will smooth the corners of rectangular regions.
-
Contiguous is defined as sharing an edge for a kernel of EIGHT and sharing a corner for a kernel of FOUR.
-
If the replacement threshold HALF is specified and two values occur as equal halves, a replacement will not occur if the value of the processing cell is the same as one of the halves. HALF allows more extensive filtering than MAJORITY.
-
While the contiguity criterion is the same for edge and corner raster cells, they obey different MAJORITY and HALF rules. Using a kernel of FOUR, an edge or corner cell always requires two matching neighbors before replacement will occur. With a kernel of EIGHT, a corner cell must have all neighbors of the same value before it is changed, while an edge cell requires three contiguous neighbors, including one along the edge, before any change will occur.
-
The output raster will be stabilized (will no longer change) after a few runs of Majority Filter.
Syntax
Parameter | Explanation | Data Type |
in_raster |
The input raster to be filtered based on the the majority of contiguous neighboring cells. It must be of integer type. | Raster Layer |
number_neighbors (Optional) |
Determines the number of neighboring cells to use in the kernel of the filter.
| String |
majority_definition (Optional) |
Specifies the number of contiguous (spatially connected) cells that must be of the same value before a replacement will occur.
| String |
Return Value
Name | Explanation | Data Type |
out_raster |
The output filtered raster. | Raster |
Code Sample
This example filters the input raster using all eight neighbors, with the greater smoothing effect by requiring half of them to have the same value for replacement.
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outMajFilt = MajorityFilter("land", "EIGHT", "HALF")
outMajFilt.save("c:/sapyexamples/output/outmajfilt")
This example filters the input raster using all eight neighbors, with the greater smoothing effect by requiring half of them to have the same value for replacement.
# Name: MajorityFilter_Ex_02.py
# Description: Replaces cells in a raster based on the
# majority of their contiguous neighboring cells.
# Requirements: Spatial Analyst Extension
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
# Set environment settings
env.workspace = "C:/sapyexamples/data"
# Set local variables
inRaster = "land"
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute MajorityFilter
outMajFilt = MajorityFilter(inRaster, "EIGHT", "HALF")
# Save the output
outMajFilt.save("c:/sapyexamples/output/majfilter")