Morpher

Applies the operations of mathematical morphology to a raster, where the valid cells are the objects and the novalues the background:

  • dilate grows the objects by one ring of cells, and erode shrinks them by one ring, removing the cells that touch the background;

  • open, an erosion followed by a dilation, removes the small objects and the thin parts; close, a dilation followed by an erosion, fills the small holes and gaps between them;

  • skeletonize thins the objects down to lines one cell wide, in four variants of the thinning, and prune removes the short branches of the lines;

  • lineendings and linejunctions find the cells where the lines end and where they meet.

The first four keep the values of the objects, so they also work on rasters of values, as an elevation model; the others work on the shapes only, and give 1 on the result. The size and shape of the dilation and erosion is set by the kernel, by default the 3x3 square.

On the rasters of the HortonMachine, the morphology mostly cleans the masks made with a threshold, as with Mapcalc or RangeLookup, from the small spots and holes that the noise of the data leaves. The line operations need lines that touch only where they really meet: on the D8 networks of ExtractNetwork, the channels that run side by side touch on the corners, and give false junctions; there NetNumbering finds the confluences properly.

Details

The dilation gives to the novalue cells next to an object the largest value of their neighbours within the kernel, so the objects grow by one ring of cells. The erosion sets to novalue the cells of an object that have a novalue within the kernel, and gives the others the smallest value of their neighbours, so the objects shrink by one ring. The opening, an erosion followed by a dilation, removes the small objects and the thin parts; the closing, a dilation followed by an erosion, fills the small holes and gaps. The kernel is a square of 0 and 1, by default the 3x3 of all 1.

The other operations work on the shapes only, all the valid cells being 1. The skeletonize ones thin the objects to lines of one cell, with four variants of the thinning kernels; the pruning removes from the lines the branches shorter than the number of iterations. The line endings and junctions give 1 on the cells where the lines end and where they meet, as the sources and the confluences of a network of channels.

The operations are the ones of the Hypermedia Image Processing Reference (HIPR2), ported from the work of Simon Horne.

Parameters

Morphological operations on rasters, where the valid cells are the objects and the novalues the background: dilation, erosion, opening and closing of the objects, their thinning to lines one cell wide (skeletonize) and the pruning of the short branches, and the search of the ends and of the junctions of the lines.

Toolbox folder: Raster Processing
Status: Draft
Keywords: Dilation, Erosion, Skeletonize, Open, Close, Raster
Authors: Simon Horne, Andrea Antonello
Class: org.hortonmachine.modules.Morpher

Inputs

Parameter

Type

Unit

Default

Description

inMap

raster file

The map to morph: the valid cells are the objects, the novalues the background.

pKernel

int[]

The kernel of the dilation and erosion, as the rows of a square of 0 and 1 one after the other: 1,1,1,1,1,1,1,1,1 is the default 3x3 one.

doBinary

boolean

true

If true, the dilation is faster but uses only the first valid value of each row of the kernel, so it gives the largest neighbour value only for maps of a single value, as masks.

pMode

String

dilate

The morphological operation. Allowed values: dilate, erode, skeletonize1, skeletonize2, skeletonize2var, skeletonize3, prune, open, close, lineendings, linejunctions.

pIterations

int

3

The number of iterations of the pruning: the branches shorter than this number of cells are removed.

Outputs

Parameter

Type

Unit

Description

outMap

output file

The morphed map: the values of the objects for dilate, erode, open and close, 1 for the other operations.

Example

The example runs on the sample elevation model of the manual, dtm_flanginec.tif in docs/manual/modules/maps/data, each module on the outputs of the previous ones. Mapcalc makes the mask of the slopes steeper than 45 degrees, from Slope, and the opening cleans it:

import org.hortonmachine.modules.Mapcalc
import org.hortonmachine.modules.Morpher

// the folder with the elevation model: change it to yours
var folder = "/data/flanginec/"

// the mask of the steep slopes, above 1 m/m (45 degrees), from the slope of Slope:
// the other cells are novalue, the background of the morphology
var mapcalc = new Mapcalc()
mapcalc.inRaster1 = folder + "slope.tif"
mapcalc.pFunction = """
images {
    slope = read;
    steep = write;
}
steep = con(slope > 1, 1, null);
"""
mapcalc.outRaster = folder + "steep.tif"
mapcalc.process()

// the opening removes the isolated cells and the thin strips, keeping the larger areas
var morpher = new Morpher()
morpher.inMap = folder + "steep.tif"
morpher.pMode = "open"
morpher.outMap = folder + "steep_open.tif"
morpher.process()
The mask of the slopes steeper than 45 degrees: the rock walls, and many isolated cells and thin strips on the edges of the terraces and of the channels.

The mask of the slopes steeper than 45 degrees: the rock walls, and many isolated cells and thin strips on the edges of the terraces and of the channels.

The same mask after the opening: the isolated cells and the thin strips are gone, the compact steep areas, as the rock walls in the north east, remain.

The same mask after the opening: the isolated cells and the thin strips are gone, the compact steep areas, as the rock walls in the north east, remain.