Medical Imaging Interaction Toolkit  2023.12.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  void ClearPicks();
57 
64  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(GpuId, int);
79  itkGetConstMacro(GpuId, int);
80 
81  itkSetMacro(TimeOutLimit, long);
82  itkGetConstMacro(TimeOutLimit, long);
83 
84  itkSetMacro(IsReady, bool);
85  itkGetConstMacro(IsReady, bool);
86  itkBooleanMacro(IsReady);
87 
93  void InitSAMPythonProcess();
94 
100  bool IsPythonReady() const;
101 
103 
104  protected:
106  ~SegmentAnythingTool() = default;
107 
108  void ConnectActionsAndFunctions() override;
109 
110  /*
111  * @brief Add positive seed point action of StateMachine pattern
112  */
113  virtual void OnAddPositivePoint(StateMachineAction*, InteractionEvent *interactionEvent);
114 
115  /*
116  * @brief Add negative seed point action of StateMachine pattern
117  */
118  virtual void OnAddNegativePoint(StateMachineAction*, InteractionEvent *interactionEvent);
119 
120  /*
121  * @brief Delete action of StateMachine pattern. The function deletes positive or negative points in
122  the reverse order of creation. This is done by finding & deleting the Point having the highest
123  PointIdentifier value from either of the PointSets m_PointSetPositive & m_PointSetNegative.
124  */
125  virtual void OnDelete(StateMachineAction*, InteractionEvent*);
126 
127  /*
128  * @brief Clear all seed points and call UpdatePreview to reset the segmentation Preview
129  */
130  void ClearSeeds();
131 
145  void DoUpdatePreview(const Image *inputAtTimeStep, const Image *oldSegAtTimeStep, LabelSetImage *previewImage, TimeStepType timeStep) override;
146 
153  std::vector<std::pair<mitk::Point2D, std::string>> GetPointsAsVector(const mitk::BaseGeometry*);
154 
161  std::stringstream GetPointsAsCSVString(const mitk::BaseGeometry *baseGeometry);
162 
168  std::string GetHashForCurrentPlane(const mitk::LevelWindow&);
169 
174  void EmitSAMStatusMessageEvent(const std::string&);
175 
180  void ConfirmCleanUp() override;
181 
186  template <typename TPixel, unsigned int VImageDimension>
187  void ITKWindowing(const itk::Image<TPixel, VImageDimension>*, mitk::Image*, ScalarType, ScalarType);
188 
189  private:
197  static mitk::Point2D Get2DIndicesfrom3DWorld(const mitk::BaseGeometry*, const mitk::Point3D&);
198 
199  std::string m_MitkTempDir;
200  std::string m_PythonPath;
201  std::string m_ModelType;
202  std::string m_CheckpointPath;
203  int m_GpuId = 0;
204  PointSet::Pointer m_PointSetPositive;
205  PointSet::Pointer m_PointSetNegative;
206  DataNode::Pointer m_PointSetNodePositive;
207  DataNode::Pointer m_PointSetNodeNegative;
208  bool m_IsGenerateEmbeddings = true;
209  bool m_IsReady = false;
210  int m_PointSetCount = 0;
211  long m_TimeOutLimit = -1;
212  std::unique_ptr<SegmentAnythingPythonService> m_PythonService;
213  const Label::PixelType MASK_VALUE = 1;
214  };
215 } // namespace
216 
217 #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
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:102
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:29
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20