convolutions: Make convolution calculations from numeric matrix

convolution2DR Documentation

Make convolution calculations from numeric matrix

Description

This function takes a matrix object, and for each cell multiplies its neighborhood by the kernel. Finally, it returns for each cell the mean of the kernel-weighted sum.

Usage

convolution2D(X, kernel, times = 1, normalize = FALSE)

convolutionQuantile(X, kernel, probs, times = 1, normalize = FALSE)

convolutionMedian(X, kernel, times = 1)

Arguments

X

A numeric matrix object used for apply filters.

kernel

A little matrix used as mask for each cell of X.

times

How many times do you want to apply the filter?

normalize

logical indicating if results will (or not) be normalized. See details.

probs

numeric vector of probabilities with values in [0,1].

Details

Convolution is a mathematical operation which allows the multiplication of two arrays of numbers, in order to produce an array of numbers of the same dimensionality. Valid results (showed in output) will be only those with non-NA values, so NA holes on a matrix will expand in the order of the kernel size.

Normalization consists on dividing the output in every window calculation by the sum(abs(as.numeric(kernel))) (disabled by default).

Value

convolution2D returns a matrix object with the same dimensions of X.

convolutionQuantile uses the kernel but, for each cell, it returns the position of quantile 'probs' (value between 0 and 1).

convolutionMedian is a wrapper of convolutionQuantile with probs = 0.5.

Examples

# Generate example matrix
nRows <- 50
nCols <- 100

myMatrix <- matrix(runif(nRows*nCols, 0, 100), nrow = nRows, ncol = nCols)
kernel <- diag(3)

# Make convolution
myOutput1 <- convolution2D(myMatrix, kernel)
myOutput2 <- convolutionQuantile(myMatrix, kernel, probs = 0.7)

# Plot results
par(mfrow = c(2, 2))
image(myMatrix, zlim = c(0, 100))
image(myOutput1, zlim = c(0, 100))
image(myOutput2, zlim = c(0, 100))

imagine documentation built on April 27, 2023, 5:12 p.m.