Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

morphology

Perform morphological operations on images


Description

Functions to perform morphological operations on binary and grayscale images.

Usage

dilate(x, kern)
erode(x, kern)
opening(x, kern)
closing(x, kern)
whiteTopHat(x, kern)
blackTopHat(x, kern)
selfComplementaryTopHat(x, kern)

makeBrush(size, shape=c('box', 'disc', 'diamond', 'Gaussian', 'line'), step=TRUE, sigma=0.3, angle=45)

Arguments

x

An Image object or an array.

kern

An Image object or an array, containing the structuring element. kern is considered as a binary image, with pixels of value 0 being the background and pixels with values other than 0 being the foreground.

size

A numeric containing the size of the brush in pixels. This should be an odd number; even numbers are rounded to the next odd one, i.e., size = 4 has the same effect as size = 5. Default is 5

shape

A character vector indicating the shape of the brush. Can be box, disc, diamond, Gaussian or line. Default is box.

step

a logical indicating if the brush is binary. Default is TRUE. This argument is relevant only for the disc and diamond shapes.

sigma

An optional numeric containing the standard deviation of the Gaussian shape. Default is 0.3.

angle

An optional numeric containing the angle at which the line should be drawn. The angle is one between the top of the image and the line.

Details

dilate applies the mask kern by positioning its center over every pixel of the image x, the output value of the pixel is the maximum value of x covered by the mask. In case of binary images this is equivalent of putting the mask over every background pixel, and setting it to foreground if any of the pixels covered by the mask is from the foreground.

erode applies the mask kern by positioning its center over every pixel of the image x, the output value of the pixel is the minimum value of x covered by the mask. In case of binary images this is equivalent of putting the mask over every foreground pixel, and setting it to background if any of the pixels covered by the mask is from the background.

opening is an erosion followed by a dilation and closing is a dilation followed by an erosion.

whiteTopHat returns the difference between the original image x and its opening by the structuring element kern.

blackTopHat subtracts the original image x from its closing by the structuring element kern.

selfComplementaryTopHat is the sum of the whiteTopHat and the blackTopHat, simplified the difference between the closing and the opening of the image.

makeBrush generates brushes of various sizes and shapes that can be used as structuring elements.

Processing Pixels at Image Borders (Padding Behavior)

Morphological functions position the center of the structuring element over each pixel in the input image. For pixels close to the edge of an image, parts of the neighborhood defined by the structuring element may extend past the border of the image. In such a case, a value is assigned to these undefined pixels, as if the image was padded with additional rows and columns. The value of these padding pixels varies for dilation and erosion operations. For dilation, pixels beyond the image border are assigned the minimum value afforded by the data type, which in case of binary images is equivalent of setting them to background. For erosion, pixels beyond the image border are assigned the maximum value afforded by the data type, which in case of binary images is equivalent of setting them to foreground.

Value

dilate, erode, opening, whiteTopHat, blackTopHat and selfComplementaryTopHat return the transformed Image object or array x, after the corresponding morphological operation.

makeBrush generates a 2D matrix containing the desired brush.

Note

Morphological operations are implemented using the efficient Urbach-Wilkinson algorithm [1]. Its required computing time is independent of both the image content and the number of gray levels used.

Author(s)

Ilia Kats <ilia-kats@gmx.net> (2012), Andrzej Oles <andrzej.oles@embl.de> (2015)

References

[1] E. R. Urbach and M.H.F. Wilkinson, "Efficient 2-D grayscale morphological transformations with arbitrary flat structuring elements", IEEE Trans Image Process 17(1), 1-8, 2008

Examples

x = readImage(system.file("images", "shapes.png", package="EBImage"))
  kern = makeBrush(5, shape='diamond')  
  
  display(x)
  display(kern, title='Structuring element')
  display(erode(x, kern), title='Erosion of x')
  display(dilate(x, kern), title='Dilatation of x')

  ## makeBrush
  display(makeBrush(99, shape='diamond'))
  display(makeBrush(99, shape='disc', step=FALSE))
  display(2000*makeBrush(99, shape='Gaussian', sigma=10))

EBImage

Image processing and analysis toolbox for R

v4.32.0
LGPL
Authors
Andrzej Oleś, Gregoire Pau, Mike Smith, Oleg Sklyar, Wolfgang Huber, with contributions from Joseph Barry and Philip A. Marais
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.