Majority Filter (Spatial Analyst)

Available with Spatial Analyst license.

Summary

Replaces cells in a raster based on the majority of their contiguous neighboring cells.

Learn more about how Majority Filter works

Illustration

Majority Filter illustration
OutRas = MajorityFilter(InRas1)

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 parameter 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 the Majority and Half options. 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.

Parameters

LabelExplanationData Type
Input raster

The input raster to be filtered based on the majority of contiguous neighboring cells.

It must be of integer type.

Raster Layer
Number of neighbors to use
(Optional)

Determines the number of neighboring cells to use in the kernel of the filter.

  • FourThe kernel of the filter will be the four direct (orthogonal) neighbors to the present cell. This is the default.
  • EightThe kernel of the filter will be the eight nearest neighbors (a three-by-three window) to the present cell.
String
Replacement threshold
(Optional)

Specifies the number of contiguous (spatially connected) cells that must be of the same value before a replacement will occur.

  • MajorityA majority of cells must have the same value and be contiguous. Three out of four or five out of eight connected cells must have the same value.
  • HalfHalf of the cells must have the same value and be contiguous. Two out of four or four out of eight connected cells must have the same value. This option will have a more smoothing effect than the other.
String

Return Value

LabelExplanationData Type
Output raster

The output filtered raster.

The output is always of integer type.

Raster

MajorityFilter(in_raster, {number_neighbors}, {majority_definition})
NameExplanationData 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.

  • FOURThe kernel of the filter will be the four direct (orthogonal) neighbors to the present cell. This is the default.
  • EIGHTThe kernel of the filter will be the eight nearest neighbors (a three-by-three window) to the present cell.
String
majority_definition
(Optional)

Specifies the number of contiguous (spatially connected) cells that must be of the same value before a replacement will occur.

  • MAJORITYA majority of cells must have the same value and be contiguous. Three out of four or five out of eight connected cells must have the same value.
  • HALFHalf of the cells must have the same value and be contiguous. Two out of four or four out of eight connected cells must have the same value. This option will have a more smoothing effect than the other.
String

Return Value

NameExplanationData 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"

# Execute MajorityFilter
outMajFilt = MajorityFilter(inRaster, "EIGHT", "HALF")

# Save the output 
outMajFilt.save("c:/sapyexamples/output/majfilter")

Related topics