Medical Imaging Interaction Toolkit  2025.12.02
Medical Imaging Interaction Toolkit
mitkMonaiLabelTool.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 #ifndef mitkMonaiLabelTool_h
13 #define mitkMonaiLabelTool_h
14 
15 #include "mitkSegWithPreviewTool.h"
17 #include <memory>
18 #include <unordered_map>
19 #include <set>
20 #include <nlohmann/json.hpp>
21 #include <mitkPointSet.h>
23 
24 
25 namespace us
26 {
27  class ModuleResource;
28 }
29 
30 namespace mitk
31 {
37  {
38  std::string name;
39  std::string type;
40  std::unordered_map<std::string, int> labels;
41  int dimension;
42  std::string description;
43  std::unordered_map<bool, std::string> config;
44 
45  inline bool operator==(const MonaiModelInfo &rhs) const
46  {
47  return (this->name == rhs.name && this->type == rhs.type); // Comparing only name and type, for now.
48  }
49 
50  inline bool IsInteractive() const
51  {
52  return ("deepgrow" == type || "deepedit" == type);
53  }
54 
55  inline bool Is2D() const
56  {
57  return dimension == 2;
58  }
59  };
60 
66  {
67  std::string name;
68  std::string description;
69  std::vector<std::string> labels;
70  std::string version;
71  std::string hostName;
72  int port;
73  std::string origin;
74  std::vector<MonaiModelInfo> models;
75  };
76 
83  {
85  std::string hostName;
86  std::string requestLabel;
87  int port;
88 
89  inline bool operator==(const MonaiLabelRequest &rhs) const
90  {
91  return (this->model == rhs.model && this->hostName == rhs.hostName && this->port == rhs.port &&
92  this->requestLabel == rhs.requestLabel);
93  }
94  };
95 
105  {
106  public:
108  itkCloneMacro(Self);
109  void Activated() override;
110  void Deactivated() override;
111  void UpdatePrepare() override;
112 
117  void FetchOverallInfo(const std::string &hostName, const int &port);
118 
123  std::unique_ptr<MonaiLabelRequest> m_RequestParameters;
124 
129  const std::vector<MonaiModelInfo> GetAutoSegmentationModels(const int dim = -1) const;
130 
135  const std::vector<MonaiModelInfo> GetInteractiveSegmentationModels(const int dim = -1) const;
136 
141  const std::vector<MonaiModelInfo> GetScribbleSegmentationModels(const int dim = -1) const;
142 
146  MonaiModelInfo GetModelInfoFromName(const std::string) const;
147 
148  itkSetMacro(ModelName, std::string);
149  itkGetConstMacro(ModelName, std::string);
150  itkSetMacro(URL, std::string);
151  itkGetConstMacro(URL, std::string);
152  itkSetMacro(TempDir, std::string);
153  itkGetConstMacro(TempDir, std::string);
154  itkSetMacro(Timeout, unsigned int);
155  itkGetConstMacro(Timeout, unsigned int);
156 
158 
159 
163  void ClearPicks();
164 
169  bool HasPicks() const;
170 
172 
173  protected:
176  void DoUpdatePreview(const Image* inputAtTimeStep, const Image* oldSegAtTimeStep, MultiLabelSegmentation* previewImage, TimeStepType timeStep) override;
177  void ConnectActionsAndFunctions() override;
178 
183  virtual void WriteImage(const Image *, const std::string &) const = 0;
184 
185  /*
186  * @brief Add positive seed point action of StateMachine pattern.
187  */
188  virtual void OnAddPositivePoint(StateMachineAction *, InteractionEvent *interactionEvent);
189 
190  /*
191  * @brief Add negative seed point action of StateMachine pattern
192  */
193  virtual void OnAddNegativePoint(StateMachineAction *, InteractionEvent *interactionEvent);
194 
195  /*
196  * @brief Delete action of StateMachine pattern
197  */
199 
203 
204  /*
205  * @brief Clear all seed points and call UpdatePreview to reset the segmentation Preview
206  */
207  void ClearSeeds();
208 
213  virtual std::string ConvertPointsAsListString(const mitk::BaseGeometry *baseGeometry,
214  const PointSet::Pointer pointSet) const;
215 
220 
221  PointSet::Pointer m_PointSetPositive;
222  PointSet::Pointer m_PointSetNegative;
223  DataNode::Pointer m_PointSetNodePositive;
224  DataNode::Pointer m_PointSetNodeNegative;
225  int m_PointSetCount = 0;
226 
227  private:
228 
233  std::unique_ptr<MonaiAppMetadata> m_InfoParameters;
234 
239  std::pair<std::string, std::string> CreateTempDirs(const std::string &filePattern) const;
240 
244  bool IsMonaiServerOn(const std::string &hostName, const int &port) const;
245 
250  mitk::Image::Pointer ApplyLevelWindowEffect(const Image *inputAtTimeStep) const;
251 
256  void PostInferRequest(const std::string &hostName, const int &port, const std::string &filePath, const std::string &outFile,
257  const mitk::BaseGeometry *baseGeometry);
258 
259  std::string m_TempDir;
260  std::string m_ModelName;
261  std::string m_URL;
262  nlohmann::json m_ResultMetadata;
263  unsigned int m_Timeout = 60; //seconds
264  const std::set<std::string> m_AUTO_SEG_TYPE_NAME = {"segmentation"};
265  const std::set<std::string> m_SCRIBBLE_SEG_TYPE_NAME = {"scribbles"};
266  const std::set<std::string> m_INTERACTIVE_SEG_TYPE_NAME = {"deepgrow"}; // deepedit not supported yet
267  const std::string m_TEMPLATE_FILENAME = "XXXXXX_000_0000.nii.gz";
268  const std::string m_SERVER_503_ERROR_TEXT = "A connection to MonaiLabel server cannot be established.";
269  const Label::PixelType MASK_VALUE = 1;
270  };
271 }
272 #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
MonaiLabel segmentation tool base class.
MonaiModelInfo GetModelInfoFromName(const std::string) const
Helper function to get full model info object from model name.
mitkClassMacro(MonaiLabelTool, SegWithPreviewTool)
DataNode::Pointer m_PointSetNodePositive
virtual void OnAddPositivePoint(StateMachineAction *, InteractionEvent *interactionEvent)
void ClearPicks()
Clears all picks and updates the preview.
bool HasPicks() const
Checks if any point exists in the either of the PointSetPositive or PointSetNegative.
PointSet::Pointer m_PointSetNegative
void ConnectActionsAndFunctions() override
virtual void WriteBackResults(MultiLabelSegmentation *, MultiLabelSegmentation *, TimeStepType) const =0
Writes back segmentation results in 3D or 2D shape to preview MultiLabelSegmentation.
PointSet::Pointer m_PointSetPositive
const std::vector< MonaiModelInfo > GetInteractiveSegmentationModels(const int dim=-1) const
Get the Interactive Segmentation Models info for the given dimension.
void FetchOverallInfo(const std::string &hostName, const int &port)
Method does the GET Rest call to fetch MonaiLabel server metadata including all models' info.
const std::vector< MonaiModelInfo > GetAutoSegmentationModels(const int dim=-1) const
Get the Auto Segmentation Models info for the given dimension.
const std::vector< MonaiModelInfo > GetScribbleSegmentationModels(const int dim=-1) const
Get the Scribble Segmentation Models info for the given dimension.
virtual void OnAddNegativePoint(StateMachineAction *, InteractionEvent *interactionEvent)
virtual void OnDelete(StateMachineAction *, InteractionEvent *)
virtual std::string ConvertPointsAsListString(const mitk::BaseGeometry *baseGeometry, const PointSet::Pointer pointSet) const
Get the Points from given pointset as csv string.
void UpdatePrepare() override
void OnMove(StateMachineAction *, InteractionEvent *)
void OnRelease(StateMachineAction *, InteractionEvent *)
void Deactivated() override
Called when the tool gets deactivated.
virtual void WriteImage(const Image *, const std::string &) const =0
Writes image to disk as the tool desires. Default implementation does nothing.
void Activated() override
Called when the tool gets activated.
DataNode::Pointer m_PointSetNodeNegative
Message1< const bool > MonaiStatusEvent
const MonaiAppMetadata * GetInfoParameters() const
void DoUpdatePreview(const Image *inputAtTimeStep, const Image *oldSegAtTimeStep, MultiLabelSegmentation *previewImage, TimeStepType timeStep) override
void OnPrimaryButtonPressed(StateMachineAction *, InteractionEvent *)
std::unique_ptr< MonaiLabelRequest > m_RequestParameters
Variable to set selected model's and other data needed for the POST call.
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.
Represents an action, that is executed after a certain event (in statemachine-mechanism) TODO: implem...
nlohmann::json json
Find image slices visible on a given plane.
std::size_t TimeStepType
Struct to store MonaiLabel server metadata including all model infos.
std::vector< MonaiModelInfo > models
std::vector< std::string > labels
Request class to pack model and other necessary server information from GUI.
bool operator==(const MonaiLabelRequest &rhs) const
Struct to hold featured models individual info.
std::unordered_map< std::string, int > labels
bool operator==(const MonaiModelInfo &rhs) const
std::unordered_map< bool, std::string > config