SCI Seg3D Mailing List

Text archives Help


Re: [Seg3D] Contact CIBC Submission


Chronological Thread 
  • From: Jess <jess@sci.utah.edu>
  • To: kelly.nicol@icahn.mssm.edu, seg3d@sci.utah.edu
  • Cc: Rob MacLeod <macleod@sci.utah.edu>
  • Subject: Re: [Seg3D] Contact CIBC Submission
  • Date: Fri, 21 Apr 2017 12:43:30 -0600

Hi Kelly,

Writing scripts for Seg3D there are a couple things that should help. 

 First, in the newest release candidate (https://github.com/SCIInstitute/Seg3D/releases) there is a new feature which you can copy the python functions from the controller window.  This means that you can load an example data, perform the steps that you want, then copy the functions from the controller window and into a script file.  If you are using an older version, you can still use the controller window to get the functions and parameters that you use, but you will need to convert it to python syntax, which isn’t too similar.  

The second aspect that you need to consider is that the python scheduler doesn’t necessarily wait for the the tools to finish before attempting the next on.  Therefore, you need a wait function to allow the filters to run in series.  There is an example attached that has the wait function.

Because of the need for a wait function, I usually make scripts in the form of a function or a library.  In the attached example, there is a function that loads the data (dicom format in this case), and some other things.  It also calls the second function, which performs a series of filters and tools similar to what you want to do. You can run this example with the following functions in the python console window (it uses data from the IBBM example data, and there is a similar data set in the SCIRun dataset):

path = ‘[path to IBBM dir]/2016/Image_Processing/Lab2/'
exec(open('[path to IBBM dir]/2016/Python_Labs/Lab1_Seg3D/scripts/autoseg_heart.py').read())
AutoSeg_file(path+'DICOM/',path,'seg_auto', [29877, 2121],[[-2.81,5.61,35.8]])

You can use this example, and modify the second function to fit your needs.  It will be something like this:

def AutoSeg(layer,orig,sz,seed):
  #crop with parameters orig and sz
  cr_im=crop(layerids=layer,origin=orig,size=size,replace=‘false’);
  wait_for_layer(cr_im)
  
  #filter with median filter radius 1
  sm_im = medianfilter(layerid = cr_im,radius=1)
  wait_for_layer(sm_im)
  
  #otsu threshold gives multiple outputs
  o_ims=otsuthresholdfilter(layerid=sm_im,amount='3’)
  wait_for_layer(o_ims[0])

  # fill holes on third otsu output
  ff_im=fillholesfilter(layerid=o_ims[2],seeds=seed,replace='false')
  wait_for_layer(ff_im)

  return ff_im

the parameters from the parent function should look like:
orig = '[-78.3714,-74.658,6.01]'
sz='[-78.3714,-74.658,6.01]
seed = [[-2.81,5.61,35.8]]

These examples should hopefully get you started.  There are some more python script examples in the IBBM data, and at 

Please let me know if you have more questions.  


cheers,
Jess


import os
import threading

def AutoSeg_file(Filepath, outputfilename,name, thresh,seed):
# runs topical denoising on an image
  if not os.path.exists(Filepath):
    raise ValueError("Path %s does not exist." % Filename)
  
  files = os.listdir(Filepath)

  Filenames=[]

  for f in files:
    Filenames.append(Filepath+f)



#load data
  im_layer = importseries(filenames=Filenames, importer='[Teem Importer]', mode='data')
  wait_for_layer(im_layer[0])


  sm_seg=AutoSeg(im_layer[0],thresh,seed)

  # set name
  set(stateid=sm_seg+'::name',value=name)
  
  #save seg
  result=exportsegmentation(layers=sm_seg,file_path=outputfilename ,exporter='[NRRD Exporter]',extension='.nrrd')



def AutoSeg(layer,thresh,seed):

#smooth image
  sm_im = medianfilter(layerid = layer,radius=1)
  wait_for_layer(sm_im)
#threshold image
  thresh_im=threshold(layerid = sm_im, lower_threshold = thresh[0], upper_threshold = thresh[1])
  wait_for_layer(thresh_im)
  #connected componten
  cc_im = connectedcomponentfilter(layerid = thresh_im, seeds = seed)
  wait_for_layer(cc_im)
# smooth segmenation
  sm_seg_1 = dilateerodefilter(layerid=cc_im,dilate_radius=0, erode_radius = 1)
  wait_for_layer(sm_seg_1)
  sm_seg = dilateerodefilter(layerid=sm_seg_1,dilate_radius=2, erode_radius = 1)
  wait_for_layer(sm_seg)
  
  computeisosurface(layerid=sm_seg)

  return sm_seg


def wait_for_layer(layer):

#checks to make sure that the layer is available before trying operations on it.

  MAX_ITERATIONS=1000
  TIMEOUT=2
  c = threading.Condition()
  c.acquire()
    
  layerStatus = get(stateid=layer+"::data")
  counter = 1
  with c:
    while not layerStatus == "available":
      if counter > MAX_ITERATIONS:
        break
      counter += 1
      c.wait(TIMEOUT)
      layerStatus = get(stateid=layer+"::data")
      print('waiting for '+layer)






On Apr 20, 2017, at 3:43 PM, SCI Webmaster <webmaster@sci.utah.edu> wrote:

Kelly Nicol <kelly.nicol@icahn.mssm.edu>

Affiliation: Mount Sinai - Dept Neurosurgery

Re Software: {Software:value}

Subject: Python Console

Message: Hi there!
I attended IBBM last year. You showed us how to semi-automate our segmentations in Seg3D with this tool. I can only code for the command line and was wondering if you could help me code 4 steps for some cardiac CT scans I'm working on. (1) cropping (2) median filtering radius =1 (3) 3 part otsu threshold (4) fill holes filter
Shouldnt be too bad, but could really use your help! Thanks...Kelly





Archive powered by MHonArc 2.6.18.

Top of page