#!/usr/bin/env python # coding: utf-8 # In[1]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') import SimpleITK as sitk print( sitk.Version() ) # Download data to work on from downloaddata import fetch_midas_data as fdata from myshow import myshow from __future__ import print_function # #Demonstrate Reporting of Progress With ConnectedThresholdImageFilter # In[2]: img = sitk.Image(100,100, sitk.sitkUInt8) # In[3]: ctFilter = sitk.ConnectedThresholdImageFilter() ctFilter.SetSeed([0,0]) ctFilter.SetUpper(1) ctFilter.SetLower(0) ctFilter.AddCommand(sitk.sitkProgressEvent, lambda: print("\rProgress: {0:03.1f}%...".format(100*ctFilter.GetProgress()))) # Demonstrate reporting of Progress # In[4]: ctFilter.Execute(img) # # Performance comparison of Median and Rank image fitlers # In[5]: img = sitk.Image(100,100,100,sitk.sitkFloat32) img = sitk.AdditiveGaussianNoise(img) myshow(img) radius = [3,3,3] # In[6]: get_ipython().run_line_magic('timeit', '-n 1 sitk.Median(img,radius=radius)') # In[7]: get_ipython().run_line_magic('timeit', '-n 1 sitk.FastApproximateRank(img, rank=0.5, radius=radius)') # In[8]: get_ipython().run_line_magic('timeit', '-n 1 sitk.Rank(img, rank=0.5, radius=radius)') # In[9]: get_ipython().run_line_magic('timeit', '-n 1 sitk.Mean(img,radius=radius)') # # Compare Binary and GrayScale Dilate # In[10]: img = sitk.ReadImage(fdata("cthead1.png")) myshow(img) # First create a simple binary image (0 and 1s) for the common cthead1.png test data set bimg = img >100 myshow(bimg) # Visually show that for binary images the output will be the same. # In[11]: myshow(sitk.BinaryDilate(bimg, radius, sitk.sitkBall), title="BinaryDilate") myshow(sitk.GrayscaleDilate(bimg, radius, sitk.sitkBall), title="GrayscaleDilate") # Breifly show that there is a performance difference. These images are only 2D an small to it may not be very accurate, but the filters use different algoritms and the GrayscaleDilate changes which algorithm based on the structuring element. # In[12]: get_ipython().run_line_magic('timeit', '-n 100 sitk.BinaryDilate(bimg, 10, sitk.sitkBall)') get_ipython().run_line_magic('timeit', '-n 100 sitk.GrayscaleDilate(bimg, 10, sitk.sitkBall)') # In[13]: get_ipython().run_line_magic('timeit', '-n 100 sitk.BinaryDilate(bimg, 10, sitk.sitkBox)') get_ipython().run_line_magic('timeit', '-n 100 sitk.GrayscaleDilate(bimg, 10, sitk.sitkBox)') # In[ ]: