Takes an image, masks it by mask, and returns an object of class nifti

mask_img(img, mask, allow.NA = TRUE)

Arguments

img

object of class nifti

mask

array or object of class nifti, same dimensions as img

allow.NA

allow NAs in the mask

Value

Object of class nifti with values outside mask set to 0 if mask is 0 and NA if mask is NA and img if mask == 1

Examples

  set.seed(5)
  dims = rep(10, 3)
  arr = array(rnorm(prod(dims)), dim = dims)
  nim = oro.nifti::nifti(arr)
  mask = abs(nim) > 1
  masked = mask_img(nim, mask)
  mask[mask == 0] = NA
  na_masked = mask_img(nim, mask, allow.NA = TRUE)
  
set.seed(5)
dims = rep(10, 3)
arr = array(rnorm(prod(dims)), dim = dims)
arr[,,10] = 0
nim = oro.nifti::nifti(arr)
rnifti = RNifti::asNifti(nim)
mask = nim > 0
timg = tempimg(nim)
limg = list(factor(timg), factor(timg))
func = function(...) mask_img(..., mask = mask)
func(arr)
#> , , 1
#> 
#>             [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 0.00000000 1.2276303 0.9005119 0.3159150 1.5500604 1.4632486 0.0000000
#>  [2,] 1.38435934 0.0000000 0.9418694 1.1096942 0.0000000 0.1877261 0.8548304
#>  [3,] 0.00000000 0.0000000 1.4679619 2.2154606 0.0000000 1.0220229 0.0000000
#>  [4,] 0.07014277 0.0000000 0.7067611 1.2171036 1.8956680 0.0000000 0.4963615
#>  [5,] 1.71144087 0.0000000 0.8190089 1.4792218 0.0000000 0.0000000 0.0000000
#>  [6,] 0.00000000 0.0000000 0.0000000 0.9515738 0.5622234 0.0000000 0.0000000
#>  [7,] 0.00000000 0.0000000 1.4185891 0.0000000 0.0000000 0.7533048 0.0000000
#>  [8,] 0.00000000 0.0000000 1.4987738 0.0000000 0.0000000 0.0000000 0.0000000
#>  [9,] 0.00000000 0.2408173 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#> [10,] 0.13810822 0.0000000 0.0000000 0.0000000 0.0000000 0.2332753 0.0000000
#>             [,8]      [,9]     [,10]
#>  [1,] 0.00000000 0.4984162 0.9260471
#>  [2,] 0.00000000 0.0000000 0.0000000
#>  [3,] 0.34702845 0.9755291 0.5570339
#>  [4,] 0.03236784 0.0000000 0.9007306
#>  [5,] 0.41353129 0.6756845 0.9899457
#>  [6,] 0.00000000 0.0000000 0.3836081
#>  [7,] 0.97348539 2.3872326 0.0000000
#>  [8,] 0.12109014 0.0000000 0.0000000
#>  [9,] 0.18917369 0.0000000 0.0000000
#> [10,] 0.00000000 0.0000000 0.0000000
#> 
#> , , 2
#> 
#>            [,1]       [,2]      [,3]      [,4]       [,5]      [,6]       [,7]
#>  [1,] 0.0000000 0.00000000 1.1708564 1.3485491 0.00000000 0.9460885 0.00000000
#>  [2,] 1.1353113 1.62237939 0.8848449 0.0000000 0.00000000 0.7517709 2.24625462
#>  [3,] 0.6757946 2.60014202 0.0000000 0.0000000 2.18023570 0.0000000 0.09343168
#>  [4,] 0.2084833 0.13964851 0.0000000 0.0000000 0.95741847 0.8083360 1.62728009
#>  [5,] 0.0000000 0.00000000 1.0592504 0.7329730 0.00000000 0.0000000 0.00000000
#>  [6,] 0.8938114 0.79893102 0.2900836 0.4556796 0.00000000 1.2382589 0.00000000
#>  [7,] 0.0000000 0.00000000 0.0000000 0.2880795 0.09995405 0.0000000 0.00000000
#>  [8,] 0.0000000 0.46372006 1.2430958 0.0000000 0.00000000 1.1963664 0.00000000
#>  [9,] 0.0000000 0.05242956 0.0000000 0.6487425 0.00000000 0.0000000 0.00000000
#> [10,] 1.2801516 0.00000000 0.0000000 0.2991623 0.00000000 0.1861149 0.00000000
#>            [,8]      [,9]      [,10]
#>  [1,] 0.0000000 2.1816467 0.66753870
#>  [2,] 0.1437689 0.0000000 0.36623695
#>  [3,] 0.0000000 0.7500592 0.00000000
#>  [4,] 0.0000000 0.9743826 0.45056824
#>  [5,] 0.0000000 0.0000000 0.00000000
#>  [6,] 0.3320349 0.0000000 1.33906937
#>  [7,] 1.5722883 0.0000000 0.81621918
#>  [8,] 0.0000000 0.0000000 0.08220176
#>  [9,] 0.9162865 0.7405880 0.00000000
#> [10,] 0.0000000 0.0000000 0.72640902
#> 
#> , , 3
#> 
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]       [,7]
#>  [1,] 0.0000000 1.2185331 0.2108329 0.0000000 0.3043872 0.0000000 0.00000000
#>  [2,] 0.0000000 0.6291344 0.0000000 0.9776881 0.0000000 0.0000000 0.09378492
#>  [3,] 0.9891685 0.5277463 2.0251970 0.0000000 0.0000000 0.1641560 0.98234922
#>  [4,] 0.0000000 0.0000000 0.0000000 0.0000000 1.4709389 0.0000000 0.00000000
#>  [5,] 0.2758983 0.8237152 0.0000000 0.0000000 0.0000000 0.0000000 0.07480485
#>  [6,] 0.4107816 0.0000000 0.0000000 1.8976216 0.0000000 0.0000000 2.19742943
#>  [7,] 0.6111832 0.0000000 0.0000000 1.3955407 0.4605406 0.0000000 0.79502321
#>  [8,] 0.9365707 1.4187830 0.0000000 0.0000000 0.0000000 0.0000000 0.00000000
#>  [9,] 0.0000000 0.4871339 0.0000000 0.0000000 0.0000000 0.7783246 0.00000000
#> [10,] 0.7403768 0.6034415 0.0000000 1.1696782 0.4090189 0.0000000 0.00000000
#>              [,8]       [,9]      [,10]
#>  [1,] 0.000000000 0.12515439 0.00000000
#>  [2,] 0.000000000 0.00000000 0.68259291
#>  [3,] 0.000000000 0.00000000 0.52413002
#>  [4,] 1.465324083 0.96691960 0.08054998
#>  [5,] 1.858615318 0.00000000 0.05378554
#>  [6,] 0.003497027 0.00000000 0.00000000
#>  [7,] 0.000000000 1.39995727 0.96643806
#>  [8,] 0.151294033 0.01701396 0.98430685
#>  [9,] 0.290009125 1.84749695 0.18729094
#> [10,] 0.000000000 0.00000000 0.27298575
#> 
#> , , 4
#> 
#>            [,1]       [,2]      [,3]       [,4]      [,5]      [,6]     [,7]
#>  [1,] 1.2101461 0.00000000 0.0000000 0.00000000 0.0000000 0.0000000 0.000000
#>  [2,] 0.1886572 1.23863449 1.3746533 0.00000000 0.0000000 0.2606761 2.029597
#>  [3,] 1.9624987 0.76743575 0.2918957 0.00000000 0.0000000 0.9455668 0.429106
#>  [4,] 0.1387119 0.00000000 0.7105917 0.00000000 0.8250096 0.0000000 1.063948
#>  [5,] 0.0000000 0.06750377 0.0000000 0.47233416 0.0000000 0.0000000 0.000000
#>  [6,] 0.0000000 1.60514057 0.0000000 0.00000000 0.0000000 0.5202254 1.074373
#>  [7,] 1.2243539 1.23222943 0.6343720 0.00000000 0.0000000 1.8062587 0.000000
#>  [8,] 0.0000000 0.00000000 0.0000000 0.07056287 0.0000000 0.0000000 0.000000
#>  [9,] 0.0000000 0.00000000 0.0000000 0.46409319 0.1385864 0.1992821 0.499760
#> [10,] 0.5604792 0.36491810 0.0000000 0.28915850 1.1835716 0.2764850 0.000000
#>            [,8]      [,9]     [,10]
#>  [1,] 0.0898948 2.1160472 0.0000000
#>  [2,] 1.0049101 1.1886396 0.2013111
#>  [3,] 0.0000000 0.8952612 0.0000000
#>  [4,] 0.5247845 0.8685406 0.7291357
#>  [5,] 0.0000000 1.6561780 0.3266486
#>  [6,] 1.2109326 1.4456360 0.0000000
#>  [7,] 0.0000000 0.7446004 0.0000000
#>  [8,] 1.0906257 0.0000000 0.5045437
#>  [9,] 0.4918449 0.0000000 0.0000000
#> [10,] 0.0000000 0.0000000 0.0000000
#> 
#> , , 5
#> 
#>            [,1]       [,2]      [,3]      [,4]        [,5]      [,6]      [,7]
#>  [1,] 0.0000000 1.03459813 0.0000000 0.0000000 0.000000000 0.5410829 0.0000000
#>  [2,] 0.4693224 0.00000000 1.8041418 0.0000000 0.000000000 0.0000000 0.0000000
#>  [3,] 0.0000000 0.00000000 0.4446371 0.2665382 0.005989837 0.0000000 0.4569621
#>  [4,] 0.0000000 0.67475583 0.0000000 0.2915323 0.855006991 0.0000000 0.0000000
#>  [5,] 0.4579751 0.00000000 0.2482291 1.7089224 0.729851453 0.0000000 1.9339075
#>  [6,] 0.0000000 2.24549141 0.0000000 0.4767018 0.348758873 0.0000000 0.5695607
#>  [7,] 0.0000000 0.00000000 0.6869890 1.3075518 1.361367470 0.0000000 0.9327798
#>  [8,] 2.0068609 0.19487458 0.0000000 1.8060127 0.000000000 0.6715357 0.0000000
#>  [9,] 0.0000000 0.03149807 0.5753220 1.0353860 1.888403664 0.0000000 0.6016184
#> [10,] 0.0000000 0.00000000 0.6377015 0.5165183 0.000000000 0.4225978 0.0000000
#>             [,8]       [,9]      [,10]
#>  [1,] 1.36036594 0.07671589 0.00000000
#>  [2,] 0.00000000 0.00000000 0.00000000
#>  [3,] 0.00000000 0.00000000 0.00000000
#>  [4,] 0.00000000 0.11760616 0.00000000
#>  [5,] 0.00000000 0.00000000 1.61094670
#>  [6,] 1.19039680 0.00000000 0.01851833
#>  [7,] 0.05119609 0.00000000 0.00000000
#>  [8,] 0.33104803 0.00000000 1.28944976
#>  [9,] 0.53161910 0.75793379 0.00000000
#> [10,] 0.61403147 0.35956026 0.00000000
#> 
#> , , 6
#> 
#>              [,1]      [,2]      [,3]       [,4]      [,5]       [,6]
#>  [1,] 0.000000000 0.8215494 0.0000000 1.88523498 0.0000000 0.00000000
#>  [2,] 0.199059827 0.0000000 0.0000000 0.84319900 0.4204740 0.09469637
#>  [3,] 1.229825830 1.0431180 3.4018720 0.33453392 0.0000000 0.76213934
#>  [4,] 0.004552531 0.0000000 0.0000000 0.02038547 0.8685939 2.36720359
#>  [5,] 0.000000000 0.6909233 1.7291981 0.00000000 1.1501575 0.00000000
#>  [6,] 0.000000000 0.3686056 0.0000000 0.41269396 0.0176960 0.06709804
#>  [7,] 1.739481988 0.6485134 0.5336196 0.00000000 0.0000000 0.15529692
#>  [8,] 1.023020955 0.0000000 0.0000000 0.92499188 0.0000000 0.00000000
#>  [9,] 0.000000000 0.0000000 0.0000000 0.30914945 1.2257332 0.00000000
#> [10,] 0.845996179 0.8653006 0.2734275 1.96684536 1.2103777 0.60055463
#>             [,7]      [,8]       [,9]     [,10]
#>  [1,] 0.08534008 0.4672982 0.00000000 0.0000000
#>  [2,] 0.00000000 0.5538269 0.00000000 1.4915981
#>  [3,] 0.00000000 0.0000000 1.01498892 0.1720902
#>  [4,] 0.00000000 0.0000000 0.00000000 0.1911603
#>  [5,] 0.40628102 0.0000000 0.03821585 0.0000000
#>  [6,] 0.50904587 0.0000000 0.18407867 0.7750196
#>  [7,] 1.58667108 0.0000000 0.45172259 0.0000000
#>  [8,] 1.04631178 0.4127632 0.30788006 0.0000000
#>  [9,] 0.00000000 0.0000000 0.28485823 0.0000000
#> [10,] 0.00000000 0.0000000 0.66455104 0.0000000
#> 
#> , , 7
#> 
#>            [,1]       [,2]      [,3]       [,4]      [,5]      [,6]      [,7]
#>  [1,] 1.3294962 0.00000000 0.0000000 0.00000000 0.0000000 0.0000000 0.2072982
#>  [2,] 0.9897694 1.78420320 0.1119540 0.00000000 1.7241555 0.0000000 0.0000000
#>  [3,] 1.6645658 0.00000000 1.5142314 0.37369104 0.2045693 0.0000000 0.0000000
#>  [4,] 0.0000000 0.00000000 0.3765598 0.98053555 1.2378090 0.0000000 0.0000000
#>  [5,] 0.1414808 0.00000000 1.2972814 2.28129142 0.2429637 0.3132652 0.6383485
#>  [6,] 0.0000000 0.00000000 2.5623566 0.53232497 1.0135437 0.0000000 0.8129250
#>  [7,] 0.2978839 0.01377361 0.0000000 0.49883079 1.2824897 0.0000000 0.0000000
#>  [8,] 1.1394333 1.32008871 0.8006673 0.15410050 0.2892092 0.3944052 0.0000000
#>  [9,] 0.1267375 0.48927367 0.0000000 0.08277325 0.0000000 0.5387158 0.8241279
#> [10,] 0.0000000 0.00000000 1.3658775 0.00000000 2.2685937 2.1372920 0.0000000
#>            [,8]      [,9]     [,10]
#>  [1,] 0.8732661 0.0000000 0.4104312
#>  [2,] 0.6897053 0.2533336 0.5518676
#>  [3,] 0.0000000 0.0000000 0.0000000
#>  [4,] 0.6541665 0.0000000 0.0000000
#>  [5,] 1.4000088 0.0000000 0.0000000
#>  [6,] 1.0024323 2.7242070 0.2083771
#>  [7,] 0.0000000 0.0000000 2.3648680
#>  [8,] 0.0000000 1.6514171 1.6055654
#>  [9,] 1.8518099 2.6095467 1.6538888
#> [10,] 0.0000000 0.0000000 2.2142716
#> 
#> , , 8
#> 
#>            [,1]      [,2]      [,3]      [,4]      [,5]        [,6]      [,7]
#>  [1,] 0.0000000 0.4342333 0.0000000 0.5136547 0.2445857 0.024357435 1.5808633
#>  [2,] 0.5633412 0.2961612 0.0000000 0.3397094 0.0000000 0.000000000 0.0000000
#>  [3,] 1.1800894 0.0000000 0.4223030 0.0000000 0.0000000 0.009875067 0.3268752
#>  [4,] 0.0000000 0.0000000 0.1233707 0.4451025 0.0000000 0.000000000 0.0000000
#>  [5,] 0.0000000 0.9229221 0.0000000 1.3680193 1.5420985 0.690627111 0.8980972
#>  [6,] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.461008072 0.0000000
#>  [7,] 0.0000000 0.4600906 0.0000000 0.1913186 0.0000000 0.565864491 0.1412008
#>  [8,] 0.5643141 0.0000000 0.8969854 1.4468181 0.3758670 0.000000000 0.0000000
#>  [9,] 0.7181216 1.3230111 1.3509294 0.0000000 0.0000000 0.494492116 0.0000000
#> [10,] 0.0000000 0.2853028 0.0000000 0.6153732 0.2447926 0.000000000 0.0000000
#>            [,8]       [,9]     [,10]
#>  [1,] 0.0000000 0.98731340 0.4150048
#>  [2,] 0.3031691 0.00000000 0.3540266
#>  [3,] 0.6137637 0.00000000 0.0000000
#>  [4,] 0.7486776 0.00000000 0.0000000
#>  [5,] 0.0000000 0.20625589 0.0000000
#>  [6,] 0.7169483 0.45214256 0.0000000
#>  [7,] 1.0791184 0.66605262 0.0000000
#>  [8,] 0.0000000 0.00000000 0.9263556
#>  [9,] 0.7296384 0.02512841 0.0000000
#> [10,] 0.2099456 0.55199547 1.2660778
#> 
#> , , 9
#> 
#>             [,1]       [,2]      [,3]       [,4]      [,5]      [,6]      [,7]
#>  [1,] 0.00000000 0.04836372 1.2651925 0.71004247 0.0000000 0.1551941 0.0000000
#>  [2,] 0.00000000 0.00000000 1.1552432 2.07437921 0.0000000 1.2495368 0.2675964
#>  [3,] 1.44336433 0.00000000 1.6734299 0.00000000 1.5000586 0.4968856 0.3137876
#>  [4,] 0.00000000 0.00000000 0.6296572 0.00000000 0.0000000 0.0000000 0.9684232
#>  [5,] 0.00000000 0.00000000 0.0000000 0.00000000 0.8536252 0.0000000 0.0000000
#>  [6,] 0.80979975 1.02394779 0.7989220 0.41789122 0.0000000 0.3575519 0.0000000
#>  [7,] 0.00000000 1.08235408 1.2099618 0.13196928 0.0000000 1.0833902 0.9528907
#>  [8,] 0.00000000 0.55476813 0.0000000 0.49800750 0.0000000 0.8411405 0.7157880
#>  [9,] 1.86129327 1.58677996 1.1828370 0.21705236 0.5452946 0.0000000 0.0000000
#> [10,] 0.05318576 0.25886095 0.0000000 0.05370814 0.2174415 0.0000000 0.0000000
#>             [,8]      [,9]     [,10]
#>  [1,] 0.00000000 0.2450356 0.0000000
#>  [2,] 0.51296587 0.0000000 0.0000000
#>  [3,] 0.04013688 0.3065782 1.1075260
#>  [4,] 0.69473425 0.2167695 0.0000000
#>  [5,] 0.84025170 0.0000000 1.2256126
#>  [6,] 0.00000000 0.0000000 0.0000000
#>  [7,] 0.76272563 0.0000000 0.0000000
#>  [8,] 1.19746787 0.6900559 0.0000000
#>  [9,] 0.00000000 2.1973152 0.0000000
#> [10,] 0.00000000 0.0000000 0.8624957
#> 
#> , , 10
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    0    0    0    0    0    0    0    0     0
#>  [2,]    0    0    0    0    0    0    0    0    0     0
#>  [3,]    0    0    0    0    0    0    0    0    0     0
#>  [4,]    0    0    0    0    0    0    0    0    0     0
#>  [5,]    0    0    0    0    0    0    0    0    0     0
#>  [6,]    0    0    0    0    0    0    0    0    0     0
#>  [7,]    0    0    0    0    0    0    0    0    0     0
#>  [8,]    0    0    0    0    0    0    0    0    0     0
#>  [9,]    0    0    0    0    0    0    0    0    0     0
#> [10,]    0    0    0    0    0    0    0    0    0     0
#> 
func(nim)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
func(rnifti)
#> Image array of mode "integer" (5.2 Kb)
#> - 10 x 10 x 10 voxels
#> - 1 x 1 x 1 per voxel
func(timg)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
lapply(limg, func)
#> [[1]]
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
#> 
#> [[2]]
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
#>