#============================================================================== # # Project: SharpImage # Module: ExtractSlice.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ # Revision: $Revision: 2 $ # # Copyright (c) Queensland University of Technology (QUT) 2007. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the above copyright notices for more information. # #============================================================================== # Import the relevent class import ImageToImageScript from ImageToImageScript import * # Add reference and import Resize library clr.AddReference("ManagedITK.Image.Resize") from itk import * class ExtractSliceScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "ExtractSlice" Help = """Extract a slice from the input 3D image.""" Parameters = """(int) Slice = the index of the slice to extract. (Current) (int) Direction = the axis to extract along (ie. 0=X, 1=Y, 2=Z). (2)""" Slice = None Direction = 2 # ------------------------------------------------------------------------- def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ try: ThreadedScriptObject.Initialise( self ) self.Renderer = self.ParentApplication.CurrentRenderer self.Input = self.Renderer.Inputs[0] except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) ScriptObject.Finalise( self ) def ThreadedDoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on a background thread. """ try: self.StartedWork() # Check that the input is a 3D image if (self.Input.Dimension != 3): raise ApplicationException( "The Input must be a 3D image." ) # Create output self.Output = itkImage.New( self.Input.PixelType, 2 ) # Get the current slice (if required) if (self.Slice == None): self.Slice = self.Renderer.Slice # Write parameters self.WriteInputName() self.WriteLineToConsole( "Slice=" + self.Slice.ToString() ) self.WriteLineToConsole( "Direction=" + self.Direction.ToString() ) # Do actual work filterExtract = itkExtractImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterExtract ) filterExtract.SetInput( self.Input ) filterExtract.ExtractSlice( self.Direction, self.Slice ) filterExtract.UpdateLargestPossibleRegion() filterExtract.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterExtract ) self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )