library( knitr ) knitr::opts_chunk$set(collapse = T, comment = "#>") library(ANTsR)
iMath
wraps some functions of ImageMath
from ANTs software. Basic
arithmetics (e.g., add, subtract), are built into the antsImage
class capabilities, and are similar to
array operations in R:
fi <- antsImageRead(getANTsRData("r16"), 2) sumfi <- fi + fi mulfi <- fi * 2 #logarithm of image where non-zero logfi <- log(fi[fi>0]) expfi <- exp(fi[fi>0]) divfi <- sumfi / mulfi
The typical rule for dilation and erosion of images in ANTsR is minimum for erosion and maximum for dilation (ITK rules).
Grayscale dilation of an image (compare it with binary dilation below):
```r
GDdilated <- iMath(fi,"GD", 3)
invisible(plot(GDdilated))
invisible(plot(GDdilated - fi)) ```
Grayscale erosion of an image (compare it with binary erosion below).
r
erosion <- iMath(fi,"GE", 3)
invisible(plot(erosion))
Morphological dilation works on binary images, where it makes white regions bigger:
```r mask <- getMask(fi) MD <- iMath(mask, "MD", 3)
invisible(plot(MD - mask)) ```
Morphological erosion makes white regions of a binary mask smaller.
r
ME <- iMath(mask, "ME", 3)
invisible(plot(ME))
Morphological closing of a binary image: operation MC
fill holes with the
provided radius parameter, for example:
r
newMask <- iMath(mask, "MC", 4)
invisible(plot(newMask,slices=c(1,1)))
Morphological opening of a binary image: removes small structures at the boundary
or interior of an image. Syntax is similar to the previous procedure with the operation
MO
.
PadImage
is a function to add/remove voxels to/from the boundaries of an image.
Positive numbers will pad image in each direction. So for example using 2 will increase an image with 256 voxels to 260.
```r padded <- iMath(fi, "PadImage", 2)
dim(fi) dim(padded) ```
Negative numbers will crop the image in each direciton. For example using -2 will convert a 2-dimensional image with 256 voxels in X and Y direction to 252 voxels in each dimension.
```r cropped <- iMath(fi, "PadImage", -2)
dim(fi) dim(cropped) ```
Distance maps may be used for a number of purposes, including: shape analysis, skeletonization and path finding.
MaurerDistance
implements ITK's SignedMaurerDistanceMap which
calculates the Euclidean distance transform of a binary
image in time linear with total number of voxels [@Maurer2003].
It assumes that inside the binary image has negative distance,
while outside of the binary mask has positive distance.
r
distanceMap <- iMath(mask, "MaurerDistance")
invisible(plot(distanceMap))
D
implements ITK's DanielssonDistanceMap. It calculates the Euclidean
distance map, which shows at each voxel the shortest distance to the nearest
voxel in the background (assuming that the input is a binary image)
[@Danielsson1980].
r
distanceMap <- iMath(mask, "D")
invisible(plot(distanceMap))
To reduce noise without changing important parts of an image in computer vision, Perona-Malik is a
promising method. Perona-Malik method to reduce noise with anisotropic diffusion is accessible
via PeronaMalik
operation in iMath
. It requires two parameters: 1) number of iterations, and
2) conductance. This implements ITK's GradientAnisotropicDiffusionImageFilter.
The conductance parameter is described by ITK's AnisotropicDiffusionFunction: "The conductance parameter controls the sensitivity of the conductance term in the basic anisotropic diffusion equation. It affects the conductance term in different ways depending on the particular variation on the basic equation. As a general rule, the lower the value, the more strongly the diffusion equation preserves image features (such as high gradients or curvature). A high value for conductance will cause the filter to diffuse image features more readily. Typical values range from 0.5 to 2.0 for data like the Visible Human color data, but the correct value for your application is wholly dependent on the results you want from a specific data set and the number or iterations you perform."
denoised <- iMath(fi, "PeronaMalik", 10, 0.5) invisible(plot(denoised)) # to see what the filter has removed invisible(plot(fi - denoised))
Grad
implements ITK's GradientMagnitudeRecursiveGaussian which calculates the gradient of the magnitude of an image by convolution
with the first derivative of a Gaussian. Parameters are:
sigma
(Optional: double, default=0.5) is the full width at half max of the Gaussian kernel,
specified in physical space units.
normalize
(Optional: 0 or 1 boolean, default=0) specifies if the output should be scaled to lie in [0,1]
grad <- iMath(fi, "Grad", 1) invisible(plot(grad))
Laplacian
implements ITK's LaplacianRecursiveGaussianImageFilter which calculates the the Laplacian of Gaussian of an image by convolving with
the second derivative of a Gaussian. Parameters are:
sigma
(Optional: double, default=0.5) is the full width at half max of the Gaussian kernel,
specified in physical space units.
normalize
(Optional: 0 or 1 boolean, default=0) specifies if the output should be scaled to lie in [0,1]
laplacianImage <- iMath(fi, "Laplacian", 1, 1) invisible(plot(laplacianImage))
Usually it is easier to perform sequential procedures starting from left to right,
instead of right to left, as one needs with functions. This has been made possible by
another package that ANTsR
depends on, magrittr
. For example, instead of:
fi<-antsImageRead( getANTsRData("r16") , 2 ) result <- iMath(iMath(fi, "Laplacian", 1), "GD", 3)
One can do:
require(magrittr) result <- fi %>% iMath("Laplacian",1) %>% iMath("GD",3)
| Operation | Example | Description |
| ------------- |:-------------------------:| --------------------------:|
| FillHoles |img %>% iMath("FillHoles")
| Fills holes in binary object|
| GetLargestComponent |img %>% iMath("GetLargestComponent")
|Returns largest portion of binary object|
|Normalize| img %>% iMath("Normalize")
|Creates image negative|
|TruncateImageIntensity| img %>% iMath("TruncateImageIntensity", 0.05, 0.95)
|Trims intensities by quantiles|
|Sharpen| img %>% iMath("Sharpen")
| Makes edges sharper|
data("iMathOps") kable( iMathOps , caption = "Valid iMath Operations", padding = 0 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.