Medical Imaging Interaction Toolkit  2025.12.02
Medical Imaging Interaction Toolkit
mitkSegmentAnythingTool.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkSegmentAnythingTool_h
14 #define mitkSegmentAnythingTool_h
15 
16 #include "mitkSegWithPreviewTool.h"
17 #include "mitkPointSet.h"
18 #include "mitkProcessExecutor.h"
21 #include <itkImage.h>
22 #include <mitkLevelWindow.h>
23 
24 namespace us
25 {
26  class ModuleResource;
27 }
28 
29 namespace mitk
30 {
40  {
41  public:
43  itkFactorylessNewMacro(Self);
44  itkCloneMacro(Self);
45 
46  const char **GetXPM() const override;
47  const char *GetName() const override;
49 
50  void Activated() override;
51  void Deactivated() override;
52 
56  virtual void ClearPicks();
57 
64  virtual bool HasPicks() const;
65 
66  itkSetMacro(MitkTempDir, std::string);
67  itkGetConstMacro(MitkTempDir, std::string);
68 
69  itkSetMacro(PythonPath, std::string);
70  itkGetConstMacro(PythonPath, std::string);
71 
72  itkSetMacro(ModelType, std::string);
73  itkGetConstMacro(ModelType, std::string);
74 
75  itkSetMacro(CheckpointPath, std::string);
76  itkGetConstMacro(CheckpointPath, std::string);
77 
78  itkSetMacro(Backend, std::string);
79  itkGetConstMacro(Backend, std::string);
80 
81  itkSetMacro(GpuId, int);
82  itkGetConstMacro(GpuId, int);
83 
84  itkSetMacro(TimeOutLimit, long);
85  itkGetConstMacro(TimeOutLimit, long);
86 
87  itkSetMacro(IsReady, bool);
88  itkGetConstMacro(IsReady, bool);
89  itkBooleanMacro(IsReady);
90 
97 
103  bool IsPythonReady() const;
104 
106 
107  protected:
109  ~SegmentAnythingTool() = default;
110 
111  void ConnectActionsAndFunctions() override;
112 
113  /*
114  * @brief Add positive seed point action of StateMachine pattern
115  */
116  virtual void OnAddPositivePoint(StateMachineAction*, InteractionEvent *interactionEvent);
117 
118  /*
119  * @brief Add negative seed point action of StateMachine pattern
120  */
121  virtual void OnAddNegativePoint(StateMachineAction*, InteractionEvent *interactionEvent);
122 
123  /*
124  * @brief Delete action of StateMachine pattern. The function deletes positive or negative points in
125  the reverse order of creation. This is done by finding & deleting the Point having the highest
126  PointIdentifier value from either of the PointSets m_PointSetPositive & m_PointSetNegative.
127  */
129 
133 
134  /*
135  * @brief Clear all seed points and call UpdatePreview to reset the segmentation Preview
136  */
137  void ClearSeeds();
138 
152  void DoUpdatePreview(const Image *inputAtTimeStep, const Image *oldSegAtTimeStep, MultiLabelSegmentation *previewImage, TimeStepType timeStep) override;
153 
159  std::vector<std::pair<mitk::Point2D, std::string>> GetPointsAsVector(const mitk::BaseGeometry *baseGeometry) const;
160 
167  virtual std::string GetPointsAsCSVString(const mitk::BaseGeometry *baseGeometry) const;
168 
174  std::string GetHashForCurrentPlane(const mitk::LevelWindow &levelWindow) const;
175 
180  void EmitSAMStatusMessageEvent(const std::string &status);
181 
186  void ConfirmCleanUp() override;
187 
192  template <typename TPixel, unsigned int VImageDimension>
193  void ITKWindowing(const itk::Image<TPixel, VImageDimension>*, mitk::Image*, ScalarType, ScalarType);
194 
201  static mitk::Point2D Get2DIndicesfrom3DWorld(const mitk::BaseGeometry *baseGeometry, const mitk::Point3D &point3d);
202 
203  SegmentAnythingPythonService::Pointer m_PythonService;
204 
205  private:
206  std::string m_MitkTempDir;
207  std::string m_PythonPath;
208  std::string m_ModelType;
209  std::string m_CheckpointPath;
210  std::string m_Backend;
211  int m_GpuId = 0;
212  PointSet::Pointer m_PointSetPositive;
213  PointSet::Pointer m_PointSetNegative;
214  DataNode::Pointer m_PointSetNodePositive;
215  DataNode::Pointer m_PointSetNodeNegative;
216  bool m_IsGenerateEmbeddings = true;
217  bool m_IsReady = false;
218  int m_PointSetCount = 0;
219  long m_TimeOutLimit = -1;
220  const Label::PixelType MASK_VALUE = 1;
221  };
222 } // namespace
223 
224 #endif
#define MITKSEGMENTATION_EXPORT
BaseGeometry Describes the geometry of a data object.
Image class for storing images.
Definition: mitkImage.h:70
unsigned short PixelType
Definition: mitkLabel.h:94
The LevelWindow class Class to store level/window values.
MultiLabelSegmentation class for handling labels and layers in a segmentation session.
Base class for any auto segmentation tool that provides a preview of the new segmentation.
Segment Anything Model interactive 2D tool class.
Message1< const std::string & > SAMStatusMessageEvent
void OnPrimaryButtonPressed(StateMachineAction *, InteractionEvent *)
std::vector< std::pair< mitk::Point2D, std::string > > GetPointsAsVector(const mitk::BaseGeometry *baseGeometry) const
Get the Points from positive and negative pointsets as std::vector.
void InitSAMPythonProcess()
Initializes python service and starts async python daemon of SegmentAnything model.
us::ModuleResource GetIconResource() const override
Returns the tool button icon of the tool wrapped by a usModuleResource.
virtual bool HasPicks() const
Checks if any point exists in the either of the pointsets.
virtual std::string GetPointsAsCSVString(const mitk::BaseGeometry *baseGeometry) const
Get the Points from positive and negative pointsets as csv string.
virtual void ClearPicks()
Clears all picks and updates the preview.
void EmitSAMStatusMessageEvent(const std::string &status)
Emits message to connected Listnerers.
SegmentAnythingPythonService::Pointer m_PythonService
virtual void OnDelete(StateMachineAction *, InteractionEvent *)
static mitk::Point2D Get2DIndicesfrom3DWorld(const mitk::BaseGeometry *baseGeometry, const mitk::Point3D &point3d)
Convert 3D world coordinates to 2D indices.
void Deactivated() override
Called when the tool gets deactivated.
virtual void OnAddNegativePoint(StateMachineAction *, InteractionEvent *interactionEvent)
void OnMove(StateMachineAction *, InteractionEvent *)
mitkClassMacro(SegmentAnythingTool, SegWithPreviewTool)
void OnRelease(StateMachineAction *, InteractionEvent *)
const char * GetName() const override
Returns the name of this tool. Make it short!
void ConfirmCleanUp() override
Cleans up segmentation preview and clears all seeds.
void DoUpdatePreview(const Image *inputAtTimeStep, const Image *oldSegAtTimeStep, MultiLabelSegmentation *previewImage, TimeStepType timeStep) override
Overridden method from the tool manager to execute the segmentation Implementation:
virtual void OnAddPositivePoint(StateMachineAction *, InteractionEvent *interactionEvent)
std::string GetHashForCurrentPlane(const mitk::LevelWindow &levelWindow) const
Get the Hash For Current Plane from current working plane geometry.
void ITKWindowing(const itk::Image< TPixel, VImageDimension > *, mitk::Image *, ScalarType, ScalarType)
Applies ITK IntensityWindowing Filter to input image;.
bool IsPythonReady() const
Checks if Python daemon is ready to accept inputs.
const char ** GetXPM() const override
Returns an icon in the XPM format.
void ConnectActionsAndFunctions() override
void Activated() override
Called when the tool gets activated.
Represents an action, that is executed after a certain event (in statemachine-mechanism) TODO: implem...
Backend
Specifies the computation backends available for nnInteractive.
Find image slices visible on a given plane.
std::size_t TimeStepType
double ScalarType