Mathematical Morphology for 1D

Guillermo Ayala

2026-07-08

Abstract

This package contains some basic tools for one dimensional mathematical morphology. The standard reference for Mathematical Morphology is Serra (1982).

Packages

pacman::p_load(ggplot2,mmorph1D,patchwork)

Data

ff = function(x,k=0){
    abs(sin(x+k*pi)  + rnorm(length(x),mean=0,sd=.05))
    }

The functions is going to be plotted.

xt = seq(-10,10,.01)
x = 1:length(xt)
f = ff(xt)
df = data.frame(xt,x,f)
ggplot(df,aes(x=xt,y=f))+geom_line()

Basic transformations

First, we show the erosion, dilation, opening and closing
of the original function.

se = -70:70
g = erosion(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p1 = ggplot(df0,aes(x=x,y=g))+geom_line() + ylim(0,1)
g_all = g

g = dilation(f=f,x=x,se=se)
df0 = data.frame(x,g)
p2 = ggplot(df0,aes(x=x,y=g))+geom_line()+ylim(0,1)
g_all = c(g_all,g)

g = opening(f=f,x=x,se=se)
df0 = data.frame(x,g)
p3 = ggplot(df0,aes(x=x,y=g))+geom_line()+ylim(0,1)
g_all = c(g_all,g)

g = closing(f=f,x=x,se=se)
df0 = data.frame(x,g)
p4 = ggplot(df0,aes(x=x,y=g))+geom_line()+ylim(0,1)
g_all = c(g_all,g)

df0 = data.frame(u=rep(x,4),v= g_all,
                type = factor(rep(1:4,each=length(x)),levels=1:4,
                              labels=c("erosion","dilation","opening",
                                       "closing")))

ggplot(df0,aes(x= u,y=v,color=type))+geom_line()

Now the top-hat, bottom-hat, gradient and Beucher gradient.

se = -70:70
g = tophat(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p5 = ggplot(df0,aes(x=x,y=g))+geom_line() + ylim(0,1)
g_all = g

g = bottomhat(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p6 = ggplot(df0,aes(x=x,y=g))+geom_line() + ylim(0,1)
g_all = c(g_all,g)

df0 = data.frame(u=rep(x,2),v= g_all,
                type = factor(rep(1:2,each=length(x)),levels=1:2,
                              labels=c("tophat","bottom-hat")))

ggplot(df0,aes(x= u,y=v,color=type))+geom_line()

p5 + p6

Two versions of the gradient using morphological tools.

se = -70:70

g = gradient(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p7 = ggplot(df0,aes(x=x,y=g))+geom_line() + ylim(0,1)
g_all = g

g = gradientB(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p8 = ggplot(df0,aes(x=x,y=g))+geom_line() 
g_all = c(g_all,g)

g = gradExt(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p9 = ggplot(df0,aes(x=x,y=g))+geom_line() + ylim(0,1)
g_all = c(g_all,g)

g = gradInt(f=f,x=x,se=se)
df0 = data.frame(x,f,g)
p10 = ggplot(df0,aes(x=x,y=g))+geom_line() + ylim(0,1)
g_all = c(g_all,g)

df0 = data.frame(u=rep(x,4),v= g_all,
                type = factor(rep(1:4,each=length(x)),levels=1:4,
                              labels=c("gradient","gradientB","gradExt",
                                       "gradInt")))

ggplot(df0,aes(x= u,y=v,color=type))+geom_line()

p7 + p8 + p9 + p10

Granulometries for a function

This a formalization of the concept of size distribution.

g = granulometry(f=f,lambdaincre=1,lambdamax=200,type="opening")
save(g,file="g.rda")

Granulometries for functions given in a matrix

Let us assume that we have a several functions observed at the same abscisas and there are provided as a matrix where each column corresponds with a different function.

## eval: false
xt = seq(-10,10,.01)
x = 1:length(xt)
f = sapply(1:20,function(i) ff(xt,k=i))
g1 = granulometry_matrix(f,lambdamax=200,lambdaincre=1,type="opening",scale=.01)
dim(g1)
[1] 201  20
save(g1,file="g1.rda")
df1 = reshape2::melt(g1)
names(df1) = c("lambda","sample","f")
df1$sample = factor(df1$sample)
ggplot(df1,aes(x=lambda,y=f,color=sample))+geom_line()

Granulometries for functions given in a list

If the functions have been observed at different durations then we can use to evaluate the granulometries the function granulometry_list().

## eval: false
xt = seq(-10,10,.01)
x = 1:length(xt)
f = lapply(1:20,function(i) ff(xt[1:(i*100)],k=i))
g2 = granulometry_list(f,lambdamax=200,lambdaincre=1,type="opening",scale=.01)
save(g2,file="g2.rda")
load("g2.rda")
df2 = reshape2::melt(g2)
names(df2) = c("lambda","sample","f")
df2$sample = factor(df2$sample)
ggplot(df2,aes(x=lambda,y=f,color=sample))+geom_line()
Serra, J. 1982. Image Analysis and Mathematical Morphology. Academic Press.