Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
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;
48  us::ModuleResource GetIconResource() 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 
96  void InitSAMPythonProcess();
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  */
128  virtual void OnDelete(StateMachineAction*, InteractionEvent*);
129 
130  void OnMove(StateMachineAction*, InteractionEvent*);
131  void OnRelease(StateMachineAction*, InteractionEvent*);
132  void OnPrimaryButtonPressed(StateMachineAction *, InteractionEvent *);
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, LabelSetImage *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
mitk::SegWithPreviewTool
Base class for any auto segmentation tool that provides a preview of the new segmentation.
Definition: mitkSegWithPreviewTool.h:38
mitk::InteractionEvent
Definition: mitkInteractionEvent.h:26
mitk::Message1< const std::string & >
mitkProcessExecutor.h
us
Definition: mitkAbstractFileReader.h:29
mitkSegWithPreviewTool.h
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
mitkSegmentAnythingPythonService.h
itk::SmartPointer< Self >
mitk::SegmentAnythingTool
Segment Anything Model interactive 2D tool class.
Definition: mitkSegmentAnythingTool.h:39
mitk::Label::PixelType
unsigned short PixelType
Definition: mitkLabel.h:34
mitkPointSet.h
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::EventStateMachine
‍**
Definition: mitkEventStateMachine.h:111
mitk::LevelWindow
The LevelWindow class Class to store level/window values.
Definition: mitkLevelWindow.h:45
mitk::BaseGeometry
BaseGeometry Describes the geometry of a data object.
Definition: mitkBaseGeometry.h:94
mitk::Point< ScalarType, 2 >
MITKSEGMENTATION_EXPORT
#define MITKSEGMENTATION_EXPORT
Definition: MitkSegmentationExports.h:15
mitk::SegmentAnythingTool::m_PythonService
SegmentAnythingPythonService::Pointer m_PythonService
Definition: mitkSegmentAnythingTool.h:203
us::ModuleResource
Definition: usModuleResource.h:55
mitk::TimeStepType
std::size_t TimeStepType
Definition: mitkTimeGeometry.h:27
mitk::SegmentAnythingTool::SAMStatusMessageEvent
Message1< const std::string & > SAMStatusMessageEvent
Definition: mitkSegmentAnythingTool.h:105
mitk::StateMachineAction
Represents an action, that is executed after a certain event (in statemachine-mechanism) TODO: implem...
Definition: mitkStateMachineAction.h:30
mitkLevelWindow.h
mitkClassMacro
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:36
MitkSegmentationExports.h
mitk::LabelSetImage
LabelSetImage class for handling labels and layers in a segmentation session.
Definition: mitkLabelSetImage.h:43
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20