Medical Imaging Interaction Toolkit  2024.06.00
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  /*
131  * @brief Clear all seed points and call UpdatePreview to reset the segmentation Preview
132  */
133  void ClearSeeds();
134 
148  void DoUpdatePreview(const Image *inputAtTimeStep, const Image *oldSegAtTimeStep, LabelSetImage *previewImage, TimeStepType timeStep) override;
149 
155  std::vector<std::pair<mitk::Point2D, std::string>> GetPointsAsVector(const mitk::BaseGeometry *baseGeometry) const;
156 
163  virtual std::string GetPointsAsCSVString(const mitk::BaseGeometry *baseGeometry) const;
164 
170  std::string GetHashForCurrentPlane(const mitk::LevelWindow &levelWindow) const;
171 
176  void EmitSAMStatusMessageEvent(const std::string &status);
177 
182  void ConfirmCleanUp() override;
183 
188  template <typename TPixel, unsigned int VImageDimension>
189  void ITKWindowing(const itk::Image<TPixel, VImageDimension>*, mitk::Image*, ScalarType, ScalarType);
190 
197  static mitk::Point2D Get2DIndicesfrom3DWorld(const mitk::BaseGeometry *baseGeometry, const mitk::Point3D &point3d);
198 
199  std::unique_ptr<SegmentAnythingPythonService> m_PythonService;
200 
201  private:
202  std::string m_MitkTempDir;
203  std::string m_PythonPath;
204  std::string m_ModelType;
205  std::string m_CheckpointPath;
206  std::string m_Backend;
207  int m_GpuId = 0;
208  PointSet::Pointer m_PointSetPositive;
209  PointSet::Pointer m_PointSetNegative;
210  DataNode::Pointer m_PointSetNodePositive;
211  DataNode::Pointer m_PointSetNodeNegative;
212  bool m_IsGenerateEmbeddings = true;
213  bool m_IsReady = false;
214  int m_PointSetCount = 0;
215  long m_TimeOutLimit = -1;
216  const Label::PixelType MASK_VALUE = 1;
217  };
218 } // namespace
219 
220 #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
std::unique_ptr< SegmentAnythingPythonService > m_PythonService
Definition: mitkSegmentAnythingTool.h:199
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