This paper is dedicated to a demonstration of the procedures of Mathematical Morphology implemented in RGeostats.

`library(RGeostats)`

`## Loading required package: Rcpp`

`## Loading RGeostats - Version:11.0.5`

These features will all be demonstrated on a regular 2-D grid (even if they are also available in 3-D). All the graphics will be displayed with a constant aspect ratio equal to 1.

```
constant.define("asp",1)
grid = db.create(nx=c(500,300))
```

We first generate a non-conditional simulation which will serve as the starting information stored on the grid. This simulation is performed using the Turning Band simulation Method, using a Model composed of an isotropic cubic structure. Its range is equal to 30 (to be compared with the grid extension 500 by 300)

```
model = model.create(vartype="Cubic",range=40)
grid = simtub(,grid,model,nbtuba=1000,seed=3213)
grid = db.rename(grid,grid$natt,"Simulation")
plot(grid)
```

The first task is to truncate the previous Gaussian variable (which has a symmetric spread of values around 0). The result is a binary image with 1 when the initial variable was positive, and o otherwise.

```
grid = morpho(grid,vmin=0,oper="thresh")
grid = db.rename(grid,grid$natt,"Binary")
plot(grid)
```

Staring from this binary image, we perform an erosion of this image, using a structuring element which corresponds to a cross with an extension of 1 pixel.

```
radius = 1
option = 0
grid = db.locate(grid,"Simulation","z")
grid = morpho(grid,vmin=0,oper="erode",option=option,radius=radius)
grid = db.rename(grid,grid$natt,"Erosion")
plot(grid)
```

For comparison sake, we can perform a dilation instead, using the same structuring element.

```
grid = db.locate(grid,"Simulation","z")
grid = morpho(grid,vmin=0,oper="dilate",option=option,radius=radius)
grid = db.rename(grid,grid$natt,"Dilation")
plot(grid)
```

An interesting feature is obtained by comparing the results of the Dilation to the one of the Erosion: it highlight the edge.

```
grid = db.add(grid,Dilation-Erosion)
grid = db.rename(grid,grid$natt,"Edge")
plot(grid)
```

The opening operation consists in an erosion followed by a dilation.

```
grid = db.locate(grid,"Simulation","z")
grid = morpho(grid,vmin=0,oper="open",option=option,radius=radius)
grid = db.rename(grid,grid$natt,"Opening")
plot(grid)
```

while the closing is the inverse sequence, i.e.Â a dilation followed by an erosion.

```
grid = db.locate(grid,"Simulation","z")
grid = morpho(grid,vmin=0,oper="close",option=option,radius=radius)
grid = db.rename(grid,grid$natt,"Closing")
plot(grid)
```

All previous operations can be run again changing the structuring element from cross to block. The next figure demonstrates the erosion with this new option.

```
radius = 1
option = 1
grid = db.locate(grid,"Simulation","z")
grid = morpho(grid,vmin=0,oper="erode",option=option,radius=radius)
grid = db.rename(grid,grid$natt,"Erosion.Block")
plot(grid)
```

Each one of the previous applications can be performed several times in sequence. The following figure shows the erosion (with the block structuring element) iterated 5 times.

```
niter=5
grid = db.locate(grid,"Simulation","z")
grid = morpho(grid,vmin=0,oper="erode",option=option,radius=radius,
niter=niter)
grid = db.rename(grid,grid$natt,"Erosion_iterated")
plot(grid,title=paste0("Erosion (x",niter,")"))
```

Starting from the last binary image (resulting from the Erosion iterated), we now wish to distinguish the different connected components.

```
grid = db.locate(grid,"Erosion_iterated","z")
grid = morpho(grid,vmin=0.5,oper="ccsize")
grid = db.rename(grid,grid$natt,"Connected components")
plot(grid)
```

The last feature is to calculate the distance to the edge (of the set where initial values were 1). The black color is used to represent pixels where initial values were 0.

```
grid = db.locate(grid,"Erosion_iterated","z")
grid = morpho(grid,vmin=0.5,oper="dist")
grid = db.rename(grid,grid$natt,"Distance to Edge")
plot(grid)
```