[58c332]: / archives / RadETL / R / ImageProcessing.R

Download this file

75 lines (70 with data), 2.4 kB

 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Integrated readDICOM Func...
library(oro.dicom)
readDCM <- function(path, debug = FALSE, view = FALSE) {
if(view) {
if(is.list(path))
files <- as.character(path)
else if(is.vector(path))
files <- path
else
files <- list.files(path = path, full.names = TRUE, include.dirs = FALSE, recursive = FALSE, pattern = "\\.dcm$")
count <- 0
nfiles <- length(files)
headers <- images <- vector("list", nfiles)
if(debug) {
cat(" ", nfiles, "files to be processed by readDCM()", fill = TRUE)
tpb <- txtProgressBar(min = 0, max = nfiles, style = 3)
}
for(i in 1:nfiles) {
if(debug) setTxtProgressBar(tpb, i)
tryCatch({
dcm <- readDICOM(path = files[i])
images[[i]] <- dcm$img
headers[[i]] <- dcm$hdr
count <- count + 1
}, error = function(e) {
errComment <- c("readDICOM func error: ", e)
print(Reduce(pasteNormal, errComment))
})
}
cat("\n", count, "read successes of", nfiles, "files", fill = TRUE)
if(debug) close(tpb)
return(list(hdr = headers, img = images))
} else {
resImg <- tryCatch({
# print(path)
resImg <- readDICOM(path = path, verbose = debug)
}, error = function(e) {
if(debug) {
errComment <- c("readDICOM func error: ", e)
errFile <- c("Change func readDICOMFile: ", path)
print(Reduce(pasteNormal, errComment))
print(Reduce(pasteNormal, errFile))
}
# Retry not parse pixelData function..
resImg <- readDICOMFile(fname = path, pixelData = FALSE)
# assign("resImg", resImg, envir = .GlobalEnv)
})
return(resImg)
}
}
# convertNifti <- function(filePath) {
# files <- list.files(path = filePath, pattern = "\\.dcm$", full.names = TRUE)
# nifList <- NA
#
# for(i in 1:length(files)) {
# tryCatch({
# img <- oro.dicom::readDICOM(path = files[i], recursive = TRUE)
# nif <- oro.dicom::dicom2nifti(dcm = img)
# if(is.na(nifList))
# nifList <- nif
# else if(length(dim(nifList)) == 3)
# nifList <- abind::abind(nifList, nif, along = 1)
# else if(length(dim(nifList)) == 2)
# nifList <- abind::abind(nifList, nif, along = 0)
# }, error = function(e) {
# print(Reduce(pasteNormal, c("Error for ", files[i])))
# })
# }
# return(nifList)
# }