Vignette for imagine package

Wencheng Lau-Medrano

2017-05-02

imagine

Imaging Engine, Tools for Application of Image Filters to Data Matrices

This package was built to take numeric data matrices and apply image-filtering algoriths.

Algoriths used by imagine include median-filter and 2D-convolution based algoriths implemented on Rcpp (C++) because sometimes the applicaion of this filters to large numeric matrices is very intensive and time expensive.

Installation

For installing imagine, as follows:

install.packages("imagine")

Engines

imagine performs algorithms written in Rcpp (called ‘engines’), ensuring a faster application of filters. At version 1.0.0, imagine include five main engines, described as follows:

Main functions

There are 5 main functions and 2 wrappers at version 1.1.0, described as follows:

Convolution functions

# Build kernels
# Kernel 1: For bottom edge recognition
kernel1 <- matrix(c(-1, -2, -1,
                     0,  0,  0,
                     1,  2,  1), nrow = 3)

# Kernel 2: Diagonal weighting
kernel2 <- matrix(c(1, 0, 1,
                    0, 2, 0,
                    1, 0, 1), nrow = 3)

# Apply filters
convolutionExample  <- convolution2D(dataMatrix = wbImage, kernel = kernel1)
convQuantileExample <- convolutionQuantile(dataMatrix = wbImage, kernel = kernel2, x = 0.7)

For compare results, we will plot both data (original and filtered) using image function, as shows in figures 1 and 2.

Original

Figure 2: Original matrix

Figure 2: Original matrix

Filtered

Figure 1: Filtered matrices

Figure 1: Filtered matrices

Median-filter asociated functions

# Add some noise (NA) to the image (matrix)
set.seed(7)
naIndex <- sample(x = seq(prod(dim(myMatrix))), size = as.integer(0.4*prod(dim(myMatrix))), replace = FALSE)
myMatrix[naIndex] <- NA

# Build kernel
radius <- 3

# Apply filters
meanfilterExample     <- meanFilter(dataMatrix = myMatrix, radius = radius)
quantilefilterExample <- quantileFilter(dataMatrix = myMatrix, radius = radius, x = 0.1)
medianfilterExample   <- medianFilter(dataMatrix = myMatrix, radius = radius)

For compare results, we will plot both data (original and filtered) using image function, as shows in figures 1 and 2.

Original

Figure 2: Original matrix

Figure 2: Original matrix

Filtered

Figure 1: Filtered matrices

Figure 1: Filtered matrices

Kernel application

In the field of image processing, one of the tools most commonly used are the convolutions, which consist of operations between two arrays: The array of image data and small matrices, called kernels, which weights each pixel values with its corresponding neighborhood. Different kernels produce different effects, for instance blur, shifted images (right, left, up or down), sharpening, etc.

The larger the radius, more pixels remain unanalyzed at the edges. In addition, kernel argument of convolution2D is a matrix, but unlike the meanFilter2D and meanFilter2D functions, it should not necessarily have square dimensions. So, if the user specifies a rectangular kernel, the function will use the neighborhood as the Figure 3 shows.

Figure 3: Neighborhood kernel application for different kernel dimensions. Black dot indicates the position of the pixel over the filter will be applied. Arrows indicates the direction of filter application

Figure 3: Neighborhood kernel application for different kernel dimensions. Black dot indicates the position of the pixel over the filter will be applied. Arrows indicates the direction of filter application