How to convert an image to an espadon object of class volume

How to convert an image (jpg, png, tif) to an espadon object of class volume?

To take advantage of the image processing functions of the espadon package, it is necessary to reformat your image (jpg, png, tif) into an espadon object of class volume.

The following function performs this formatting operation :

vol.from.image <- function (image, description = ""){
  im <- read.bitmap (image)
  if (description == "") {
    description <- unlist (strsplit (basename (image), "[.]"))
    description <- description[1:(length (description) - 1)]
  }
  map <- vol.create (n.ijk = c(dim(im)[2:1],1), dxyz = c(1,1,1), 
                     pt000 = c (0, 0, 0), default.value = 0, modality = "image", 
                     description = description)
  map$acq.date <- substr (file.mtime (image), 1, 10)
  
  if (!is.na (dim (im)[3])){
    R <- as.numeric (t (im[ , , 1]))
    G <- as.numeric (t (im[ , , 2]))
    B <- as.numeric (t (im[ , , 3]))
    if (dim(im)[3]==4) {
      map.rgb <- rgb (R, G, B, as.numeric (t (im[ , , 4])))
    } else {
      map.rgb <- rgb (R, G, B)
    }
    gr.order <- order(R+G+B)
    map$palette <- unique (map.rgb[gr.order])
    map$vol3D.data[ , , 1] <- matrix (match(map.rgb, map$palette), ncol = map$n.ijk[1], byrow = F)
  } else {
 
    resol <- max(im)*(2^(1:16))
    resol <- which(abs(resol - round(resol)) < 1e-6)[1]
    map$vol3D.data[ , , 1] <- t(im) * (2^resol)
    map$palette <- grey.colors((2^resol), start = 0, end = 1)
  }
  
  map$min.pixel <- min(map$vol3D.data, na.rm = T)
  map$max.pixel <- max(map$vol3D.data, na.rm = T)
  map
} 

You can then test this function by executing the following instructions:

library (readbitmap)
library (espadon)

image <- file.choose()

vol.image <- vol.from.image (image)

display.kplane (vol.image, ord.flip = T)
display.kplane (vol.image, col = vol.image$palette, 
                breaks = seq (0.5, length (vol.image$palette) + 0.5), 
                ord.flip = T)