Medical Imaging Interaction Toolkit  2023.12.00
Medical Imaging Interaction Toolkit
mitk::SegmentAnythingTool Class Reference

Segment Anything Model interactive 2D tool class. More...

#include <mitkSegmentAnythingTool.h>

Inheritance diagram for mitk::SegmentAnythingTool:
Collaboration diagram for mitk::SegmentAnythingTool:

Public Member Functions

 mitkClassMacro (SegmentAnythingTool, SegWithPreviewTool)
 
Pointer Clone () const
 
const char ** GetXPM () const override
 Returns an icon in the XPM format. More...
 
const char * GetName () const override
 Returns the name of this tool. Make it short! More...
 
us::ModuleResource GetIconResource () const override
 Returns the tool button icon of the tool wrapped by a usModuleResource. More...
 
void Activated () override
 Called when the tool gets activated. More...
 
void Deactivated () override
 Called when the tool gets deactivated. More...
 
void ClearPicks ()
 Clears all picks and updates the preview. More...
 
bool HasPicks () const
 Checks if any point exists in the either of the pointsets. More...
 
virtual void SetMitkTempDir (std::string _arg)
 
virtual std::string GetMitkTempDir () const
 
virtual void SetPythonPath (std::string _arg)
 
virtual std::string GetPythonPath () const
 
virtual void SetModelType (std::string _arg)
 
virtual std::string GetModelType () const
 
virtual void SetCheckpointPath (std::string _arg)
 
virtual std::string GetCheckpointPath () const
 
virtual void SetGpuId (int _arg)
 
virtual int GetGpuId () const
 
virtual void SetTimeOutLimit (long _arg)
 
virtual long GetTimeOutLimit () const
 
virtual void SetIsReady (bool _arg)
 
virtual bool GetIsReady () const
 
virtual void IsReadyOn ()
 
virtual void IsReadyOff ()
 
void InitSAMPythonProcess ()
 Initializes python service and starts async python daemon of SegmentAnything model. More...
 
bool IsPythonReady () const
 Checks if Python daemon is ready to accept inputs. More...
 
- Public Member Functions inherited from mitk::SegWithPreviewTool
 mitkClassMacro (SegWithPreviewTool, Tool)
 
void Activated () override
 Called when the tool gets activated. More...
 
void Deactivated () override
 Called when the tool gets deactivated. More...
 
void ConfirmSegmentation ()
 
virtual void SetCreateAllTimeSteps (bool _arg)
 
virtual bool GetCreateAllTimeSteps ()
 
virtual void CreateAllTimeStepsOn ()
 
virtual void CreateAllTimeStepsOff ()
 
virtual void SetKeepActiveAfterAccept (bool _arg)
 
virtual bool GetKeepActiveAfterAccept ()
 
virtual void KeepActiveAfterAcceptOn ()
 
virtual void KeepActiveAfterAcceptOff ()
 
virtual void SetIsTimePointChangeAware (bool _arg)
 
virtual bool GetIsTimePointChangeAware ()
 
virtual void IsTimePointChangeAwareOn ()
 
virtual void IsTimePointChangeAwareOff ()
 
virtual void SetResetsToEmptyPreview (bool _arg)
 
virtual bool GetResetsToEmptyPreview ()
 
virtual void ResetsToEmptyPreviewOn ()
 
virtual void ResetsToEmptyPreviewOff ()
 
virtual void SetUseSpecialPreviewColor (bool _arg)
 
virtual bool GetUseSpecialPreviewColor ()
 
virtual void UseSpecialPreviewColorOn ()
 
virtual void UseSpecialPreviewColorOff ()
 
void SetMergeStyle (MultiLabelSegmentation::MergeStyle mergeStyle)
 
virtual MultiLabelSegmentation::MergeStyle GetMergeStyle ()
 
void SetOverwriteStyle (MultiLabelSegmentation::OverwriteStyle overwriteStyle)
 
virtual MultiLabelSegmentation::OverwriteStyle GetOverwriteStyle ()
 
void SetLabelTransferScope (LabelTransferScope labelTransferScope)
 
virtual LabelTransferScope GetLabelTransferScope ()
 
void SetSelectedLabels (const SelectedLabelVectorType &labelsToTransfer)
 
virtual SelectedLabelVectorType GetSelectedLabels ()
 
void SetLabelTransferMode (LabelTransferMode labelTransferMode)
 
virtual LabelTransferMode GetLabelTransferMode ()
 
bool CanHandle (const BaseData *referenceData, const BaseData *workingData) const override
 
void UpdatePreview (bool ignoreLazyPreviewSetting=false)
 
bool IsUpdating () const
 
std::string GetCurrentSegmentationName ()
 Gets the name of the currently selected segmentation node. More...
 
virtual DataNodeGetTargetSegmentationNode () const
 Returns the currently selected segmentation node. More...
 
LabelSetImageGetTargetSegmentation () const
 
LabelSetImageGetPreviewSegmentation ()
 
const LabelSetImageGetPreviewSegmentation () const
 
DataNodeGetPreviewSegmentationNode ()
 
- Public Member Functions inherited from mitk::Tool
 mitkClassMacro (Tool, EventStateMachine)
 
virtual std::string GetIconPath () const
 Returns the path of an icon. More...
 
virtual us::ModuleResource GetCursorIconResource () const
 Returns the path of a cursor icon. More...
 
virtual void InitializeStateMachine ()
 
virtual itk::Object::Pointer GetGUI (const std::string &toolkitPrefix, const std::string &toolkitPostfix)
 Interface for GUI creation. More...
 
virtual NodePredicateBase::ConstPointer GetReferenceDataPreference () const
 
virtual NodePredicateBase::ConstPointer GetWorkingDataPreference () const
 
DataNode::Pointer CreateEmptySegmentationNode (const Image *original, const std::string &organName, const mitk::Color &color) const
 
DataNode::Pointer CreateSegmentationNode (Image *image, const std::string &organName, const mitk::Color &color) const
 
- Public Member Functions inherited from mitk::EventStateMachine
virtual std::vector< std::string > GetClassHierarchy () const override
 
virtual const char * GetClassName () const
 
bool LoadStateMachine (const std::string &filename, const us::Module *module=nullptr)
 Loads XML resource. More...
 
bool HandleEvent (InteractionEvent *event, DataNode *dataNode)
 
void EnableUndo (bool enable)
 Enables or disabled Undo. More...
 
void EnableInteraction (bool enable)
 Enables/disables the state machine. In un-enabled state it won't react to any events. More...
 
- Public Member Functions inherited from mitk::InteractionEventHandler
bool SetEventConfig (const std::string &filename, const us::Module *module=nullptr)
 Loads a configuration from an XML resource. More...
 
bool SetEventConfig (const EventConfig &config)
 Loads a configuration from an EventConfig object. More...
 
EventConfig GetEventConfig () const
 Returns the current configuration. More...
 
bool AddEventConfig (const std::string &filename, const us::Module *module=nullptr)
 This method extends the configuration. More...
 
bool AddEventConfig (const EventConfig &config)
 This method extends the configuration. The configuration from the EventConfig object is loaded and only the ones conflicting are replaced by the new one. This way several configurations can be combined. More...
 
- Public Member Functions inherited from mitk::InteractionEventObserver
 InteractionEventObserver ()
 
virtual ~InteractionEventObserver ()
 
void Disable ()
 
void Enable ()
 
bool IsEnabled () const
 

Static Public Member Functions

static Pointer New ()
 
- Static Public Member Functions inherited from mitk::EventStateMachine
static const char * GetStaticNameOfClass ()
 
- Static Public Member Functions inherited from mitk::InteractionEventHandler
static const char * GetStaticNameOfClass ()
 

Public Attributes

Message1< const std::string & > SAMStatusMessageEvent
 
- Public Attributes inherited from mitk::Tool
Message GUIProcessEventsMessage
 To let GUI process new events (e.g. qApp->processEvents() ) More...
 
Message1< std::string > ErrorMessage
 To send error messages (to be shown by some GUI) More...
 
Message1< bool > CurrentlyBusy
 To send whether the tool is busy (to be shown by some GUI) More...
 
Message1< std::string > GeneralMessage
 To send general messages (to be shown by some GUI) More...
 

Protected Member Functions

 SegmentAnythingTool ()
 
 ~SegmentAnythingTool ()=default
 
void ConnectActionsAndFunctions () override
 
virtual void OnAddPositivePoint (StateMachineAction *, InteractionEvent *interactionEvent)
 
virtual void OnAddNegativePoint (StateMachineAction *, InteractionEvent *interactionEvent)
 
virtual void OnDelete (StateMachineAction *, InteractionEvent *)
 
void ClearSeeds ()
 
void DoUpdatePreview (const Image *inputAtTimeStep, const Image *oldSegAtTimeStep, LabelSetImage *previewImage, TimeStepType timeStep) override
 Overriden method from the tool manager to execute the segmentation Implementation: More...
 
std::vector< std::pair< mitk::Point2D, std::string > > GetPointsAsVector (const mitk::BaseGeometry *)
 Get the Points from positive and negative pointsets as std::vector. More...
 
std::stringstream GetPointsAsCSVString (const mitk::BaseGeometry *baseGeometry)
 Get the Points from positive and negative pointsets as csv string. More...
 
std::string GetHashForCurrentPlane (const mitk::LevelWindow &)
 Get the Hash For Current Plane from current working plane geometry. More...
 
void EmitSAMStatusMessageEvent (const std::string &)
 Emits message to connected Listnerers. More...
 
void ConfirmCleanUp () override
 Cleans up segmentation preview and clears all seeds. More...
 
template<typename TPixel , unsigned int VImageDimension>
void ITKWindowing (const itk::Image< TPixel, VImageDimension > *, mitk::Image *, ScalarType, ScalarType)
 Applies ITK IntensityWindowing Filter to input image;. More...
 
- Protected Member Functions inherited from mitk::SegWithPreviewTool
 SegWithPreviewTool (bool lazyDynamicPreviews=false)
 
 SegWithPreviewTool (bool lazyDynamicPreviews, const char *interactorType, const us::Module *interactorModule=nullptr)
 
 ~SegWithPreviewTool () override
 
const char * GetGroup () const override
 Name of a group. More...
 
void EnsureTargetSegmentationNodeInDataStorage () const
 
virtual void InitiateToolByInput ()
 
virtual void UpdatePrepare ()
 
virtual void UpdateCleanUp ()
 
virtual void PreparePreviewToResultTransfer (const LabelMappingType &labelMapping)
 
const ImageGetSegmentationInput () const
 
const ImageGetReferenceData () const
 
void ResetPreviewNode ()
 
void ResetPreviewContent ()
 
void ResetPreviewContentAtTimeStep (unsigned int timeStep)
 
TimePointType GetLastTimePointOfUpdate () const
 
LabelSetImage::LabelValueType GetActiveLabelValueOfPreview () const
 
virtual Label::PixelType GetUserDefinedActiveLabel () const
 
virtual void SetWorkingPlaneGeometry (PlaneGeometry *_arg)
 
virtual const PlaneGeometryGetWorkingPlaneGeometry ()
 
- Protected Member Functions inherited from mitk::Tool
virtual void SetToolManager (ToolManager *)
 
ToolManagerGetToolManager () const
 
mitk::DataStorageGetDataStorage () const
 
void ConnectActionsAndFunctions () override
 
 Tool (const char *, const us::Module *interactorModule=nullptr)
 
 ~Tool () override
 
void Notify (InteractionEvent *interactionEvent, bool isHandled) override
 
bool FilterEvents (InteractionEvent *, DataNode *) override
 
- Protected Member Functions inherited from mitk::EventStateMachine
 EventStateMachine ()
 
 ~EventStateMachine () override
 
void AddActionFunction (const std::string &action, TActionFunctor *functor)
 
void AddActionFunction (const std::string &action, const ActionFunctionDelegate &delegate)
 
void AddConditionFunction (const std::string &condition, const ConditionFunctionDelegate &delegate)
 
StateMachineStateGetCurrentState () const
 
void ResetToStartState ()
 ResetToStartState Reset state machine to it initial starting state. More...
 
virtual bool CheckCondition (const StateMachineCondition &condition, const InteractionEvent *interactionEvent)
 
virtual void ExecuteAction (StateMachineAction *action, InteractionEvent *interactionEvent)
 
void SetMouseCursor (const char *xpm[], int hotspotX, int hotspotY)
 Sets the specified mouse cursor. More...
 
void ResetMouseCursor ()
 Resets the mouse cursor to its original state. More...
 
StateMachineTransitionGetExecutableTransition (InteractionEvent *event)
 Returns the executable transition for the given event. More...
 
- Protected Member Functions inherited from mitk::InteractionEventHandler
 InteractionEventHandler ()
 
 ~InteractionEventHandler () override
 
PropertyList::Pointer GetAttributes () const
 
std::string MapToEventVariant (InteractionEvent *interactionEvent)
 
virtual void ConfigurationChanged ()
 

Additional Inherited Members

- Public Types inherited from mitk::SegWithPreviewTool
enum  LabelTransferScope { LabelTransferScope::ActiveLabel, LabelTransferScope::SelectedLabels, LabelTransferScope::AllLabels }
 
enum  LabelTransferMode { LabelTransferMode::MapLabel, LabelTransferMode::AddLabel }
 
using SelectedLabelVectorType = std::vector< Label::PixelType >
 
- Public Types inherited from mitk::Tool
typedef mitk::Label::PixelType DefaultSegmentationDataType
 
- Public Types inherited from mitk::EventStateMachine
typedef EventStateMachine Self
 
typedef InteractionEventHandler Superclass
 
typedef itk::SmartPointer< SelfPointer
 
typedef itk::SmartPointer< const SelfConstPointer
 
typedef std::map< std::string, TActionFunctor * > ActionFunctionsMapType
 
typedef itk::SmartPointer< StateMachineStateStateMachineStateType
 
- Public Types inherited from mitk::InteractionEventHandler
typedef InteractionEventHandler Self
 
typedef itk::Object Superclass
 
typedef itk::SmartPointer< SelfPointer
 
typedef itk::SmartPointer< const SelfConstPointer
 
- Protected Types inherited from mitk::SegWithPreviewTool
using LabelMappingType = std::vector< std::pair< Label::PixelType, Label::PixelType > >
 
- Protected Types inherited from mitk::EventStateMachine
typedef MessageAbstractDelegate2< StateMachineAction *, InteractionEvent *, void > ActionFunctionDelegate
 
typedef MessageAbstractDelegate1< const InteractionEvent *, bool > ConditionFunctionDelegate
 
- Static Protected Member Functions inherited from mitk::SegWithPreviewTool
static Image::ConstPointer GetImageByTimeStep (const Image *image, TimeStepType timestep)
 
static Image::Pointer GetImageByTimeStep (Image *image, TimeStepType timestep)
 
static Image::ConstPointer GetImageByTimePoint (const Image *image, TimePointType timePoint)
 
static void TransferLabelInformation (const LabelMappingType &labelMapping, const mitk::LabelSetImage *source, mitk::LabelSetImage *target)
 
static void TransferLabelSetImageContent (const LabelSetImage *source, LabelSetImage *target, TimeStepType timeStep)
 
- Protected Attributes inherited from mitk::SegWithPreviewTool
ToolCommand::Pointer m_ProgressCommand
 
- Protected Attributes inherited from mitk::Tool
std::string m_EventConfig
 Let subclasses change their event configuration. More...
 
- Protected Attributes inherited from mitk::EventStateMachine
bool m_IsActive
 
UndoControllerm_UndoController
 
bool m_UndoEnabled
 

Detailed Description

Segment Anything Model interactive 2D tool class.

See also
mitk::Tool
QmitkInteractiveSegmentation

Definition at line 39 of file mitkSegmentAnythingTool.h.

Constructor & Destructor Documentation

◆ SegmentAnythingTool()

mitk::SegmentAnythingTool::SegmentAnythingTool ( )
protected

◆ ~SegmentAnythingTool()

mitk::SegmentAnythingTool::~SegmentAnythingTool ( )
protecteddefault

Member Function Documentation

◆ Activated()

void mitk::SegmentAnythingTool::Activated ( )
overridevirtual

Called when the tool gets activated.

Derived tools should call their parents implementation at the beginning of the overriding function.

Reimplemented from mitk::Tool.

◆ ClearPicks()

void mitk::SegmentAnythingTool::ClearPicks ( )

Clears all picks and updates the preview.

◆ ClearSeeds()

void mitk::SegmentAnythingTool::ClearSeeds ( )
protected

◆ Clone()

Pointer mitk::SegmentAnythingTool::Clone ( ) const

◆ ConfirmCleanUp()

void mitk::SegmentAnythingTool::ConfirmCleanUp ( )
overrideprotectedvirtual

Cleans up segmentation preview and clears all seeds.

Reimplemented from mitk::SegWithPreviewTool.

◆ ConnectActionsAndFunctions()

void mitk::SegmentAnythingTool::ConnectActionsAndFunctions ( )
overrideprotectedvirtual

Is called after loading a statemachine. Overwrite this function in specific interactor implementations. Connect actions and functions using the CONNECT_FUNCTION macro within this function.

Reimplemented from mitk::EventStateMachine.

◆ Deactivated()

void mitk::SegmentAnythingTool::Deactivated ( )
overridevirtual

Called when the tool gets deactivated.

Derived tools should call their parents implementation at the end of the overriding function.

Reimplemented from mitk::Tool.

◆ DoUpdatePreview()

void mitk::SegmentAnythingTool::DoUpdatePreview ( const Image inputAtTimeStep,
const Image oldSegAtTimeStep,
LabelSetImage previewImage,
TimeStepType  timeStep 
)
overrideprotectedvirtual

Overriden method from the tool manager to execute the segmentation Implementation:

  1. Creates Hash for input image from current plane geometry.
  2. Transfers image pointer to python service along with the hash code.
  3. Creates seed points as CSV string & transfers to python service
  1. Retrieves resulting segmentation Image pointer from python service and sets to previewImage.
Parameters
inputAtTimeStep
oldSegAtTimeStep
previewImage
timeStep

Implements mitk::SegWithPreviewTool.

◆ EmitSAMStatusMessageEvent()

void mitk::SegmentAnythingTool::EmitSAMStatusMessageEvent ( const std::string &  )
protected

Emits message to connected Listnerers.

◆ GetCheckpointPath()

virtual std::string mitk::SegmentAnythingTool::GetCheckpointPath ( ) const
virtual

◆ GetGpuId()

virtual int mitk::SegmentAnythingTool::GetGpuId ( ) const
virtual

◆ GetHashForCurrentPlane()

std::string mitk::SegmentAnythingTool::GetHashForCurrentPlane ( const mitk::LevelWindow )
protected

Get the Hash For Current Plane from current working plane geometry.

Returns
std::string

◆ GetIconResource()

us::ModuleResource mitk::SegmentAnythingTool::GetIconResource ( ) const
overridevirtual

Returns the tool button icon of the tool wrapped by a usModuleResource.

Returns
a valid ModuleResource or an invalid if this function is not reimplemented

Reimplemented from mitk::Tool.

◆ GetIsReady()

virtual bool mitk::SegmentAnythingTool::GetIsReady ( ) const
virtual

◆ GetMitkTempDir()

virtual std::string mitk::SegmentAnythingTool::GetMitkTempDir ( ) const
virtual

◆ GetModelType()

virtual std::string mitk::SegmentAnythingTool::GetModelType ( ) const
virtual

◆ GetName()

const char* mitk::SegmentAnythingTool::GetName ( ) const
overridevirtual

Returns the name of this tool. Make it short!

This name has to fit into some kind of button in most applications, so take some time to think of a good name!

Implements mitk::Tool.

◆ GetPointsAsCSVString()

std::stringstream mitk::SegmentAnythingTool::GetPointsAsCSVString ( const mitk::BaseGeometry baseGeometry)
protected

Get the Points from positive and negative pointsets as csv string.

Parameters
baseGeometry
Returns
std::stringstream

◆ GetPointsAsVector()

std::vector<std::pair<mitk::Point2D, std::string> > mitk::SegmentAnythingTool::GetPointsAsVector ( const mitk::BaseGeometry )
protected

Get the Points from positive and negative pointsets as std::vector.

Parameters
baseGeometryof Image
Returns
std::vector<std::pair<mitk::Point2D, std::string>>

◆ GetPythonPath()

virtual std::string mitk::SegmentAnythingTool::GetPythonPath ( ) const
virtual

◆ GetTimeOutLimit()

virtual long mitk::SegmentAnythingTool::GetTimeOutLimit ( ) const
virtual

◆ GetXPM()

const char** mitk::SegmentAnythingTool::GetXPM ( ) const
overridevirtual

Returns an icon in the XPM format.

This icon has to fit into some kind of button in most applications, so make it smaller than 25x25 pixels.

XPM is e.g. supported by The Gimp. But if you open any XPM file in your text editor, you will see that you could also "draw" it with an editor.

Implements mitk::Tool.

◆ HasPicks()

bool mitk::SegmentAnythingTool::HasPicks ( ) const

Checks if any point exists in the either of the pointsets.

Returns
bool

◆ InitSAMPythonProcess()

void mitk::SegmentAnythingTool::InitSAMPythonProcess ( )

Initializes python service and starts async python daemon of SegmentAnything model.

◆ IsPythonReady()

bool mitk::SegmentAnythingTool::IsPythonReady ( ) const

Checks if Python daemon is ready to accept inputs.

Returns
bool

◆ IsReadyOff()

virtual void mitk::SegmentAnythingTool::IsReadyOff ( )
virtual

◆ IsReadyOn()

virtual void mitk::SegmentAnythingTool::IsReadyOn ( )
virtual

◆ ITKWindowing()

template<typename TPixel , unsigned int VImageDimension>
void mitk::SegmentAnythingTool::ITKWindowing ( const itk::Image< TPixel, VImageDimension > *  ,
mitk::Image ,
ScalarType  ,
ScalarType   
)
protected

Applies ITK IntensityWindowing Filter to input image;.

◆ mitkClassMacro()

mitk::SegmentAnythingTool::mitkClassMacro ( SegmentAnythingTool  ,
SegWithPreviewTool   
)

◆ New()

static Pointer mitk::SegmentAnythingTool::New ( )
static

◆ OnAddNegativePoint()

virtual void mitk::SegmentAnythingTool::OnAddNegativePoint ( StateMachineAction ,
InteractionEvent interactionEvent 
)
protectedvirtual

◆ OnAddPositivePoint()

virtual void mitk::SegmentAnythingTool::OnAddPositivePoint ( StateMachineAction ,
InteractionEvent interactionEvent 
)
protectedvirtual

◆ OnDelete()

virtual void mitk::SegmentAnythingTool::OnDelete ( StateMachineAction ,
InteractionEvent  
)
protectedvirtual

◆ SetCheckpointPath()

virtual void mitk::SegmentAnythingTool::SetCheckpointPath ( std::string  _arg)
virtual

◆ SetGpuId()

virtual void mitk::SegmentAnythingTool::SetGpuId ( int  _arg)
virtual

◆ SetIsReady()

virtual void mitk::SegmentAnythingTool::SetIsReady ( bool  _arg)
virtual

◆ SetMitkTempDir()

virtual void mitk::SegmentAnythingTool::SetMitkTempDir ( std::string  _arg)
virtual

◆ SetModelType()

virtual void mitk::SegmentAnythingTool::SetModelType ( std::string  _arg)
virtual

◆ SetPythonPath()

virtual void mitk::SegmentAnythingTool::SetPythonPath ( std::string  _arg)
virtual

◆ SetTimeOutLimit()

virtual void mitk::SegmentAnythingTool::SetTimeOutLimit ( long  _arg)
virtual

Member Data Documentation

◆ SAMStatusMessageEvent

Message1<const std::string&> mitk::SegmentAnythingTool::SAMStatusMessageEvent

Definition at line 102 of file mitkSegmentAnythingTool.h.


The documentation for this class was generated from the following file: