Medical Imaging Interaction Toolkit  2025.12.02
Medical Imaging Interaction Toolkit
mitkMultiLabelIOHelper.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 mitkMultiLabelIOHelper_h
14 #define mitkMultiLabelIOHelper_h
15 
16 #include <mitkLabel.h>
17 
18 #include <mitkLabelSetImage.h>
19 
20 #include <itkSmartPointer.h>
21 #include <nlohmann/json.hpp>
22 
23 #include <MitkMultilabelExports.h>
24 
25 namespace tinyxml2
26 {
27  class XMLDocument;
28  class XMLElement;
29 }
30 
31 namespace itk
32 {
33  class MetaDataDictionary;
34 }
35 
36 namespace mitk
37 {
38  const constexpr char* const PROPERTY_NAME_TIMEGEOMETRY_TYPE = "org.mitk.timegeometry.type";
39  const constexpr char* const PROPERTY_NAME_TIMEGEOMETRY_TIMEPOINTS = "org.mitk.timegeometry.timepoints";
40  const constexpr char* const PROPERTY_KEY_TIMEGEOMETRY_TYPE = "org_mitk_timegeometry_type";
41  const constexpr char* const PROPERTY_KEY_TIMEGEOMETRY_TIMEPOINTS = "org_mitk_timegeometry_timepoints";
42  const constexpr char* const PROPERTY_KEY_UID = "org_mitk_uid";
43 
51  {
52  public:
60  static bool SaveMultiLabelSegmentationPreset(const std::string &presetFilename,
61  const mitk::MultiLabelSegmentation *inputSegmentation);
62 
70  static bool LoadMultiLabelSegmentationPreset(const std::string &presetFilename,
71  mitk::MultiLabelSegmentation *inputSegmentation);
72 
78  static itk::SmartPointer<mitk::Label> LoadLabelFromXMLDocument(const tinyxml2::XMLElement *labelElem);
79 
86  static tinyxml2::XMLElement *GetLabelAsXMLElement(tinyxml2::XMLDocument &doc, const Label *label);
87 
96  static tinyxml2::XMLElement *PropertyToXMLElement(tinyxml2::XMLDocument& doc, const std::string &key, const BaseProperty *property);
97 
105  static bool PropertyFromXMLElement(std::string &key, itk::SmartPointer<mitk::BaseProperty> &prop, const tinyxml2::XMLElement *elem);
106 
109  static int GetIntByKey(const itk::MetaDataDictionary& dic, const std::string& key);
112  static std::string GetStringByKey(const itk::MetaDataDictionary& dic, const std::string& key);
113 
118  {
119  std::string name;
121  PropertyList::Pointer properties; // For custom group properties
122 
124  LabelGroupMetaData(const std::string& groupName, const LabelVector& groupLabels);
125  LabelGroupMetaData(const std::string& groupName, const LabelVector& groupLabels, PropertyList* groupProperties);
126  ~LabelGroupMetaData() = default;
127  };
128 
129 
133 
146  GroupFileNameCallback groupFileNameCallback = nullptr ,
147  LabelFileNameCallback labelFileNameCallback = nullptr,
148  LabelFileValueCallback labelFileValueCallback = nullptr);
149 
155  static std::vector<LabelGroupMetaData> DeserializeMultiLabelGroupsFromJSON(const nlohmann::json& listOfLabelGroups);
156 
163 
169  static mitk::Label::Pointer DeserializeLabelFromJSON(const nlohmann::json& labelJson);
170 
177 
178  template<typename TValueType>
179  static bool GetValueFromJson(const nlohmann::json& labelJson, const std::string& key, TValueType& value)
180  {
181  if (labelJson.find(key) != labelJson.end())
182  {
183  try
184  {
185  value = labelJson[key].get<TValueType>();
186  return true;
187  }
188  catch (...)
189  {
190  MITK_ERROR << "Unable to read label information from json. Value has wrong type. Failed key: " << key << "; invalid value: " << labelJson[key].dump();
191  throw;
192  }
193  }
194  return false;
195  }
196 
205 
214 
215  private:
216  MultiLabelIOHelper() = delete;
217  };
218 }
219 
220 #endif
#define MITKMULTILABEL_EXPORT
Abstract base class for properties.
A data structure describing a label.
Definition: mitkLabel.h:90
The MultiLabelIOHelper is a static helper class that supports serialization of mitk::MultiLabelSegmen...
static void RemoveMetaPropertiesFromLabel(Label *label)
Remove all meta properties from a label instance. Meta properties (indicated by a preceding "_" in th...
static nlohmann::json SerializeMultLabelGroupsToJSON(const mitk::MultiLabelSegmentation *inputSegmentation, GroupFileNameCallback groupFileNameCallback=nullptr, LabelFileNameCallback labelFileNameCallback=nullptr, LabelFileValueCallback labelFileValueCallback=nullptr)
Serialize meta data all groups of the multilabel segmentation to JSON format (v2)
static bool LoadMultiLabelSegmentationPreset(const std::string &presetFilename, mitk::MultiLabelSegmentation *inputSegmentation)
Loads an existing preset for a mitk::MultiLabelSegmentation from presetFilename and applies it to inp...
static int GetIntByKey(const itk::MetaDataDictionary &dic, const std::string &key)
static tinyxml2::XMLElement * GetLabelAsXMLElement(tinyxml2::XMLDocument &doc, const Label *label)
Creates an XML element from a mitk::Label.
static tinyxml2::XMLElement * PropertyToXMLElement(tinyxml2::XMLDocument &doc, const std::string &key, const BaseProperty *property)
Since a mitk::Label is basically a mitk::PropertyList this function converts the label's properties i...
std::function< mitk::MultiLabelSegmentation::LabelValueType(const mitk::MultiLabelSegmentation *, mitk::MultiLabelSegmentation::LabelValueType)> LabelFileValueCallback
static bool SaveMultiLabelSegmentationPreset(const std::string &presetFilename, const mitk::MultiLabelSegmentation *inputSegmentation)
Saves the mitk::LabelSet configuration of inputSegmentation to presetFilename. The preset is stored a...
static bool PropertyFromXMLElement(std::string &key, itk::SmartPointer< mitk::BaseProperty > &prop, const tinyxml2::XMLElement *elem)
Since a mitk::Label is basically a mitk::PropertyList this function converts an XML element into a pr...
static std::vector< LabelGroupMetaData > DeserializeMultiLabelGroupsFromJSON(const nlohmann::json &listOfLabelGroups)
Deserialize meta data of multilabel groups from JSON format (v2)
static nlohmann::json SerializeLabelPropertyToJSON(const BaseProperty *property)
Serialize the property of a label to JSON.
static mitk::Label::Pointer DeserializeLabelFromJSON(const nlohmann::json &labelJson)
Deserialize a single label from JSON.
static itk::SmartPointer< mitk::Label > LoadLabelFromXMLDocument(const tinyxml2::XMLElement *labelElem)
Creates a mitk::Label from an XML element.
static nlohmann::json SerializeLabelToJSON(const Label *label)
Serialize a single label to JSON.
static std::string GetStringByKey(const itk::MetaDataDictionary &dic, const std::string &key)
static bool GetValueFromJson(const nlohmann::json &labelJson, const std::string &key, TValueType &value)
static LabelVector CloneLabelsWithoutMetaProperties(const LabelVector &labels)
Remove all meta properties from all label clones of the passed vector. Meta properties (indicated by ...
std::function< std::string(const mitk::MultiLabelSegmentation *, mitk::MultiLabelSegmentation::LabelValueType)> LabelFileNameCallback
std::function< std::string(const mitk::MultiLabelSegmentation *, mitk::MultiLabelSegmentation::GroupIndexType)> GroupFileNameCallback
MultiLabelSegmentation class for handling labels and layers in a segmentation session.
mitk::Label::PixelType LabelValueType
Key-value list holding instances of BaseProperty.
#define MITK_ERROR
Definition: mitkLog.h:211
nlohmann::json json
Find image slices visible on a given plane.
constexpr const char *const PROPERTY_NAME_TIMEGEOMETRY_TYPE
constexpr const char *const PROPERTY_KEY_TIMEGEOMETRY_TYPE
std::vector< Label::Pointer > LabelVector
Definition: mitkLabel.h:306
constexpr const char *const PROPERTY_KEY_TIMEGEOMETRY_TIMEPOINTS
constexpr const char *const PROPERTY_NAME_TIMEGEOMETRY_TIMEPOINTS
constexpr const char *const PROPERTY_KEY_UID
Structure to hold label group metadata including labels and group properties.
LabelGroupMetaData(const std::string &groupName, const LabelVector &groupLabels, PropertyList *groupProperties)
LabelGroupMetaData(const std::string &groupName, const LabelVector &groupLabels)