drawtext {EBImage}R Documentation

Drawing primitives on images

Description

The family of functions to draw primitives on images. At the moment, there is only one functions out of the planned family to draw text.

Usage

  ## S4 methods for signatures 'img=Image,xy=numeric,label=character' 
  ## and 'img=Image,xy=matrix,label=character' and
  ## S4 method for signature 'Image, list, list':
  drawtext(img, xy, labels, font, col, ...)
    
  drawfont(family=switch(.Platform$OS.type, windows="Arial", "helvetica"), 
           style="n", size=14, weight=200, antialias=TRUE)

Arguments

img An object of Image. With indexed images, please ensure that the color range is in [0,1].
xy (x,y) coordinates of labels. For single frames a matrix with the first column being x and second y or a corresponding numeric vector as it would be used to construct such a matrix. For multiple frames, a list of corresponding matrices/vectors.
labels A character vector of labels to be output. For multiple frames a list of such characters.
font An S3 object of class DrawFont as returned by drawfont. If omitted, the drawfont functions is called internally to obtain the default values.
col A character vector of font colors. One per frame, recycled between frames if required.
... Reserved.
family A character value for the font family to use. On Linux/UNIX machines one can try to use helvetica, times, courier and symbol. On Windows machines, one can specify installed TrueType fonts, like Arial.
style A character value for the font style to use. Can be specified by providing the first letter only. Supported are: normal (default), italic, oblique.
size A numeric value for the font size.
weight A numeric value for the font weight (bold font). Supported values between 100 and 900.
antialias A logical value for whether the font should be anti-aliased.

Value

An object of Image even if the supplied img was of any derived class. If supplied was an IndexedImage, ensure that it was normalized to the range $[0,1]$ before calling drawtext: the function is for annotation purposes only and using it with IndexedImage's is likely to destroy indexing information!

Author(s)

Oleg Sklyar, osklyar@ebi.ac.uk, 2007

See Also

Image

Examples

## FIXME: This example is currently excluded from MacOS builds as it fails in 
## ImageMagick string assertion. Although I tried to find the reason for
## the problem, I do not have a test environment (MacOS). Please test it and
## report what goes wrong here and at which stage.

if ( length(grep("apple", Sys.getenv("R_PLATFORM"))) == 0 ) {
  ## load images
  f <- paste( system.file(package="EBImage"), "images/Gene1_G.tif", sep="/" )
  ii = normalize(readImage(f), separate=TRUE)
  ## segment
  mask = thresh(ii, 25, 25, 0.02)
  mk3 = morphKern(3)
  mk5 = morphKern(5)
  mask = dilate(erode(closing(mask, mk5), mk3), mk5)
  ## index objects in images and remove bad ones
  io = watershed( distmap(mask), 1.5, 1)
  ft = hullFeatures(io)
  mf = moments(io, ii)   ## need these for intensity and size
  for ( i in seq_along(ft) ) ft[[i]] = cbind(ft[[i]], mf[[i]])
  io = rmObjects(io, lapply(ft, function(x) 
         which(x[,"h.s"] < 150 | x[,"int"] < 50 | 0.3 * x[,"h.p"] < x[,"h.edge"] )
       ))
  ft = hullFeatures(io)
  ## get centres of objects (list, for ii is a stack of 4)
  xy <- lapply(ft, function(x) x[,1:2])
  # create labels for objects (list, for ii is a stack of 4)
  labels <- lapply(xy, function(x) as.character(1:nrow(x)))
  ## set font properties: semi bold
  f <- drawfont()
  f$weight=600
  ## draw annotations, recycle 2 colours between 4 images
  annot <- drawtext(channel(ii,"rgb"), xy, labels, font=f, col=c("#F0B769","#ACEE3F"))
  if (interactive()) display(annot)
}


[Package EBImage version 2.6.0 Index]