r.seg - manual
GRASS logo


r.seg - Generates a smooth approximation of the input raster and a discontinuity map


image segmentation, edge detection, smooth


r.seg help
r.seg [-k] in_g=string out_u=string out_z=string [lambda=float] [alpha=float] [mxi=integer] [tol=float] [kepsilon=float] [beta=float] [--overwrite] [--verbose] [--quiet]


Activate MSK model (Mumford-Shah with curvature term)
Allow output files to overwrite existing files
Verbose module output
Quiet module output


Input raster map to segment
Output segmented raster map
Output raster map with detected discontinuities
Smoothness coefficient [>0]
Default: 1.0
Discontinuity coefficient [>0]
Default: 1.0
Maximal number of numerical iterations
Default: 100
Convergence tolerance [>0]
Default: 0.001
Discontinuity thickness [>0]
Default: 1.0
Curvature coefficient [>=0]
Default: 0.0


r.seg generates a piece-wise smooth approximation of the input raster map and a raster map of the discontinuities of the output approximation.
The discontinuities of the output approximation are preserved from being smoothed. The values of the discontinuity map are close to one where the output approximation is "homogeneous", where the output approximation has discontinuities (edges) the values are close to zero.
The module makes use of the varseg library which implements the Mumford-Shah [1] variational model for image segmentation. The Mumford-Shah variational model with curvature term [2] is also implemented in the library. The curvature term prevents the discontinuities from being shortened too much when the parameter alpha is set to very high values, (this happens very rarely).
Some examples of use of the module can be found here and in this presentation [FOSS4G 2009 - pdf].
For details on the numerical implementation see [3].


Remove any MASK before the execution of the module. If a MASK is present, the module stops after just one iteration.

Replace (r.null) any null data with the map average value (get with r.univar).

The segmentation depends on the parameters alpha and lambda:
  • alpha controls how many discontinuities are allowed to exist.
  • lambda controls the smoothness of the solution.
  • It is not possible to select the values of the parameters in an automatic way. Test some different values to understand their influence on the results. Try the following procedure:
    • run the module with both alpha and lambda set to 1.0
    • run the module with alpha set to 1.0 and different values for lambda
        e.g., 0.01, 0.1, 1, 10, 100
    • run the module with lambda set to 1.0 and different values for alpha
        e.g., let's say 0.01, 0.1, 1, 10, 100
    • see how the segmentations change and select the values that produce the result that best fits your requirements.
The module computes the segmentation by means of an iterative procedure.
The module stops either when the number of iterations reaches the maximum number of iterations [mxi] or when the maximum difference between the solutions of two successive iterations is less than the convergence tolerance [tol].
To stop the iteration procedure, it is easier to act on the maximum number of iterations parameter [mxi] than on the convergence tolerance parameter [tol].
The number of iterations needed to reach the convergence tolerance increases for high values of the parameter lambda. The larger the total number of pixels of the input raster map the larger the number of iterations will be.

The data type of the output raster maps is DOUBLE PRECISION.

The module works on one raster map at a time, imagery groups are not supported.

To avoid to inappropriately re-sampled the input raster map, the settings for the current region should be set so that:
  • the resolution of the region matches the resolution of the input raster map;
  • the boundaries of the region are lined up along the edges of the nearest cells in the input raster map.
The discontinuity thickness should be changed for test purposes only.

The actual need to use the MSK model should be very rare, see [3]. Due to a different implementation of the MSK model with respect to MS one, the values of the parameters lambda and alpha in MSK have to be set independently from the values used in MS.


This example is based the North Carolina GRASS sample data set, [complete GRASS location].
# set the region to match the ortho_2001_t792_1m raster map:
g.region rast=ortho_2001_t792_1m

# select a smaller region:
g.region n=221725 s=220225 w=638350 e=639550

# run r.seg:
r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u_OF out_z=z_OF lambda=10 alpha=200 mxi=250

# for a better visualization of the output raster map u_OF, set its color table to:
r.colors u_OF rast=ortho_2001_t792_1m

# compute the difference between the input raster map and the output raster map u_OF:
r.mapcalc "diff = abs(ortho_2001_t792_1m@PERMANENT - u_OF)"

# for a better visualization of the differences, compute the natural logarithm of the diff map:
r.mapcalc "log_diff = log(1 + diff)"

# and set its color table to the "differences" style:
r.colors log_diff color=differences

# for a better visualization of the output raster map u_OF, set its color table to:
r.colors z_OF color=bgyr

# run r.seg with different parameter values:
r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u1_OF out_z=z1_OF lambda=10 alpha=65 mxi=250
r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u2_OF out_z=z2_OF lambda=10 alpha=600 mxi=250
r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u3_OF out_z=z3_OF lambda=0.1 alpha=200 mxi=250
r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u4_OF out_z=z4_OF lambda=1 alpha=200 mxi=250

# visualize and compare the different results


  • [1] D. Mumford and J. Shah. Optimal Approximation by Piecewise Smooth Functions and Associated Variational Problems.
    Communications on Pure Applied Mathematics, 42:577-685, 1989.
  • [2] R. March and M. Dozio. A variational method for the recovery of smooth boundaries.
    Image and Vision Computing, 15:705-712, 1997.
  • [3] A. Vitti. Free discontinuity problems in image and signal segmentatiion.
    Ph.D. Thesis - University of Trento (Italy), 2008.


i.smap, i.zc, r.mfilter


Alfonso Vitti
  Dept. Civil and Environmental Engineering
  University of Trento - Italy
  alfonso.vitti [at] ing.unitn.it

Last changed: $Date: 2010-08-10 12:00:00 +0200 (Tue, 10 Aug 2010)$

Main index - raster index - Full index

© 2003-2010 GRASS Development Team

Home | SW page

..: 2009 :: Alfonso Vitti :..