Background

This is an illustration of how to manipulate TIFF images in R. The inspiration came from this tutorial for python. The result of the below described steps is the combination of two images into one:

Image Image Image

Step 1: Read in ultrasound and corresponding mask images

I used the EBImage package to read and manipulate the tiff images.

library(EBImage)

## read tiff files
filepath <- "train/1_1.tif"
filepath.m <- "train/1_1_mask.tif"
Image <- readImage(filepath)
Mask <- readImage(filepath.m)

Let’s have a look at the ultrasound image:

display(Image)

The images are read into objects of class ‘Image’. The pixel data is saved into a matrix called ‘.Data’.

str(Image)
## Formal class 'Image' [package "EBImage"] with 2 slots
##   ..@ .Data    : num [1:580, 1:420] 0 0.361 0.329 0.318 0.318 ...
##   ..@ colormode: int 0

Step 2: Add red overlay to ultrasound image

In order to create the red overlay to the ultrasound image, the image first has to be transformed to a color (RGB) file. After that the ‘paintObjects’ in combination with the mask image will magically create an outline.

## Add masked line to original image
Image_color <- toRGB(Image)
Image_color <- paintObjects(Mask, Image_color, col='red')
display(Image_color)