Available with Spatial Analyst license.
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.
Using four for the number of neighbors will retain the corners of rectangular regions. Using eight neighbors will smooth the corners of rectangular regions.
With the number of neighbors set to eight, contiguous is defined as sharing an edge. With the number of neighbors set to four, contiguous is defined as sharing a corner.
If the Replacement threshold is set to HALF and two values occur as equal portions, no replacement will occur if the value of the processing cell is the same as one of the halves. The HALF option allows more extensive filtering than the MAJORITY option.
While the contiguity criterion is the same for edge and corner raster cells, they obey different rules for MAJORITY and HALF. When the number of neighbors to be used is four, an edge or corner cell always requires two matching neighbors before replacement will occur. With eight neighbors, 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.
See Analysis environments and Spatial Analyst for additional details on the geoprocessing environments that apply to this tool.
Syntax
MajorityFilter (in_raster, {number_neighbors}, {majority_definition})
Parameter | Explanation | Data Type |
in_raster | The input raster to be filtered based on 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. The output is always of integer type. | Raster |
Code Sample
MajorityFilter example 1 (Python window)
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")
MajorityFilter example 2 (stand-alone script)
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")
Environments
Licensing Information
- ArcGIS for Desktop Basic: Requires Spatial Analyst
- ArcGIS for Desktop Standard: Requires Spatial Analyst
- ArcGIS for Desktop Advanced: Requires Spatial Analyst