Medical Imaging Interaction Toolkit  2023.04.00
Medical Imaging Interaction Toolkit
mitkLabelSetImage.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 mitkLabelSetImage_h
14 #define mitkLabelSetImage_h
15 
16 #include <mitkImage.h>
17 #include <mitkLabelSet.h>
18 
19 #include <MitkMultilabelExports.h>
20 
21 namespace mitk
22 {
23  //##Documentation
24  //## @brief LabelSetImage class for handling labels and layers in a segmentation session.
25  //##
26  //## Handles operations for adding, removing, erasing and editing labels and layers.
27  //## @ingroup Data
28 
30  {
31  public:
33  itkNewMacro(Self);
34 
36 
43 
50 
53  // FUTURE MultiLabelSegmentation:
54  // Section that already contains declarations used in the new class.
55  // So this part of the interface will stay after refactoring towards
56  // the new MultiLabelSegmentation class (see T28524). This section was introduced
57  // because some of the planned features are already urgently needed.
60 
61  using GroupIndexType = std::size_t;
63  const static LabelValueType UnlabeledValue = 0;
64  using ConstLabelVectorType = std::vector<Label::ConstPointer>;
65  using LabelVectorType = std::vector<Label::Pointer>;
66  using LabelValueVectorType = std::vector<LabelValueType>;
67 
74  void RemoveLabel(LabelValueType labelValue);
75 
81  void RemoveLabels(const LabelValueVectorType& vectorOfLabelPixelValues);
82 
91  void RemoveGroup(GroupIndexType group);
92 
93  //future declaration (T28524) currently conflicted with old declaration
95  // * \brief Returns true if the value exists in the MultiLabelSegmentation instance*/
96  //bool ExistLabel(LabelValueType value) const;
97 
99  // * @brief Checks if a label belongs in a certain spatial group
100  // * @param value the label value
101  // * @param groupIndex Indexp of the spacial group which should be checked for the label
102  // * @return true if the label exists otherwise false
103  // */
104  //bool ExistLabel(LabelValueType value, GroupIndexType groupIndex) const;
105 
111  bool ExistGroup(GroupIndexType index) const;
112 
113  bool IsLabelInGroup(LabelValueType value) const;
114  bool IsLabelInGroup(LabelValueType value, GroupIndexType& groupIndex) const;
115 
119  GroupIndexType GetGroupIndexOfLabel(LabelValueType value) const;
120 
126  const mitk::Label* GetLabel(LabelValueType value) const;
127  mitk::Label* GetLabel(LabelValueType value);
128 
131  bool IsLabelLocked(LabelValueType value) const;
132 
135  const ConstLabelVectorType GetLabels() const;
136  const LabelVectorType GetLabels();
137 
145  const ConstLabelVectorType GetLabelsInGroup(GroupIndexType index) const;
146  const LabelVectorType GetLabelsInGroup(GroupIndexType index);
147 
148  itkGetConstMacro(UnlabeledLabelLock, bool);
149  itkSetMacro(UnlabeledLabelLock, bool);
150  itkBooleanMacro(UnlabeledLabelLock);
151 
153  //Message slots that allow to react to changes in an instance
154 
158 
170 
186  mitkNewMessage1Macro(LabelModified, LabelValueType);
187 
198  mitkNewMessage1Macro(LabelRemoved, LabelValueType);
199 
215 
227 
239  mitkNewMessage1Macro(GroupModified, GroupIndexType);
240 
251  mitkNewMessage1Macro(GroupRemoved, GroupIndexType);
252 
253  protected:
254 
255  void OnLabelAdded(LabelValueType labelValue);
256  void AddLabelToMap(LabelValueType labelValue, mitk::Label* label, GroupIndexType groupID);
257  void OnLabelModified(LabelValueType labelValue);
258  void OnLabelRemoved(LabelValueType labelValue);
259  void OnGroupAdded(GroupIndexType groupIndex);
260  void OnGroupModified(GroupIndexType groupIndex);
261  void OnGroupRemoved(GroupIndexType groupIndex);
262 
265  void ReinitMaps();
266 
267  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
269 
272  using GroupToLabelMapType = std::map<GroupIndexType, LabelValueVectorType>;
274  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
276 
277  private:
279  bool m_UnlabeledLabelLock;
280 
281  public:
282 
285  void UpdateCenterOfMass(PixelType pixelValue);
286 
289  // END FUTURE MultiLabelSegmentation
292 
299  void Initialize(const mitk::Image *image) override;
300 
303  void ClearBuffer();
304 
312  void MergeLabel(PixelType pixelValue, PixelType sourcePixelValue, unsigned int layer = 0);
313 
321  void MergeLabels(PixelType pixelValue, const std::vector<PixelType>& vectorOfSourcePixelValues, unsigned int layer = 0);
322 
325  void UpdateCenterOfMass(PixelType pixelValue, unsigned int layer);
326 
333  void EraseLabel(PixelType pixelValue);
334 
340  void EraseLabels(const std::vector<PixelType> &VectorOfLabelPixelValues);
341 
344  //[[deprecated("Will be changed with T28524")]]
345  DEPRECATED(bool ExistLabel(PixelType pixelValue) const);
346 
353  //[[deprecated("Will be changed with T28524")]]
354  DEPRECATED(bool ExistLabel(PixelType pixelValue, unsigned int layer) const);
355 
358  //[[deprecated("Will be removed with T28524")]]
359  DEPRECATED(bool ExistLabelSet(unsigned int layer) const);
360 
366  //[[deprecated("Will be removed with T28524")]]
367  DEPRECATED(mitk::Label *GetActiveLabel(unsigned int layer = 0));
368  //[[deprecated("Will be removed with T28524")]]
369  DEPRECATED(const mitk::Label* GetActiveLabel(unsigned int layer = 0) const);
370 
377  mitk::Label *GetLabel(PixelType pixelValue, unsigned int layer) const;
378 
383  //[[deprecated ("Will be removed with T28524")]]
384  DEPRECATED(mitk::LabelSet *GetActiveLabelSet());
385  //[[deprecated("Will be removed with T28524")]]
386  DEPRECATED(const mitk::LabelSet* GetActiveLabelSet() const);
387 
393  mitk::LabelSet *GetLabelSet(unsigned int layer = 0);
394  const mitk::LabelSet *GetLabelSet(unsigned int layer = 0) const;
395 
400  unsigned int GetActiveLayer() const;
401 
407  unsigned int GetNumberOfLabels(unsigned int layer = 0) const;
408 
413  unsigned int GetTotalNumberOfLabels() const;
414 
415  // This function will need to be ported to an external class
416  // it requires knowledge of pixeltype and dimension and includes
417  // too much algorithm to be sensibly part of a data class
419  // * \brief */
420  // void SurfaceStamp(mitk::Surface* surface, bool forceOverwrite);
421 
424  mitk::Image::Pointer CreateLabelMask(PixelType index, bool useActiveLayer = true, unsigned int layer = 0);
425 
433  void InitializeByLabeledImage(mitk::Image::Pointer image);
434 
437  void MaskStamp(mitk::Image *mask, bool forceOverwrite);
438 
441  void SetActiveLayer(unsigned int layer);
442 
445  unsigned int GetNumberOfLayers() const;
446 
452  unsigned int AddLayer(mitk::LabelSet::Pointer labelSet = nullptr);
453 
460  unsigned int AddLayer(mitk::Image::Pointer layerImage, mitk::LabelSet::Pointer labelSet = nullptr);
461 
477  void AddLabelSetToLayer(const unsigned int layerIdx, const mitk::LabelSet* labelSet);
478 
483  void RemoveLayer();
484 
487  mitk::Image *GetLayerImage(unsigned int layer);
488 
489  const mitk::Image *GetLayerImage(unsigned int layer) const;
490 
491  void OnLabelSetModified();
492 
493  protected:
495 
496  LabelSetImage();
497  LabelSetImage(const LabelSetImage &other);
498  ~LabelSetImage() override;
499 
500  template <typename TPixel, unsigned int VImageDimension>
501  void LayerContainerToImageProcessing(itk::Image<TPixel, VImageDimension> *source, unsigned int layer);
502 
503  template <typename TPixel, unsigned int VImageDimension>
504  void ImageToLayerContainerProcessing(itk::Image<TPixel, VImageDimension> *source, unsigned int layer) const;
505 
506  template <typename ImageType>
507  void CalculateCenterOfMassProcessing(ImageType *input, PixelType index, unsigned int layer);
508 
509  template <typename ImageType>
510  void ClearBufferProcessing(ImageType *input);
511 
512  template <typename ImageType>
513  void EraseLabelProcessing(ImageType *input, PixelType index);
514 
515  template <typename ImageType>
516  void MergeLabelProcessing(ImageType *input, PixelType pixelValue, PixelType index);
517 
518  template <typename ImageType>
519  void MaskStampProcessing(ImageType *input, mitk::Image *mask, bool forceOverwrite);
520 
521  template <typename LabelSetImageType, typename ImageType>
522  void InitializeByLabeledImageProcessing(LabelSetImageType *input, ImageType *other);
523 
526  LabelValueVectorType GetUsedLabelValues() const;
527 
528  //helper function that ensures
529  void RegisterLabelSet(mitk::LabelSet* ls);
530  void ReleaseLabelSet(mitk::LabelSet* ls);
531 
532  std::vector<LabelSet::Pointer> m_LabelSetContainer;
533  std::vector<Image::Pointer> m_LayerContainer;
534 
536 
538  };
539 
557  MITKMULTILABEL_EXPORT bool Equal(const mitk::LabelSetImage &leftHandSide,
558  const mitk::LabelSetImage &rightHandSide,
559  ScalarType eps,
560  bool verbose);
561 
562 
566  namespace MultiLabelSegmentation
567  {
568  enum class MergeStyle
569  {
570  Replace, //The old label content of a lable value will be replaced by its new label content.
571  //Therefore pixels that are labeled might become unlabeled again.
572  //(This means that a lock of the value is also ignored).
573  Merge //The union of old and new label content will be generated.
574  };
575 
576  enum class OverwriteStyle
577  {
578  RegardLocks, //Locked labels in the same spatial group will not be overwritten/changed.
579  IgnoreLocks //Label locks in the same spatial group will be ignored, so these labels might be changed.
580  };
581  }
582 
608  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const LabelSetImage* sourceImage, LabelSetImage* destinationImage,
609  const TimeStepType timeStep, std::vector<std::pair<Label::PixelType, Label::PixelType> > labelMapping = { {1,1} },
612 
617  MITKMULTILABEL_EXPORT void TransferLabelContent(const LabelSetImage* sourceImage, LabelSetImage* destinationImage, std::vector<std::pair<Label::PixelType, Label::PixelType> > labelMapping = { {1,1} },
620 
621 
650  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::LabelSet* destinationLabelSet,
651  const TimeStepType timeStep, mitk::Label::PixelType sourceBackground = LabelSetImage::UnlabeledValue,
653  bool destinationBackgroundLocked = false,
654  std::vector<std::pair<Label::PixelType, Label::PixelType> > labelMapping = { {1,1} },
657 
662  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::LabelSet* destinationLabelSet,
665  bool destinationBackgroundLocked = false,
666  std::vector<std::pair<Label::PixelType, Label::PixelType> > labelMapping = { {1,1} },
669 
670 } // namespace mitk
671 
672 #endif
mitk::eps
const MITKCORE_EXPORT ScalarType eps
mitk::MultiLabelSegmentation::OverwriteStyle::IgnoreLocks
@ IgnoreLocks
MITKMULTILABEL_EXPORT
#define MITKMULTILABEL_EXPORT
Definition: MitkMultilabelExports.h:15
mitk::LabelSetImage::BeforeChangeLayerEvent
Message BeforeChangeLayerEvent
BeforeChangeLayerEvent (e.g. used for GUI integration) As soon as active labelset should be changed,...
Definition: mitkLabelSetImage.h:42
mitk::LabelSetImage::m_LabelToGroupMap
LabelToGroupMapType m_LabelToGroupMap
Definition: mitkLabelSetImage.h:275
mitk::Message1< LabelValueType >
mitkImage.h
mitk::MultiLabelSegmentation::OverwriteStyle::RegardLocks
@ RegardLocks
mitk::LabelSetImage::LabelVectorType
std::vector< Label::Pointer > LabelVectorType
Definition: mitkLabelSetImage.h:65
mitkLabelSet.h
mitk::LabelSetImage::m_GroupToLabelMap
GroupToLabelMapType m_GroupToLabelMap
Definition: mitkLabelSetImage.h:273
mitk::LabelSet
Definition: mitkLabelSet.h:34
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
DEPRECATED
#define DEPRECATED(func)
Definition: mitkCommon.h:175
mitk::Equal
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
mitk::LabelSetImage::m_activeLayerInvalid
bool m_activeLayerInvalid
Definition: mitkLabelSetImage.h:537
mitk::LabelSetImage::LabelToGroupMapType
std::map< LabelValueType, GroupIndexType > LabelToGroupMapType
Definition: mitkLabelSetImage.h:274
itk::SmartPointer< Self >
mitk::LabelSetImage::AfterChangeLayerEvent
Message AfterChangeLayerEvent
AfterchangeLayerEvent (e.g. used for GUI integration) As soon as active labelset was changed,...
Definition: mitkLabelSetImage.h:49
mitk::Label::PixelType
unsigned short PixelType
Definition: mitkLabel.h:33
mitk::LabelSetImage::GroupToLabelMapType
std::map< GroupIndexType, LabelValueVectorType > GroupToLabelMapType
Definition: mitkLabelSetImage.h:272
mitk::LabelSetImage::m_LabelMap
LabelMapType m_LabelMap
Definition: mitkLabelSetImage.h:268
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::LabelSetImage::ConstLabelVectorType
std::vector< Label::ConstPointer > ConstLabelVectorType
Definition: mitkLabelSetImage.h:64
mitkNewMessage1Macro
#define mitkNewMessage1Macro(msgHandleObject, type1)
Definition: mitkMessage.h:52
mitk::Label
A data structure describing a label.
Definition: mitkLabel.h:28
mitk::MultiLabelSegmentation::MergeStyle::Replace
@ Replace
mitk::LabelSetImage::UnlabeledValue
const static LabelValueType UnlabeledValue
Definition: mitkLabelSetImage.h:63
mitk::MultiLabelSegmentation::MergeStyle
MergeStyle
Definition: mitkLabelSetImage.h:568
mitk::LabelSetImage::m_ActiveLayer
int m_ActiveLayer
Definition: mitkLabelSetImage.h:535
mitk::LabelSetImage::m_LayerContainer
std::vector< Image::Pointer > m_LayerContainer
Definition: mitkLabelSetImage.h:533
mitk::LabelSetImage::LabelMapType
std::map< LabelValueType, Label::Pointer > LabelMapType
Definition: mitkLabelSetImage.h:267
mitk::BaseData
Base of all data objects.
Definition: mitkBaseData.h:42
mitk::MultiLabelSegmentation::OverwriteStyle
OverwriteStyle
Definition: mitkLabelSetImage.h:576
mitk::MultiLabelSegmentation::MergeStyle::Merge
@ Merge
mitk::TimeStepType
std::size_t TimeStepType
Definition: mitkTimeGeometry.h:27
mitkCloneMacro
#define mitkCloneMacro(classname)
Definition: mitkCommon.h:154
mitkClassMacro
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:36
itk::Image
class ITK_EXPORT Image
Definition: mitkGeometryClipImageFilter.h:25
mitk::Image::Initialize
void Initialize() override
mitk::LabelSetImage::m_LabelSetContainer
std::vector< LabelSet::Pointer > m_LabelSetContainer
Definition: mitkLabelSetImage.h:532
mitk::TransferLabelContent
MITKMULTILABEL_EXPORT void TransferLabelContent(const LabelSetImage *sourceImage, LabelSetImage *destinationImage, std::vector< std::pair< Label::PixelType, Label::PixelType > > labelMapping={ {1, 1} }, MultiLabelSegmentation::MergeStyle mergeStyle=MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle overwriteStlye=MultiLabelSegmentation::OverwriteStyle::RegardLocks)
mitk::TransferLabelContentAtTimeStep
MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const LabelSetImage *sourceImage, LabelSetImage *destinationImage, const TimeStepType timeStep, std::vector< std::pair< Label::PixelType, Label::PixelType > > labelMapping={ {1, 1} }, MultiLabelSegmentation::MergeStyle mergeStyle=MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle overwriteStlye=MultiLabelSegmentation::OverwriteStyle::RegardLocks)
mitk::LabelSetImage::PixelType
mitk::Label::PixelType PixelType
Definition: mitkLabelSetImage.h:33
mitk::LabelSetImage::GroupIndexType
std::size_t GroupIndexType
Definition: mitkLabelSetImage.h:61
mitk::LabelSetImage::LabelValueVectorType
std::vector< LabelValueType > LabelValueVectorType
Definition: mitkLabelSetImage.h:66
mitk::Message
Event/message/notification class.
Definition: mitkMessage.h:452
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
mitk::LabelSetImage::LabelValueType
mitk::Label::PixelType LabelValueType
Definition: mitkLabelSetImage.h:62
MitkMultilabelExports.h