Learn R Programming

imbibe: A pipe-friendly image calculator

The imbibe R package offers fast, chainable image-processing operations which are applicable to images of two, three or four dimensions. It provides an R interface to the core C functions of the niimath project by Chris Rorden, which is in turn a free-software reimplementation of fslmaths, so it has its roots in medical image analysis and is particularly well-suited to such data. The package was designed from the outset to work well with the pipe syntax widely popularised amongst R users by the Tidyverse family of packages.

Installation

This package is still at quite an early stage of development. The latest version can be easily installed using the remotes package.

## install.packages("remotes")
remotes::install_github("jonclayden/imbibe")

Usage

The first step for any usage of the package is to create or read in an image. We will use a 3D medical image from the RNifti package by way of an example.

library(RNifti)
library(imbibe)
image <- readNifti(system.file("extdata", "example.nii.gz", package="RNifti"))

We can also use the RNifti image viewer to visualise the image.

view(image)

A simple example operation would be to smooth the image with a Gaussian smoothing kernel of standard deviation 4 mm. We can use standard R syntax to perform this operation, return a result, and then show it:

smoothed <- run(smooth_gauss(image, 4))
view(smoothed)
## Setting window to (0, 549.9)

Here, smooth_gauss() requests the smoothing operation, and run() actually runs the pipeline and returns the processed image.

However, the pipe syntax provides an alternative, which can be further simplified because calling view() on a pipeline will implicitly run it.

image %>% smooth_gauss(4) %>% view()
## Setting window to (0, 549.9)

Notice now smooth_gauss() is now called with only one argument, and view() with none, because the input to the pipe (%>%) is implicitly added first. The benefits to readability of this approach increase substantially as more operations are added to the chain:

image %>% kernel_sphere(radius=3) %>% dilate() %>% subtract(image) %>% view()
## Setting window to (0, 60)

This example sets up a spherical kernel of radius 3 mm, dilates the image with it, and then subtracts the original image from the result to leave just the outer edge of the imaged object.

Copy Link

Version

Install

install.packages('imbibe')

Monthly Downloads

166

Version

0.1.1

License

BSD_3_clause + file LICENCE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

November 9th, 2022

Functions in imbibe (0.1.1)

dim_mean

Dimensionality reduction operations
reexports

Objects exported from other packages
run

Run a pipeline and return an image result
threshold

Image thresholding
dilate

Mathematical morphology and filtering operations
kernel_3d

Mathematical morphology kernels
exponent

Basic unary operations
expect_pipeline_result

Expectation for testing pipeline output
add

Basic binary operations
imbibe

Create an operation pipeline