#============================================================================== # # Project: SharpImage # Module: Crop.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 base script class import ImageToImageScript from ImageToImageScript import * # Add reference and import Resize library clr.AddReference("ManagedITK.Image.Resize") from itk import * class CropScript(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "Crop" Help = """Crops an input image using a rectangular ROI.""" Parameters = """None""" Selection = None # ------------------------------------------------------------------------- def Run(self): """ The entry-point for this script. """ self.Initialise() def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ImageToImageScriptObject.Initialise(self) # Search for inputs pattern = Path.GetFileNameWithoutExtension( self.Input.Name ) self.Renderers = self.GetAllRenderersMatchingPattern( pattern ) # Add region selection actor self.Selection = siRegionSelection( self.Input ) self.Selection.Modified += self.SelectionModifiedHandler self.Selection.MouseDownEvent += self.SelectionMouseDownHandler # Write the inputs used and add the selection to each renderer for renderer in self.Renderers: renderer.AddActorToFront( self.Selection ) name = Path.GetFileName( renderer.Inputs[0].Name ) self.WriteLineToConsole( "Using Input: " + name ) # Give user instructions self.StartedWork() self.WriteLineToConsole( "Edit the region by dragging the control points with the mouse." ) self.WriteLineToConsole( "When finished, double-left-click on the final region." ) self.ParentApplication.SetApplicationAsReady(0) self.ParentApplication.SetApplicationStatusLabel( self.Selection.ToString() ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Setup and run filter self.WriteInputName() filterCrop = itkExtractImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterCrop ) filterCrop.SetInput( self.Input ) region = self.Selection.ToImageRegion() region.Crop( self.Input.LargestPossibleRegion ) filterCrop.ExtractionRegion = region self.WriteLineToConsole( "ExtractionRegion=" + region.ToString() ) filterCrop.UpdateLargestPossibleRegion() filterCrop.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterCrop ) # We must manually add the output region index to the output origin origin = itkPoint(self.Output.Dimension) indexZero = itkIndex(self.Output.Dimension) for i in range(self.Output.Dimension): origin[i] = self.Output.LargestPossibleRegion.Index[i]*self.Output.Spacing[i] + self.Output.Origin[i] region = itkImageRegion( self.Output.LargestPossibleRegion.Size, indexZero ) self.Output.Origin = origin self.Output.SetRegions( region ) # Finish off self.WriteOutputName() self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def SelectionModifiedHandler(self, actor, partial): """ Display the selection details to the user. """ self.ParentApplication.SetApplicationStatusLabel( self.Selection.ToString() ) def SelectionMouseDownHandler(self, sender, e): """ Handle the mouse down event. """ if (e.Button == MouseButtons.Left and e.Clicks == 2): for renderer in self.Renderers: renderer.RemoveActor( self.Selection ) self.DoWork()