i.segment mean shift algorithm - Identifies segments (objects) from imagery data using mean shift algorithm.


imagery, segmentation,



i.segment [-dwap] group=name output=name [band_suffix=name] threshold=value [radius=value] [hr=value] [method=string] [similarity=string] [minsize=value] [memory=value] [iterations=value] [seeds=name] [bounds=name] [goodness=name] [--overwrite] [--help] [--verbose] [--quiet] [--ui]


Use 8 neighbors (3x3 neighborhood) instead of the default 4 neighbors for each pixel

Weighted input, do not perform the default scaling of input raster maps

Use adaptive bandwidth for mean shift
Range (spectral) bandwidth is adapted for each moving window

Use progressive bandwidth for mean shift
Spatial bandwidth is increased, range (spectral) bandwidth is decreased in each iteration

Allow output files to overwrite existing files

Print usage summary

Verbose module output

Quiet module output

Super quiet module output

Force launching GUI dialog


group=name [required]

Name of input imagery or imagery group

output=name [required]

Name of output segmentation map

threshold=value [required]

Difference threshold between 0 and 1 Threshold = 0 merges only identical segments; threshold = 1 merges all


Suffix for output bands with shifted mean value


spatial bandwidth in number of cells


range bandwidth in number of cells


Segmentation method Options: region_growing, mean_shift, watershed


Similarity calculation method Options: euclidean, manhattan Default: euclidean


Minimum number of cells in a segment The final step will merge small segments with their best neighbor Options: 1-100000 Default: 1


Memory in MB Default: 300


Maximum number of iterations during the mean shift process


Name for input raster map with starting seeds


Name of input bounding/constraining raster map


Name for output goodness of fit estimate map


Boolean if overtie the existing raster


run with the user interface mode


Mean shift segmentation is a local homogenization technique that is very useful for damping shading or tonality differences in localized objects. For the algorithm implementation of this case, basically the algorithm replaces each pixel with the mean of the pixels in a range-r neighborhood and whose value is within a distance d. The Mean Shift usually has 3 important parameters: 1) A distance function for measuring distances between pixels. Usually the Euclidean distance, but any other well-defined distance function could be used. The Manhattan Distance is another useful choice sometimes. 2) A radius (spatial bandwidth). All pixels within this radius (measured according the above distance) will be accounted for the calculation. 3) A value difference (range bandwidth). From all pixels inside radius r, we will take only those whose values are within this difference for calculating the mean [1].


Mean shift

Numerous nonparametric clustering methods can be separated into two parts:hierarchical clustering and density estimation. Hierarchical clustering composes either aggregation or division based on some proximate measure. The concept of the density estimation-based nonparametric clustering method is that the feature space can be considered as the experiential probability density function (p.d.f.) of the represented parameter. The mean shift algorithm can be classified as density estimation. It adequately analyzes feature space to cluster them and can provide reliable solutions for many vision tasks. The basics of mean shift are discussed as below [3].


  • An extremely versatile tool for feature space analysis. Mean-shift algorithm is applicable not only in image segmentation, but also in motion detector, image filtering and etc.
  • Suitable for arbitrary feature spaces.


  • The range bandwidth (hr) and spatial bandwidth (hs) are the only two factors can control the output, especially the range bandwidth results in a tremendous influence to the result.
  • The computation time is quite long, for the iterations of anisotropic filtering, clustering and merging small objects.

Seeded Region Growing

The seeded region growing (SRG) algorithm is one of the simplest region-based segmentation methods. It performs a segmentation of an image with examine the neighboring pixels of a set of points, known as seed points, and determine whether the pixels could be classified to the cluster of seed point or not [2].

  • The image could be split progressively according to user demanded resolution because

the number of splitting level is determined by user.

  • User could split the image using the criteria to be decided, such as mean or variance of segment pixel value. In addition, the merging criteria could be different to the splitting criteria


  • The blocky segment problem could be reduced by splitting in higher level, but the trade off is that the computation time will arise.

Other segmentation methods


The main goal of watershed segmentation algorithm is to find the “watershed lines” in an image in order to separate the distinct regions. To imagine the pixel values of an image is a 3D topographic chart, where x and y denote the coordinate of plane, and z denotes the pixel value. The algorithm starts to pour water in the topographic chart from the lowest basin to the highest peak.

  • The boundaries of each region are continuous.

Cons: The segmentation result has over-segmentation problem. The algorithm is time-consuming.

Fast scanning algorithm

The concept of fast scanning algorithm is to scan from the upper-left corner to lower-right corner of the whole image and determine if we can merge the pixel into an existed clustering. The merged criterion is based on our assigned threshold. If the difference between the pixel value and the average pixel value of the adjacent cluster is smaller than the threshold, then this pixel can be merged into the cluster.

  • The pixels of each cluster are connected and have similar pixel value, i.e. it has good shape connectivity.
  • The computation time is faster than both region growing algorithm and region splitting and merging algorithm.
  • The segmentation results exactly match the shape of real objects, i.e. it has good shape matching.

Hierarchical clustering

The concept of hierarchical clustering is to construct a dendrogram representing the nested grouping of patterns (for image, known as pixels) and the similarity levels at which groupings change.The hierarchical clustering can be divided into two kinds of algorithm: the hierarchical agglomerative algorithm and the hierarchical divisive algorithm.

  • The process and relationships of hierarchical clustering can just be realized by checking the dendrogram.
  • The result of hierarchical clustering presents high correlation with the characteristics of original database.
  • We only need to compute the distances between each pattern, instead of calculating the centroid of clusters.


  • For the reason that hierarchical clustering involves in detailed level, the fatal problem is the computation time

K-mean clustering


  • K-means algorithm is easy to implement.
  • Its time complexity is O(n), where n is the number of patterns. It is faster than the hierarchical clustering.


  • The result is sensitive to the selection of the initial random centroids.
  • We cannot show the clustering details as hierarchical clustering does.


Minimum segment size

Minimum segment size is the parameter control the minimum object size interm of pixel fro the segment result. Its default value is zero while in reality the segmentation objects are always larger than 1. Considering that the result of clumping is in line with the logic of adaptive bandwidth, minimum segment size should be >1 when using adaptive bandwidth, this would connect the border cells to the appropriate segments. Such a message could be added for mean shift + adaptive bandwidth + minimum segment size = 1.

Adaptive bandwidth (-a flag)

Adaptive bandwidth means here that the range bandwidth is adapted for each moving window separately. Range bandwidth is a very important factor which control the segment result. Sometimes fixed range bandwidth in a very large region results in the over-segment or under segment in certain regions. Using adaptive bandwidth, effectively, the bandwidth becomes smaller when there focus cell differs quite a bit from its neighbors, this prevents blending of separate objects that are not clearly separated with regard to band values.

Progressive bandwidth (-p flag)

Progressive bandwidth increases the spatial bandwidth and decreases the range bandwidth with each iteration. Because the segmentation on the very large coverage involved the tremendous variabilities over the different regions, so the fixed bandwidth(s) are not suitable for the entire image. The rationale is that with each iteration, cell values are shifted a bit more towards the object's mean and become more similar to the cell's neighbors (within range bandwidth). Therefore the range bandwidth can be a bit decreased and the spatial bandwidth a bit increased. A larger spatial bandwidth means that fewer iterations are needed to shift cell values towards the object's mean.


Segmentation of single band panchromatic image

This example uses the panchromatic image inluded in the NC sample dataset. Set up an imagery gourp: group=singleband input=lsat7_2002_80

Set the segmentation region, the first testing case will be the full image.

g.region raster=lsat7_2002_80 -p

Firstly test the region with least input arguments.

i.segment --o group=singleband output=lsat7_2002_80_MM_min1 method=mean_shift thresh=0.01

Compare with the original image, there are many small segmentations showing because minsize has been set to 1. Then zoom into a smaller region and compare with the original image.

Compare with the original image, most of the features in original image has been segmented out. the noise can be significantly reduced by increasing the threshold of minsize​.

Segmentation using different range bandwidth and spatial bandwidth

This example uses fixed range bandwidth(hr) value, larger hr value indicates pixel value in a larger range take part in the filtering calculation.

keeping all else constant, changed the value of spatial bandwidth. A larger spatial bandwidth hardly results in larger segments.

Segmentation using adaptive bandwidth and progressive bandwidth

This example uses -a and -p flag to add adaptive bandwidth and progressive bandwidth respectively

In the zoomed-in image, adaptive bandwidth enhanced the result significantly. Because range bandwidth is the most important parameter for mean shift.

Segmentation of multi-bands landsat image

This example uses a group of landsat multi-bands data from GRASS NC sample dataset. Landsat 7 ETM+ band of 5,4,3,2,1 are grouped to be the input of the mean-shift segmentation. group=lsat_Mult_bands input=lsat7_2002_50,lsat7_2002_40,lsat7_2002_30,lsat7_2002_20,lsat7_2002_10

set the segment region to the entire image.

g.region raster=lsat7_2002_40 -p

The input arguements are adaptive bandwidth and progressive band width

i.segment --o group=lsat_Mult_bands threshold=0.01 output=testout_ms_group3 method=mean_shift -a -p


[2] Adams, Rolf, and Leanne Bischof. "Seeded region growing." IEEE Transactions on pattern analysis and machine intelligence 16.6 (1994): 641-647
[3] Y. Cheng, “Mean shift, mode seeking, and clustering,” IEEE Trans. Pattern Analysis and Machine Intelligence, vol. 17, no. 8, pp. 790-799, Aug. 1995
[4]Wang, Yu-Hsiang. "Tutorial: Image Segmentation." National Taiwan University, Taipei (2010): 1-36.

Last modified 3 years ago Last modified on Sep 14, 2016 7:34:37 AM

Attachments (7)

Download all attachments as: .zip