Medical Imaging Interaction Toolkit  2024.12.99-01bcec80
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 SaveLabelSetImagePreset(const std::string &presetFilename,
61  const mitk::MultiLabelSegmentation *inputImage);
62 
69  static bool LoadLabelSetImagePreset(const std::string &presetFilename,
70  mitk::MultiLabelSegmentation *inputImage);
71 
77  static itk::SmartPointer<mitk::Label> LoadLabelFromXMLDocument(const tinyxml2::XMLElement *labelElem);
78 
85  static tinyxml2::XMLElement *GetLabelAsXMLElement(tinyxml2::XMLDocument &doc, const Label *label);
86 
95  static tinyxml2::XMLElement *PropertyToXMLElement(tinyxml2::XMLDocument& doc, const std::string &key, const BaseProperty *property);
96 
104  static bool PropertyFromXMLElement(std::string &key, itk::SmartPointer<mitk::BaseProperty> &prop, const tinyxml2::XMLElement *elem);
105 
108  static int GetIntByKey(const itk::MetaDataDictionary& dic, const std::string& key);
111  static std::string GetStringByKey(const itk::MetaDataDictionary& dic, const std::string& key);
112 
117  {
118  std::string name;
120  PropertyList::Pointer properties; // For custom group properties
121 
123  LabelGroupMetaData(const std::string& groupName, const LabelVector& groupLabels);
124  LabelGroupMetaData(const std::string& groupName, const LabelVector& groupLabels, PropertyList* groupProperties);
125  ~LabelGroupMetaData() = default;
126  };
127 
128 
132 
144  static nlohmann::json SerializeMultLabelGroupsToJSON(const mitk::MultiLabelSegmentation* inputSegmentation,
145  GroupFileNameCallback groupFileNameCallback = nullptr ,
146  LabelFileNameCallback labelFileNameCallback = nullptr,
147  LabelFileValueCallback labelFileValueCallback = nullptr);
148 
154  static std::vector<LabelGroupMetaData> DeserializeMultiLabelGroupsFromJSON(const nlohmann::json& listOfLabelGroups);
155 
161  static nlohmann::json SerializeLabelToJSON(const Label* label);
162 
168  static mitk::Label::Pointer DeserializeLabelFromJSON(const nlohmann::json& labelJson);
169 
175  static nlohmann::json SerializeLabelPropertyToJSON(const BaseProperty* property);
176 
177  template<typename TValueType>
178  static bool GetValueFromJson(const nlohmann::json& labelJson, const std::string& key, TValueType& value)
179  {
180  if (labelJson.find(key) != labelJson.end())
181  {
182  try
183  {
184  value = labelJson[key].get<TValueType>();
185  return true;
186  }
187  catch (...)
188  {
189  MITK_ERROR << "Unable to read label information from json. Value has wrong type. Failed key: " << key << "; invalid value: " << labelJson[key].dump();
190  throw;
191  }
192  }
193  return false;
194  }
195 
196  private:
198  };
199 }
200 
201 #endif
MITKMULTILABEL_EXPORT
#define MITKMULTILABEL_EXPORT
Definition: MitkMultilabelExports.h:15
mitk::MultiLabelIOHelper::LabelFileNameCallback
std::function< std::string(const mitk::MultiLabelSegmentation *, mitk::MultiLabelSegmentation::LabelValueType)> LabelFileNameCallback
Definition: mitkMultiLabelIOHelper.h:130
mitkLabel.h
mitk::BaseProperty
Abstract base class for properties.
Definition: mitkBaseProperty.h:36
mitk::MultiLabelIOHelper
The MultiLabelIOHelper is a static helper class that supports serialization of mitk::MultiLabelSegmen...
Definition: mitkMultiLabelIOHelper.h:50
mitk::PROPERTY_KEY_TIMEGEOMETRY_TIMEPOINTS
const constexpr char *const PROPERTY_KEY_TIMEGEOMETRY_TIMEPOINTS
Definition: mitkMultiLabelIOHelper.h:41
mitk::MultiLabelIOHelper::LabelGroupMetaData::properties
PropertyList::Pointer properties
Definition: mitkMultiLabelIOHelper.h:120
MITK_ERROR
#define MITK_ERROR
Definition: mitkLog.h:211
itk::SmartPointer
Definition: mitkIFileReader.h:30
mitk::PROPERTY_KEY_TIMEGEOMETRY_TYPE
const constexpr char *const PROPERTY_KEY_TIMEGEOMETRY_TYPE
Definition: mitkMultiLabelIOHelper.h:40
mitk::MultiLabelIOHelper::LabelFileValueCallback
std::function< mitk::MultiLabelSegmentation::LabelValueType(const mitk::MultiLabelSegmentation *, mitk::MultiLabelSegmentation::LabelValueType)> LabelFileValueCallback
Definition: mitkMultiLabelIOHelper.h:131
mitk::MultiLabelSegmentation::GroupIndexType
std::size_t GroupIndexType
Definition: mitkLabelSetImage.h:56
mitk::MultiLabelIOHelper::GetValueFromJson
static bool GetValueFromJson(const nlohmann::json &labelJson, const std::string &key, TValueType &value)
Definition: mitkMultiLabelIOHelper.h:178
mitk::MultiLabelSegmentation::LabelValueType
mitk::Label::PixelType LabelValueType
Definition: mitkLabelSetImage.h:57
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::MultiLabelIOHelper::GroupFileNameCallback
std::function< std::string(const mitk::MultiLabelSegmentation *, mitk::MultiLabelSegmentation::GroupIndexType)> GroupFileNameCallback
Definition: mitkMultiLabelIOHelper.h:129
mitk::Label
A data structure describing a label.
Definition: mitkLabel.h:29
json
nlohmann::json json
Definition: mitkModelTestFixture.h:29
mitk::MultiLabelIOHelper::LabelGroupMetaData::name
std::string name
Definition: mitkMultiLabelIOHelper.h:118
mitk::MultiLabelSegmentation
MultiLabelSegmentation class for handling labels and layers in a segmentation session.
Definition: mitkLabelSetImage.h:43
mitk::MultiLabelIOHelper::LabelGroupMetaData
Structure to hold label group metadata including labels and group properties.
Definition: mitkMultiLabelIOHelper.h:116
mitk::PROPERTY_KEY_UID
const constexpr char *const PROPERTY_KEY_UID
Definition: mitkMultiLabelIOHelper.h:42
mitk::PROPERTY_NAME_TIMEGEOMETRY_TIMEPOINTS
const constexpr char *const PROPERTY_NAME_TIMEGEOMETRY_TIMEPOINTS
Definition: mitkMultiLabelIOHelper.h:39
mitk::PropertyList
Key-value list holding instances of BaseProperty.
Definition: mitkPropertyList.h:56
mitk::LabelVector
std::vector< Label::Pointer > LabelVector
Definition: mitkLabel.h:95
mitk::MultiLabelIOHelper::LabelGroupMetaData::labels
LabelVector labels
Definition: mitkMultiLabelIOHelper.h:119
itk
SET FUNCTIONS.
Definition: itkIntelligentBinaryClosingFilter.h:30
tinyxml2
Definition: mitkContourModelReader.h:25
mitk::PROPERTY_NAME_TIMEGEOMETRY_TYPE
const constexpr char *const PROPERTY_NAME_TIMEGEOMETRY_TYPE
Definition: mitkMultiLabelIOHelper.h:38
MitkMultilabelExports.h
mitkLabelSetImage.h