Medical Imaging Interaction Toolkit  2026.06.00
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 {
36  {
37  std::string name;
38  std::string type;
39  std::unordered_map<std::string, int> labels;
40  int dimension;
41  std::string description;
42  std::unordered_map<bool, std::string> config;
43 
44  inline bool operator==(const MonaiModelInfo &rhs) const
45  {
46  return (this->name == rhs.name && this->type == rhs.type); // Comparing only name and type, for now.
47  }
48 
49  inline bool IsInteractive() const
50  {
51  return ("deepgrow" == type || "deepedit" == type);
52  }
53 
54  inline bool Is2D() const
55  {
56  return dimension == 2;
57  }
58  };
59 
64  {
65  std::string name;
66  std::string description;
67  std::vector<std::string> labels;
68  std::string version;
69  std::string hostName;
70  int port;
71  std::string origin;
72  std::vector<MonaiModelInfo> models;
73  };
74 
79  {
81  std::string hostName;
82  std::string requestLabel;
83  int port;
84 
85  inline bool operator==(const MonaiLabelRequest &rhs) const
86  {
87  return (this->model == rhs.model && this->hostName == rhs.hostName && this->port == rhs.port &&
88  this->requestLabel == rhs.requestLabel);
89  }
90  };
91 
101  {
102  public:
104  itkCloneMacro(Self);
105  void Activated() override;
106  void Deactivated() override;
107  void UpdatePrepare() override;
108 
112 
116  void FetchOverallInfo(const std::string &hostName, const int &port);
117 
121  std::unique_ptr<MonaiLabelRequest> m_RequestParameters;
122 
126  const std::vector<MonaiModelInfo> GetAutoSegmentationModels(const int dim = -1) const;
127 
131  const std::vector<MonaiModelInfo> GetInteractiveSegmentationModels(const int dim = -1) const;
132 
136  const std::vector<MonaiModelInfo> GetScribbleSegmentationModels(const int dim = -1) const;
137 
141  MonaiModelInfo GetModelInfoFromName(const std::string) const;
142 
143  itkSetMacro(ModelName, std::string);
144  itkGetConstMacro(ModelName, std::string);
145  itkSetMacro(URL, std::string);
146  itkGetConstMacro(URL, std::string);
147  itkSetMacro(TempDir, std::string);
148  itkGetConstMacro(TempDir, std::string);
149  itkSetMacro(Timeout, unsigned int);
150  itkGetConstMacro(Timeout, unsigned int);
151 
153 
154 
158  void ClearPicks();
159 
163  bool HasPicks() const;
164 
166 
167  protected:
170  void DoUpdatePreview(const Image* inputAtTimeStep, const Image* oldSegAtTimeStep, MultiLabelSegmentation* previewImage, TimeStepType timeStep) override;
171  void ConnectActionsAndFunctions() override;
172 
178  virtual void WriteImage(const Image *, const std::string &) const = 0;
179 
183  virtual void OnAddPositivePoint(StateMachineAction *, InteractionEvent *interactionEvent);
184 
188  virtual void OnAddNegativePoint(StateMachineAction *, InteractionEvent *interactionEvent);
189 
194 
198 
202  void ClearSeeds();
203 
207  virtual std::string ConvertPointsAsListString(const mitk::BaseGeometry *baseGeometry,
208  const PointSet::Pointer pointSet) const;
209 
214 
215  PointSet::Pointer m_PointSetPositive;
216  PointSet::Pointer m_PointSetNegative;
217  DataNode::Pointer m_PointSetNodePositive;
218  DataNode::Pointer m_PointSetNodeNegative;
219  int m_PointSetCount = 0;
220 
221  private:
222 
226  std::unique_ptr<MonaiAppMetadata> m_InfoParameters;
227 
232  std::pair<std::string, std::string> CreateTempDirs(const std::string &filePattern) const;
233 
237  bool IsMonaiServerOn(const std::string &hostName, const int &port) const;
238 
242  mitk::Image::Pointer ApplyLevelWindowEffect(const Image *inputAtTimeStep) const;
243 
247  void PostInferRequest(const std::string &hostName, const int &port, const std::string &filePath, const std::string &outFile,
248  const mitk::BaseGeometry *baseGeometry);
249 
250  std::string m_TempDir;
251  std::string m_ModelName;
252  std::string m_URL;
253  nlohmann::json m_ResultMetadata;
254  unsigned int m_Timeout = 60; //seconds
255  const std::set<std::string> m_AUTO_SEG_TYPE_NAME = {"segmentation"};
256  const std::set<std::string> m_SCRIBBLE_SEG_TYPE_NAME = {"scribbles"};
257  const std::set<std::string> m_INTERACTIVE_SEG_TYPE_NAME = {"deepgrow"}; // deepedit not supported yet
258  const std::string m_TEMPLATE_FILENAME = "XXXXXX_000_0000.nii.gz";
259  const std::string m_SERVER_503_ERROR_TEXT = "A connection to MonaiLabel server cannot be established.";
260  const Label::PixelType MASK_VALUE = 1;
261  };
262 }
263 #endif
#define MITKSEGMENTATION_EXPORT
Abstract base class describing the geometry of a data object.
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
Base class for all interaction events in MITK.
unsigned short PixelType
Pixel type used for label values (unsigned short).
Definition: mitkLabel.h:106
AlgorithmType
Enumeration describing how a label was created or refined.
Definition: mitkLabel.h:283
MonaiLabel segmentation tool base class.
MonaiModelInfo GetModelInfoFromName(const std::string) const
Returns full model info for the given model name.
mitkClassMacro(MonaiLabelTool, SegWithPreviewTool)
DataNode::Pointer m_PointSetNodePositive
virtual void OnAddPositivePoint(StateMachineAction *, InteractionEvent *interactionEvent)
Add positive seed point action of StateMachine pattern.
void ClearPicks()
Clears all seed picks and updates the preview.
bool HasPicks() const
Checks if any seed point exists in either the positive or negative point set.
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 the preview image.
PointSet::Pointer m_PointSetPositive
const std::vector< MonaiModelInfo > GetInteractiveSegmentationModels(const int dim=-1) const
Returns the interactive segmentation model information for the given dimension.
void FetchOverallInfo(const std::string &hostName, const int &port)
Fetches MonaiLabel server metadata via GET REST call.
const std::vector< MonaiModelInfo > GetAutoSegmentationModels(const int dim=-1) const
Returns the auto-segmentation model information for the given dimension.
const std::vector< MonaiModelInfo > GetScribbleSegmentationModels(const int dim=-1) const
Returns the scribble segmentation model information for the given dimension.
virtual void OnAddNegativePoint(StateMachineAction *, InteractionEvent *interactionEvent)
Add negative seed point action of StateMachine pattern.
virtual void OnDelete(StateMachineAction *, InteractionEvent *)
Delete action of StateMachine pattern.
virtual std::string ConvertPointsAsListString(const mitk::BaseGeometry *baseGeometry, const PointSet::Pointer pointSet) const
Converts point coordinates from a given point set to a 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 the input image to disk in a format required by the tool.
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
Holds the selected model and server data for the POST inference call.
mitk::Label::AlgorithmType GetAlgorithmType() const override
AUTOMATIC for auto-segmentation models (no per-image human input, like nnUNet), SEMIAUTOMATIC for int...
void ClearSeeds()
Clears all seed points and updates the preview to reset the segmentation.
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 the state machine mechanism.
nlohmann::json json
Find image slices visible on a given plane.
std::size_t TimeStepType
Type for time step indices (non-negative integers starting from 0).
Struct storing MonaiLabel server metadata including all model information.
std::vector< MonaiModelInfo > models
std::vector< std::string > labels
Request class encapsulating model and server information for a MonaiLabel inference request.
bool operator==(const MonaiLabelRequest &rhs) const
Struct holding metadata for a single MonaiLabel model.
std::unordered_map< std::string, int > labels
bool operator==(const MonaiModelInfo &rhs) const
std::unordered_map< bool, std::string > config