Skip to main content
Version: 2027.01-beta

Custom

Run Script​

If Run Script is selected as the post processing step, Visiopharm will execute an R-script, allowing the creation of custom post processing operations. The file follows the standard UTF8 text file format, and uses US locale for all numbers (. As Decimal separator). The file can contain any number of lines and variables in any format. Anything after a # in a line will be regarded as comments.

Accessing and loading input data: When the script is executed, Visiopharm passes the following arguments to the script:

  • args[1]: Label image
  • args[2]: ROI image
  • args[3]: Input image

The arguments can be retrieved with:

args <- commandArgs(trailingOnly=TRUE)

The label, ROI, and input images can now be loaded using the tiff package. Using this package, images are loaded as arrays of type double with the readTIFF function. Pixel values are automatically converted from byte format (0–255) to double precision values in the range 0–1. For example, a value of 3/255 (~0.012) in the label image indicates that the pixel belongs to the third label type (not including the Clear label, which has value 0). The same applies to the ROI image.

#install.packages("tiff") 
library(tiff)

label <- readTIFF(args[1])
roi <- readTIFF(args[2])
input <- readTIFF(args[3])

Modifications can now be applied. For example, the following code changes all pixels belonging to label type 1 to label type 2.

DIM_label <- dim(label)

for (r in 1:DIM_label[1])
{
for (c in 1:DIM_label[2])
{
if (label[r,c] == 1/255)
{
label[r,c] <- 2/255
}
}
}

The modified images can be written back to the arguments with the writeTIFF command. This line will write the label image back:

writeTIFF(label, args[1])

If the R script throws a warning, the execution will stop, even though the script can technically be executed. To run the script ignoring warnings use the command

options(warn=-1)

Full Script Example: Threshold Classification

The following example performs a threshold classification on the input image. Pixels with a red-channel intensity below 200 are assigned to label type 1.

# R-Script example for execution in Visiopharm APP
# Perform a threshold classification of red channel and apply label 1 to pixels below threshold
# Arguments are expected as
# 1: Label image (Tiff image in byte format)
# 1: ROI image (Tiff image in byte format)
# 1: Input image (Tiff image in byte format)

options(echo=FALSE)
options(warn=-1)
args <- commandArgs(trailingOnly=TRUE)

# install.packages("tiff")
library(tiff) # use tiff package rather than rtiff

# Read label overlay from arguments
label <- readTIFF(args[1])

# Read input image from arguments
img <- readTIFF(args[3])

# Perform threshold classification
DIM_img <- dim(img)

for(r in 1:DIM_img[1])
{
for(c in 1:DIM_img[2])
{
if(img[r,c,1] < 200/255)
{
label[r,c] <- 1/255
}
}
}

# Write resulting label back to arguments
writeTIFF(label, args[1])

#Sys.sleep(2)
warning

Visiopharm supports the execution of the script but does not validate or troubleshoot the script logic itself.