Diff of /R/segment_human.R [000000] .. [242173]

Switch to unified view

a b/R/segment_human.R
1
#' @title Segment Human from CT scans
2
#' @description Segment Human from a non-contrast CT scan
3
#'
4
#' @param img nifti image
5
#' @param adder amount to be added to the image to make non-zero
6
#' @param lthresh lower threshold for the image
7
#' @param verbose Print diagnostic messages
8
#' @param smooth smooth the image using `Perona-Malik` smoother,
9
#' see [extrantsr::oMath]
10
#'
11
#' @return List of smoothed image, body, adder
12
#' @export
13
segment_human = function(
14
    img,
15
    adder = 1025,
16
    lthresh = -300,
17
    verbose = TRUE,
18
    smooth = TRUE
19
) {
20
21
  ##############################
22
  # 1024 should be lower limit
23
  ##############################
24
  if (verbose) {
25
    message("# Adding to Values, usually to make them positive")
26
  }
27
  # reg_img = as.array(reg_img)
28
29
  reg_img = img + adder
30
  if (verbose) {
31
    message("# Setting voxel ranges, 0 to (3071 + adder)")
32
  }
33
  reg_img[reg_img < 0] = 0
34
  reg_img[reg_img > 3071 + adder] = 3071 + adder
35
  # reg_img = as.antsImage(reg_img, reference = img)
36
37
  if (verbose) {
38
    message("# Getting Humans: Smoothing Image")
39
  }
40
  if (smooth) {
41
    ss = extrantsr::oMath(reg_img, "PeronaMalik", 10, 5)
42
  } else {
43
    ss = reg_img
44
  }
45
46
  if (verbose) {
47
    message("# Getting Humans: Largest Component")
48
  }
49
  body = ss > (0 + adder)
50
  body = extrantsr::oMath(img = body, operation = "GetLargestComponent")
51
  inds = neurobase::getEmptyImageDimensions(body)
52
  if (verbose) {
53
    message("# Getting Humans: Dropping Zero Dimensions")
54
  }
55
  ss = neurobase::maskEmptyImageDimensions(
56
    img = ss,
57
    inds = inds,
58
    mask.value = 0)
59
60
  if (verbose) {
61
    message("# Getting Humans: Making Coarse Body")
62
  }
63
  body = ss > (lthresh + adder)
64
  body = extrantsr::oMath(img = body, operation = "GetLargestComponent")
65
66
  L = list(
67
    smoothed = ss,
68
    body = body,
69
    adder = adder)
70
  return(L)
71
}