Last data update: 2014.03.03

R: Image filtration
calcFilterR Documentation

Image filtration

Description

Apply a filter to an image.

Usage

## S4 method for signature 'array'
calcFilter(object, filter, norm.filter = TRUE, bilateral = FALSE, na.rm = FALSE)

## S4 method for signature 'MRIaggr'
calcFilter(object, param, filter, norm.filter = TRUE, bilateral = FALSE,
     na.rm = FALSE, name_newparam = NULL,
	 verbose = optionsMRIaggr("verbose"), update.object = FALSE, overwrite = FALSE)

Arguments

object

an array or an object of class MRIaggr. REQUIRED.

param

the contrast parameter to be filtered. character vector. REQUIRED.

filter

the filter to use. Can be a matrix or an array, or a name indicating which filter should be used. REQUIRED.

norm.filter

should the filtered correspond to a weighted mean over site ? (or a weighted sum). logical.

bilateral

should the influence of each neighbor be ponderated by the difference in signal with the considered observation ? logical.

na.rm

should observations with missing values in their neighbourhood be set to NA ? Otherwise the ponderation is adjusted. logical.

name_newparam

the name of the new parameters. character vector.

verbose

should the execution of the function be traced ? logical.

update.object

should the resulting filtered parameters be stored in object ? logical.

overwrite

if contrast parameters with the same names are already stored in object@data, can they be overwritten ? logical.

Details

ARGUMENTS:
Several types of pre-stored filters are availables and can be called by their name:

  • filter for smoothing purpose (e.g. "3D_G5") : see the details section of initFilter.

  • filter for neighbourhood definition purpose (e.g. "3D_N10") : see the details section of initNeighborhood.

norm.filter should be set to TRUE for gaussian, median or sobel filters to correct edge effects. This lead to weight the neighboring value in order to offset the incomplete neighbourhood.

Value

An list containing :

  • [[res]] : a data.frame containing the coordinates and the filtered parameters.

  • [[filter]] : the name of the filter that has been used. character.

See Also

selectContrast to select the filtered parameter(s). initFilter or codeinitNeighborhood to select pre-stored filters.

Examples

#### 1- array method ####
## load a NIFTI file
path.Pat1 <- system.file(file.path("nifti"), package = "MRIaggr")
nifti.Pat1_DWI_t0 <- readMRI(file.path(path.Pat1, "DWI_t0"), format = "nifti")

## before filtering
graphics::image(nifti.Pat1_DWI_t0[,,1,1])

## after median filtering
niftiF.Pat1_DWI_t0 <- calcFilter(nifti.Pat1_DWI_t0[,,,1], filter = "2D_M3")$res
graphics::image(niftiF.Pat1_DWI_t0[,,1])

#### 2- MRIaggr method ####
## load a MRIaggr object
data("MRIaggr.Pat1_red", package = "MRIaggr")

## compute and allocate filtered parameter to the MRIaggr object
# gaussian filter
calcFilter(MRIaggr.Pat1_red, param = c("T2_FLAIR_t2","DWI_t0","TTP_t0"),
           filter = "2D_G3", bilateral = FALSE, na.rm = FALSE,
		   update.object = TRUE, overwrite = TRUE)
selectParameter(MRIaggr.Pat1_red)

# median filter
calcFilter(MRIaggr.Pat1_red, param = c("T2_FLAIR_t2","DWI_t0","TTP_t0"),
           filter = "2D_M3", na.rm = FALSE, update.object = TRUE, overwrite = TRUE)

## display
par(mfrow = c(2,2), mar = c(2,2,2,2))
multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
          num.main = FALSE, main = "raw",
          num = 1, window = NULL, breaks = c(-100,seq(0, 450),601))
multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2_2D_G3",
          num.main = FALSE, main = "2D_G3",
          num = 1, legend = FALSE, window = NULL, breaks = c(-100,seq(0,450),601))
multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2_2D_M3",
          num.main = FALSE, main = "2D_M3",
          num = 1, legend = FALSE, window = NULL, breaks = c(-100,seq(0, 450),601))

## see the results of the different filters
# G : Gaussian filter
resG <- calcFilter(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
                   filter = "2D_G3")

# M : median filter
resM <- calcFilter(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
                   filter = "2D_M3")

# S : Sobel filter
resS <- calcFilter(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
                  filter = "2D_Sx")

# I
resI.T <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
                  filter = "2D_I3", norm.filter = TRUE)
resI.F <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
                   filter = "2D_I3", norm.filter = FALSE)

# N
resN.T <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
                     filter = "3D_N10", norm.filter = TRUE)
resN.F <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
                     filter = "3D_N10", norm.filter = FALSE)

## display
par(mfrow = c(2,2), mar = rep(2,4), mgp = c(2,0.75,0))
breaks <- seq(-50,500,1)
multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2", num = 3,
             breaks = breaks, window = NULL, legend = FALSE,
             main = "no filtering", num.main = FALSE, main.legend = "")
multiplot(resS$res[,c("i","j","k")], contrast = resS$res[,4],
             num = 1, window = NULL, legend = FALSE,
             palette = "cm.colors", breaks = seq(-100, 100),
             main = "sobelX filtering", num.main = FALSE)
multiplot(resG$res[,c("i","j","k")], contrast = resG$res[,4],
             num = 3, window = NULL, legend = FALSE, breaks = breaks,
             main = "gaussian filtering", num.main = FALSE)
multiplot(resM$res[,c("i","j","k")], contrast = resM$res[,4],
             num = 3, window = NULL, legend = FALSE, breaks = breaks,
             main = "median filtering", num.main = FALSE)

layout(matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE), widths = c(2,1), heights = rep(1,3))
par(mar = rep(2,4), mgp = c(2,0.75,0))
multiplot(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2", num = 3,
             window = NULL, legend = TRUE, main = "raw", num.main = FALSE, main.legend = "")
multiplot(resI.T$res[,c("i","j","k")], contrast = resI.T$res[,4],
             num = 3, window = NULL, legend = TRUE,
             main = "Influence filtering - norm = TRUE", num.main = FALSE)
multiplot(resI.F$res[,c("i","j","k")], contrast = resI.F$res[,4],
             num = 3, window = NULL, legend = TRUE,
             main = "Influence filtering - norm = FALSE", num.main = FALSE)


layout(matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE), widths = c(2,1), heights = rep(1,3))
par(mar = rep(2,4), mgp = c(2,0.75,0))
multiplot(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2", num = 3,
             window = NULL, legend = TRUE, main = "raw", num.main = FALSE, main.legend = "")
multiplot(resN.T$res[,c("i","j","k")], contrast = resN.T$res[,4],
             num = 3, window = NULL, legend = TRUE,
             main = "Neighborhood3D filtering - norm = TRUE", num.main = FALSE)
multiplot(resN.F$res[,c("i","j","k")], contrast = resN.F$res[,4],
             num = 3, window = NULL, legend = TRUE,
             main = "Neighborhood3D filtering - norm = FALSE", num.main = FALSE)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(MRIaggr)
Loading required package: Rcpp
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/MRIaggr/MRIaggr-calcFilter.Rd_%03d_medium.png", width=480, height=480)
> ### Name: calcFilter
> ### Title: Image filtration
> ### Aliases: calcFilter calcFilter,array-method calcFilter,MRIaggr-method
> ### Keywords: methods
> 
> ### ** Examples
> 
> #### 1- array method ####
> ## load a NIFTI file
> path.Pat1 <- system.file(file.path("nifti"), package = "MRIaggr")
> nifti.Pat1_DWI_t0 <- readMRI(file.path(path.Pat1, "DWI_t0"), format = "nifti")
> 
> ## before filtering
> graphics::image(nifti.Pat1_DWI_t0[,,1,1])
> 
> ## after median filtering
> niftiF.Pat1_DWI_t0 <- calcFilter(nifti.Pat1_DWI_t0[,,,1], filter = "2D_M3")$res
> graphics::image(niftiF.Pat1_DWI_t0[,,1])
> 
> #### 2- MRIaggr method ####
> ## load a MRIaggr object
> data("MRIaggr.Pat1_red", package = "MRIaggr")
> 
> ## compute and allocate filtered parameter to the MRIaggr object
> # gaussian filter
> calcFilter(MRIaggr.Pat1_red, param = c("T2_FLAIR_t2","DWI_t0","TTP_t0"),
+            filter = "2D_G3", bilateral = FALSE, na.rm = FALSE,
+ 		   update.object = TRUE, overwrite = TRUE)
T2_FLAIR_t2 DWI_t0 TTP_t0 
allocContrast[MRIaggr] : Cartographies "T2_FLAIR_t2_2D_G3" "DWI_t0_2D_G3" "TTP_t0_2D_G3" 
                         have been allocated 
> selectParameter(MRIaggr.Pat1_red)
 [1] "DWI_t0"            "MASK_DWI_t0"       "MASK_T2_FLAIR_t2" 
 [4] "MTT_t0"            "MTT_t1"            "T1_t0"            
 [7] "T2_FLAIR_t2"       "T2_GRE_t0"         "TTP_t0"           
[10] "TTP_t1"            "i_hemisphere"      "j_hemisphere"     
[13] "hemisphere"        "CSF"               "GM"               
[16] "WM"                "T2_FLAIR_t2_2D_G3" "DWI_t0_2D_G3"     
[19] "TTP_t0_2D_G3"     
> 
> # median filter
> calcFilter(MRIaggr.Pat1_red, param = c("T2_FLAIR_t2","DWI_t0","TTP_t0"),
+            filter = "2D_M3", na.rm = FALSE, update.object = TRUE, overwrite = TRUE)
T2_FLAIR_t2 DWI_t0 TTP_t0 
allocContrast[MRIaggr] : Cartographies "T2_FLAIR_t2_2D_M3" "DWI_t0_2D_M3" "TTP_t0_2D_M3" 
                         have been allocated 
> 
> ## display
> par(mfrow = c(2,2), mar = c(2,2,2,2))
> multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
+           num.main = FALSE, main = "raw",
+           num = 1, window = NULL, breaks = c(-100,seq(0, 450),601))
> multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2_2D_G3",
+           num.main = FALSE, main = "2D_G3",
+           num = 1, legend = FALSE, window = NULL, breaks = c(-100,seq(0,450),601))
> multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2_2D_M3",
+           num.main = FALSE, main = "2D_M3",
+           num = 1, legend = FALSE, window = NULL, breaks = c(-100,seq(0, 450),601))
> 
> ## see the results of the different filters
> # G : Gaussian filter
> resG <- calcFilter(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
+                    filter = "2D_G3")
T2_FLAIR_t2 
> 
> # M : median filter
> resM <- calcFilter(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
+                    filter = "2D_M3")
T2_FLAIR_t2 
> 
> # S : Sobel filter
> resS <- calcFilter(MRIaggr.Pat1_red, param = "T2_FLAIR_t2",
+                   filter = "2D_Sx")
T2_FLAIR_t2 
Warning message:
In .local(object, ...) :
  calcFilter[array] : gradient values may be incorrect at the edges 
set 'na.rm' to TRUE to remove these values 

> 
> # I
> resI.T <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
+                   filter = "2D_I3", norm.filter = TRUE)
MASK_T2_FLAIR_t2 
> resI.F <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
+                    filter = "2D_I3", norm.filter = FALSE)
MASK_T2_FLAIR_t2 
> 
> # N
> resN.T <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
+                      filter = "3D_N10", norm.filter = TRUE)
MASK_T2_FLAIR_t2 
> resN.F <- calcFilter(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2",
+                      filter = "3D_N10", norm.filter = FALSE)
MASK_T2_FLAIR_t2 
> 
> ## display
> par(mfrow = c(2,2), mar = rep(2,4), mgp = c(2,0.75,0))
> breaks <- seq(-50,500,1)
> multiplot(MRIaggr.Pat1_red, param = "T2_FLAIR_t2", num = 3,
+              breaks = breaks, window = NULL, legend = FALSE,
+              main = "no filtering", num.main = FALSE, main.legend = "")
> multiplot(resS$res[,c("i","j","k")], contrast = resS$res[,4],
+              num = 1, window = NULL, legend = FALSE,
+              palette = "cm.colors", breaks = seq(-100, 100),
+              main = "sobelX filtering", num.main = FALSE)
> multiplot(resG$res[,c("i","j","k")], contrast = resG$res[,4],
+              num = 3, window = NULL, legend = FALSE, breaks = breaks,
+              main = "gaussian filtering", num.main = FALSE)
> multiplot(resM$res[,c("i","j","k")], contrast = resM$res[,4],
+              num = 3, window = NULL, legend = FALSE, breaks = breaks,
+              main = "median filtering", num.main = FALSE)
> 
> layout(matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE), widths = c(2,1), heights = rep(1,3))
> par(mar = rep(2,4), mgp = c(2,0.75,0))
> multiplot(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2", num = 3,
+              window = NULL, legend = TRUE, main = "raw", num.main = FALSE, main.legend = "")
> multiplot(resI.T$res[,c("i","j","k")], contrast = resI.T$res[,4],
+              num = 3, window = NULL, legend = TRUE,
+              main = "Influence filtering - norm = TRUE", num.main = FALSE)
> multiplot(resI.F$res[,c("i","j","k")], contrast = resI.F$res[,4],
+              num = 3, window = NULL, legend = TRUE,
+              main = "Influence filtering - norm = FALSE", num.main = FALSE)
> 
> 
> layout(matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE), widths = c(2,1), heights = rep(1,3))
> par(mar = rep(2,4), mgp = c(2,0.75,0))
> multiplot(MRIaggr.Pat1_red, param = "MASK_T2_FLAIR_t2", num = 3,
+              window = NULL, legend = TRUE, main = "raw", num.main = FALSE, main.legend = "")
> multiplot(resN.T$res[,c("i","j","k")], contrast = resN.T$res[,4],
+              num = 3, window = NULL, legend = TRUE,
+              main = "Neighborhood3D filtering - norm = TRUE", num.main = FALSE)
> multiplot(resN.F$res[,c("i","j","k")], contrast = resN.F$res[,4],
+              num = 3, window = NULL, legend = TRUE,
+              main = "Neighborhood3D filtering - norm = FALSE", num.main = FALSE)
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>