write: Write Analyze/NIfTI Format Binary Image Files

Description Usage Arguments Details Value Author(s) References See Also Examples

Description

This function saves both the header information and multidimensional image array into a pair of binary files in Analyze format or a single binary file in NIfTI format.

Usage

1
2
3
write.analyze.img(fname, hdr, img, type, gzipped=TRUE, warn=-1)
write.nifti.img(fname, hdr, img, type, gzipped=TRUE, warn=-1,
                ignoreQform=FALSE, ignoreSform=FALSE)

Arguments

fname

is the pathname to save the Analzye pair (.img and .hdr) or NIfTI file (.nii) without the suffix.

hdr

is the appropriate header object information.

img

is the multidimensional array of image data.

type

is a character string describing the image format. Valid character strings include:“uint1”, “uint8”, “int16”, “int32”, “float32”, “float64”.

gzipped

is a character string that enables exportation of compressed (.gz) files (default = TRUE).

warn

is a number to regulate the display of warnings (default = -1). See options for more details.

ignoreQform

is a logical variable (default = FALSE) that ignores any ‘qform’ infomration in the NIfTI header.

ignoreSform

is a logical variable (default = FALSE) that ignores any ‘sform’ infomration in the NIfTI header.

Details

Both functions utilize the internal writeBin and writeChar command to write infromation to a binary file.

Current acceptable data types include

“uint1”

DT BINARY (1 bit per voxel)

“uint8”

DT UNSIGNED CHAR (8 bits per voxel)

“int16”

DT SIGNED SHORT (16 bits per voxel)

“int32”

DT SINGED INT (32 bits per voxel)

“float”

DT FLOAT (32 bits per voxel)

“double”

DT DOUBLE (64 bits per voxel)

Value

write.analyze.img and write.nifti.img return nothing.

Author(s)

Brandon Whitcher, Volker Schmid

References

Analyze 7.5
http://www.mayo.edu/bir/PDF/ANALYZE75.pdf

NIfTI-1
http://nifti.nimh.nih.gov/

See Also

read.hdr, read.img

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
norm <- dnorm(seq(-5, 5, length=32), sd=2)
norm <- (norm-min(norm)) / max(norm-min(norm))
img <- outer(outer(norm, norm), norm)
img <- round(255*img)
img[17:32,,] <- 255 - img[17:32,,]
X <- nrow(img)
Y <- ncol(img)
Z <- nsli(img)

## NIfTI
nhdr <- make.hdr(X, Y, Z, 1, "INT", "nifti")
## Not run: 
write.nifti.img("test-image-int16", nhdr, img, "int16")
## These files should be viewable in, for example, FSLview
## Make sure you adjust the min/max values for proper visualization
data <- read.img("test-image-int16.nii.gz")
par(mfrow=c(6,6), mar=rep(0,4))
for (z in 1:32)
  image(img[,,z], zlim=range(img), col=grey(0:255/255), xlab="",
        ylab="", axes=FALSE)
for (z in 1:32)
  image(data[,,z,1], zlim=range(img), col=grey(0:255/255), xlab="",
        ylab="", axes=FALSE)
for (z in 1:32)
  image(abs(data[,,z,1] - img[,,z]), zlim=range(img),
        col=grey(0:255/255), xlab="", ylab="", axes=FALSE)

## End(Not run)

## Loop through all possible data types
datatypes <- c("uint8", "int16", "int32", "float", "double")
equal <- vector("list")
for (x in 1:length(datatypes)) {
  fname <- paste("test-image-", datatypes[x], sep="")
  write.nifti.img(fname, nhdr, img, datatypes[x])
  equal[[x]] <- all(drop(read.img(fname)) == img)
}
names(equal) <- datatypes
unlist(equal)

## Analyze (datatypes are the same as NIfTI)
hdr <- make.hdr(X, Y, Z, 1, "int16")
equal <- vector("list")
for (x in 1:length(datatypes)) {
  fname <- paste("test-analyze-image-", datatypes[x], sep="")
  write.analyze.img(fname, hdr, img, datatypes[x])
  equal[[x]] <- all(drop(read.img(fname)) == img)
}
names(equal) <- datatypes
unlist(equal)

dcemri documentation built on May 2, 2019, 5:27 p.m.