0

I am doing a project about brain tumor segmentation. And when I apply N4BiasCorrection to my file(.mha), I used slicer and simpleITK methods. Slicer performs well but is time-consuming because I do not know how to use code to run through all my file, I just use the Slicer-N4ITK module and process each file by hand.

Then I try the simpleITK with python, problems show up. First, it runs very slow on each .mha file and gets a really big file(36.7MB compare with 4.4MB using Slicer) after applying n4biasfieldcorrection. Second, in order to speed up, I set the Shrink parameter to 4 but the whole .mha file becomes really blurred, which will not happen using slicer.

So can anyone tell me whether it is normal ? are there any methods to speed up without blurring my file? Or could you please tell me an example to apply N4BiasFieldCorrection within Slicer python interactor .

Thanks!!

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
from __future__ import print_function
import SimpleITK as sitk
import sys
import os
#from skimage import io
from glob import glob
import numpy as np


def n4process(inputimage, outpath):
    inputImage = sitk.ReadImage( inputimage )    
#    numberFilltingLevels = 4
    maskImage = sitk.OtsuThreshold( inputImage, 0, 1, 200 )
#    inputImage = sitk.Shrink( inputImage, [ 2 ] * inputImage.GetDimension() )
#    maskImage = sitk.Shrink( maskImage, [ 2 ] * inputImage.GetDimension() )
    inputImage = sitk.Cast( inputImage, sitk.sitkFloat32 )
    corrector = sitk.N4BiasFieldCorrectionImageFilter();
    corrector.SetConvergenceThreshold=0.001
    corrector.SetBiasFieldFullWidthAtHalfMaximum=0.15
    corrector.SetMaximumNumberOfIterations=50
    corrector.SetNumberOfControlPoints=4
    corrector.SetNumberOfHistogramBins=200
    corrector.SetSplineOrder=3
    corrector.SetWienerFilterNoise=0.1
    output = corrector.Execute( inputImage,maskImage )    
    sitk.WriteImage( output, outpath )

    
input_path = '/Users/chenrui/Desktop/BRATS2015_Training/HGG/'
patientpath = glob('/Users/chenrui/Desktop/BRATS2015_Training/HGG/*')
num = 0
for i in patientpath:
    num = num+1
#i = '/Users/chenrui/Desktop/BRATS2015_Training/HGG/brats_2013_pat0001_1'
    flair = glob(i + '/*Flair*/*.mha')
    flair_outpath = '/Users/chenrui/Desktop/BRATS2015_Training/test/'+'Flair/'+str(num)+'.mha'
    n4process(flair[0], flair_outpath)
    
    t2 = glob(i + '/*T2*/*.mha')
    t2_outpath = '/Users/chenrui/Desktop/BRATS2015_Training/HGG_n4/'+'T2/'+str(num)+'.mha'
    n4process(t2[0], t2_outpath)
    
    t1c = glob(i + '/*_T1c*/*.mha')
    t1c_outpath = '/Users/chenrui/Desktop/BRATS2015_Training/HGG_n4/'+'T1c/'+str(num)+'.mha'
    n4process(t1c[0], t1c_outpath)
    
    t1 = glob(i + '/*_T1*/*.mha')
    t1 = [scan for scan in t1 if scan not in t1c]
    t1_outpath = '/Users/chenrui/Desktop/BRATS2015_Training/HGG_n4/'+'T1/'+str(num)+'.mha'
    n4process(t1[0],t1_outpath)
Chen
  • 1

1 Answers1

0

From a look at the original implementation http://www.insight-journal.org/browse/publication/640

You can download this and generate the example to then test on your data. The parameters you set appear to be the same as defined in the defaults except for WeinerFilterNoise which should be 0.01 unless you've changed this for a reason - is this the blurring issue?

The size differential (x 8 increase) will be that you've probably saved out the data from 8bit to 64 bit or something. Checking the metaimage header will show this. This can be resolved with casting.

g.stevo
  • 745
  • 3
  • 10
  • Thank you very much for your reply. And yes, when I add a Cast to the output to int16, the whole size decreased to 17.9M. And I notice the code downloaded from the link is written in C++.Maybe I need to spend more time to figure out how to use them in OS platform. Some problems still remained ,like it runs very slowly and the result is blurred even after I change the parameter for WeinerFilterNoise to 0.01. – Chen Apr 04 '17 at 02:14