Medical Imaging Interaction Toolkit  2025.08.99-f7084adb
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 mitkMultiLabelSegmentation_h
14 #define mitkMultiLabelSegmentation_h
15 
16 #include <shared_mutex>
17 #include <mitkImage.h>
18 #include <mitkLabel.h>
19 #include <mitkLookupTable.h>
20 #include <mitkMultiLabelEvents.h>
21 #include <mitkMessage.h>
23 
24 #include <MitkMultilabelExports.h>
25 
26 namespace mitk
27 {
44  {
45  public:
52 
54  itkNewMacro(Self);
55 
56  using GroupIndexType = std::size_t;
60  using LabelValueVectorType = std::vector<LabelValueType>;
61 
63  //* Returns the pixel type used in group images*/
64  static PixelType GetPixelType();
65 
66  enum class MergeStyle
67  {
68  Replace, //The old label content of a label value will be replaced by its new label content.
69  //Therefore pixels that are labeled might become unlabeled again.
70  //(This means that a lock of the value is also ignored).
71  Merge //The union of old and new label content will be generated.
72  };
73 
74  enum class OverwriteStyle
75  {
76  RegardLocks, //Locked labels in the same spatial group will not be overwritten/changed.
77  IgnoreLocks //Label locks in the same spatial group will be ignored, so these labels might be changed.
78  };
79 
83  bool IsSliceSet(int s = 0, int t = 0, int n = 0) const override;
84 
88  bool IsVolumeSet(int t = 0, int n = 0) const override;
89 
93  bool IsChannelSet(int n = 0) const override;
94 
98  unsigned int GetDimension() const;
99  using GroupImageDimensionVectorType = std::vector<unsigned int>;
104  const GroupImageDimensionVectorType& GetDimensions() const;
105 
122  mitk::Label* AddLabel(Label* label, GroupIndexType groupID, bool addAsClone = true, bool correctLabelValue = true);
123 
144  mitk::Label* AddLabelWithContent(Label* label, const Image* labelContent, GroupIndexType groupID, LabelValueType contentLabelValue, bool addAsClone = true, bool correctLabelValue = true);
145 
153  mitk::Label* AddLabel(const std::string& name, const Color& color, GroupIndexType groupID);
154 
161  void RenameLabel(LabelValueType labelValue, const std::string& name, const Color& color);
162 
170  void RemoveLabel(LabelValueType labelValue);
171 
179  void RemoveLabels(const LabelValueVectorType& vectorOfLabelPixelValues);
180 
188  void EraseLabel(LabelValueType labelValue);
189 
196  void EraseLabels(const LabelValueVectorType& labelValues);
197 
213  void MergeLabel(LabelValueType targetLabelValue, LabelValueType sourceLabelValue,
214  OverwriteStyle overwriteStyle = OverwriteStyle::RegardLocks);
215 
229  void MergeLabels(LabelValueType targetLabelValue, const LabelValueVectorType& sourceLabelValues,
230  OverwriteStyle overwriteStyle = OverwriteStyle::RegardLocks);
231 
238  GroupIndexType AddGroup(ConstLabelVector labels = {});
239 
249  GroupIndexType AddGroup(mitk::Image* layerImage, ConstLabelVector labels = {});
250 
259  void InsertGroup(GroupIndexType groupID, ConstLabelVector labels = {}, std::string name = "");
260 
274  void InsertGroup(GroupIndexType groupID, mitk::Image* layerImage, ConstLabelVector labels = {}, std::string name = "");
275 
284  void RemoveGroup(GroupIndexType group);
285 
286 
288  bool ExistLabel(LabelValueType value) const;
289 
296  bool ExistLabel(LabelValueType value, GroupIndexType groupIndex) const;
297 
303  bool ExistGroup(GroupIndexType index) const;
304 
308  GroupIndexType GetGroupIndexOfLabel(LabelValueType value) const;
309 
320  mitk::Label::ConstPointer GetLabel(LabelValueType value) const;
321  mitk::Label::Pointer GetLabel(LabelValueType value);
322 
325  const ConstLabelVectorType GetLabels() const;
326  const LabelVectorType GetLabels();
327 
330  const LabelValueVectorType GetAllLabelValues() const;
331 
338  const LabelVectorType GetLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = true);
339 
344  const ConstLabelVectorType GetConstLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = false) const;
345 
347  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const ConstLabelVectorType& labels);
349  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const LabelVectorType& labels);
350 
352  static ConstLabelVectorType ConvertLabelVectorConst(const LabelVectorType& labels);
353 
361  const LabelValueVectorType GetLabelValuesByGroup(GroupIndexType index) const;
362 
371  const LabelValueVectorType GetLabelValuesByName(GroupIndexType index, const std::string_view name) const;
372 
376  std::vector<std::string> GetLabelClassNames() const;
377 
382  std::vector<std::string> GetLabelClassNamesByGroup(GroupIndexType index) const;
383 
388  LabelValueType GetUnusedLabelValue() const;
389 
390  itkGetConstMacro(UnlabeledLabelLock, bool);
391  itkSetMacro(UnlabeledLabelLock, bool);
392  itkBooleanMacro(UnlabeledLabelLock);
393 
396  void SetAllLabelsVisible(bool visible);
397 
401  void SetAllLabelsVisibleByGroup(GroupIndexType group, bool visible);
402 
407  void SetAllLabelsVisibleByName(GroupIndexType group, const std::string_view name, bool visible);
408 
411  bool IsLabelLocked(LabelValueType value) const;
412 
415  void SetAllLabelsLocked(bool locked);
416 
420  void SetAllLabelsLockedByGroup(GroupIndexType group, bool locked);
421 
426  void SetAllLabelsLockedByName(GroupIndexType group, const std::string_view name, bool locked);
427 
439  void ReplaceGroupLabels(const GroupIndexType groupID, const ConstLabelVectorType& newLabels);
440 
441  void ReplaceGroupLabels(const GroupIndexType groupID, const LabelVectorType& newLabels);
442 
455  void ReplaceLabels(const ConstLabelVectorType& labelsUpdates);
456 
457  void ReplaceLabels(const LabelVectorType& newLabels);
458 
461  mitk::Image* GetGroupImage(GroupIndexType groupID);
462 
465  const mitk::Image* GetGroupImage(GroupIndexType groupID) const;
466 
476  void UpdateGroupImage(GroupIndexType groupID, const mitk::Image* sourceImage, TimeStepType timestep, TimeStepType sourceTimestep = 0);
477 
482  void ClearGroupImage(GroupIndexType groupID);
488  void ClearGroupImage(GroupIndexType groupID, TimeStepType timestep);
489 
494  void ClearGroupImages();
499  void ClearGroupImages(TimeStepType timestep);
500 
505  const std::string& GetGroupName(GroupIndexType groupID) const;
506 
509  void SetGroupName(GroupIndexType groupID, const std::string& name);
510 
511  itkGetModifiableObjectMacro(LookupTable, mitk::LookupTable);
512  void SetLookupTable(LookupTable* lut);
516  void UpdateLookupTable(LabelValueType pixelValue);
517 
520  void Initialize() override;
521 
536  void Initialize(const mitk::Image* templateImage, bool resetLabels = true, bool ensure1stGroup = true);
537 
552  void Initialize(const mitk::TimeGeometry* geometry, bool resetLabels = true, bool ensure1stGroup = true);
553 
560  void InitializeByLabeledImage(const mitk::Image* image);
561 
563  itk::ModifiedTimeType GetMTime() const override;
564 
567  void UpdateCenterOfMass(LabelValueType pixelValue);
568 
569  using BaseData::IsEmpty;
570 
573  bool IsEmpty(const Label* label, TimeStepType t = 0) const;
574  bool IsEmpty(LabelValueType pixelValue, TimeStepType t = 0) const;
575 
581  unsigned int GetActiveLayer() const;
582 
583  void SetActiveLabel(LabelValueType label);
584  Label* GetActiveLabel();
585  const Label* GetActiveLabel() const;
586 
592  unsigned int GetNumberOfLabels(unsigned int group) const;
593 
598  unsigned int GetTotalNumberOfLabels() const;
599 
600  unsigned int GetNumberOfGroups() const;
601 
602  protected:
603  mitkCloneMacro(Self);
604 
605  MultiLabelSegmentation();
606  MultiLabelSegmentation(const MultiLabelSegmentation& other);
607  ~MultiLabelSegmentation() override;
608 
609  void OnLabelModified(const Object* sender, const itk::EventObject&);
610 
612  void AddLabelToMap(LabelValueType labelValue, Label* label, GroupIndexType groupID);
613  void RemoveLabelFromMap(LabelValueType labelValue);
615  void RegisterLabel(Label* label);
617  void ReleaseLabel(Label* label);
618 
621  void ApplyToLabels(const LabelValueVectorType& values, std::function<void(Label*)>&& lambda);
625  void VisitLabels(const LabelValueVectorType& values, std::function<void(const Label*)>&& lambda) const;
626 
628 
629  template <typename ImageType>
630  void CalculateCenterOfMassProcessing(ImageType* input, LabelValueType index);
631 
632  template <typename ImageType>
633  void EraseLabelProcessing(ImageType* input, LabelValueType index);
634 
635  template <typename MultiLabelSegmentationType, typename ImageType>
636  void InitializeByLabeledImageProcessing(MultiLabelSegmentationType* input, const ImageType* other);
637 
640  LabelValueVectorType GetUsedLabelValues() const;
641 
642  private:
645  Image::Pointer GenerateNewGroupImage() const;
646 
647  std::vector<Image::Pointer> m_GroupContainer;
648 
649  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
651  LabelMapType m_LabelMap;
652 
653  using GroupNameVectorType = std::vector<std::string>;
655  GroupNameVectorType m_Groups;
656 
659  using GroupToLabelMapType = std::vector<LabelValueVectorType>;
660  /* Dictionary that maps between group id (key) and label values in the group (vector of label value).*/
661  GroupToLabelMapType m_GroupToLabelMap;
662  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
663  /* Dictionary that maps between label value (key) and group id (value)*/
664  LabelToGroupMapType m_LabelToGroupMap;
665 
666  using LabelEventGuardMapType = std::map<LabelValueType, ITKEventObserverGuard>;
667  LabelEventGuardMapType m_LabelModEventGuardMap;
668 
669  LookupTable::Pointer m_LookupTable;
670 
672  bool m_UnlabeledLabelLock;
673 
675  std::shared_mutex m_LabelNGroupMapsMutex;
676 
679  GroupImageDimensionVectorType m_GroupImageDimensions;
680  };
681 
700  const mitk::MultiLabelSegmentation &rightHandSide,
701  ScalarType eps,
702  bool verbose);
703 
720  ScalarType eps,
721  bool verbose);
722 
739  bool orderIsRelevant = false);
740 
741 
742  using LabelValueMappingVector = std::vector < std::pair<Label::PixelType, Label::PixelType> >;
743 
769  const TimeStepType timeStep, LabelValueMappingVector labelMapping = { {1,1} },
772 
777  MITKMULTILABEL_EXPORT void TransferLabelContent(const MultiLabelSegmentation* sourceImage, MultiLabelSegmentation* destinationImage, LabelValueMappingVector labelMapping = { {1,1} },
780 
781 
810  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
813  bool destinationBackgroundLocked = false,
814  LabelValueMappingVector labelMapping = { {1,1} },
817 
822  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
825  bool destinationBackgroundLocked = false,
826  LabelValueMappingVector labelMapping = { {1,1} },
829 
830 } // namespace mitk
831 
832 #endif
mitk::eps
const MITKCORE_EXPORT ScalarType eps
MITKMULTILABEL_EXPORT
#define MITKMULTILABEL_EXPORT
Definition: MitkMultilabelExports.h:15
mitk::ConstLabelVector
std::vector< Label::ConstPointer > ConstLabelVector
Definition: mitkLabel.h:96
mitkLabel.h
mitk::TimeGeometry
Definition: mitkTimeGeometry.h:45
mitkImage.h
mitk::MultiLabelSegmentation::MergeStyle::Replace
@ Replace
mitk::SlicedData
Super class of data objects consisting of slices.
Definition: mitkSlicedData.h:37
mitk::BaseData::IsEmpty
virtual bool IsEmpty() const
Check whether object contains data (at least at one point in time), e.g., a set of points may be empt...
mitk::MultiLabelSegmentation::GroupImageDimensionVectorType
std::vector< unsigned int > GroupImageDimensionVectorType
Definition: mitkLabelSetImage.h:99
mitk::MultiLabelSegmentation::OverwriteStyle
OverwriteStyle
Definition: mitkLabelSetImage.h:74
mitk::Color
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
Definition: mitkColorProperty.h:38
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
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.
itk::SmartPointer< const Self >
mitk::LookupTable
The LookupTable class mitk wrapper for a vtkLookupTable.
Definition: mitkLookupTable.h:42
mitk::MultiLabelSegmentation::GroupIndexType
std::size_t GroupIndexType
Definition: mitkLabelSetImage.h:56
mitk::Label::PixelType
unsigned short PixelType
Definition: mitkLabel.h:34
mitk::MultiLabelSegmentation::m_ActiveLabelValue
LabelValueType m_ActiveLabelValue
Definition: mitkLabelSetImage.h:627
mitk::MultiLabelSegmentation::LabelValueType
mitk::Label::PixelType LabelValueType
Definition: mitkLabelSetImage.h:57
mitk::MultiLabelSegmentation::OverwriteStyle::RegardLocks
@ RegardLocks
mitk::TransferLabelContent
MITKMULTILABEL_EXPORT void TransferLabelContent(const MultiLabelSegmentation *sourceImage, MultiLabelSegmentation *destinationImage, LabelValueMappingVector labelMapping={ {1, 1} }, MultiLabelSegmentation::MergeStyle mergeStyle=MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle overwriteStlye=MultiLabelSegmentation::OverwriteStyle::RegardLocks)
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::MultiLabelSegmentation::LabelVectorType
LabelVector LabelVectorType
Definition: mitkLabelSetImage.h:59
mitkLookupTable.h
mitk::MultiLabelSegmentation::AfterChangeLayerEvent
Message AfterChangeLayerEvent
AfterchangeLayerEvent (e.g. used for GUI integration) As soon as active labelset was changed,...
Definition: mitkLabelSetImage.h:51
mitk::MultiLabelSegmentation::LabelValueVectorType
std::vector< LabelValueType > LabelValueVectorType
Definition: mitkLabelSetImage.h:60
mitk::Label
A data structure describing a label.
Definition: mitkLabel.h:29
mitk::MultiLabelSegmentation::UNLABELED_VALUE
const static LabelValueType UNLABELED_VALUE
Definition: mitkLabelSetImage.h:62
mitk::GetPixelType
Definition: mitkPixelTypeList.h:89
mitkMessage.h
mitk::MultiLabelSegmentation
MultiLabelSegmentation class for handling labels and layers in a segmentation session.
Definition: mitkLabelSetImage.h:43
mitk::PixelType
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
mitk::BaseData
Base of all data objects.
Definition: mitkBaseData.h:42
mitk::LabelVector
std::vector< Label::Pointer > LabelVector
Definition: mitkLabel.h:95
mitk::MultiLabelSegmentation::ConstLabelVectorType
ConstLabelVector ConstLabelVectorType
Definition: mitkLabelSetImage.h:58
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::MultiLabelSegmentation::MergeStyle
MergeStyle
Definition: mitkLabelSetImage.h:66
mitkMultiLabelEvents.h
mitk::TransferLabelContentAtTimeStep
MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const MultiLabelSegmentation *sourceImage, MultiLabelSegmentation *destinationImage, const TimeStepType timeStep, LabelValueMappingVector labelMapping={ {1, 1} }, MultiLabelSegmentation::MergeStyle mergeStyle=MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle overwriteStlye=MultiLabelSegmentation::OverwriteStyle::RegardLocks)
mitk::LabelValueMappingVector
std::vector< std::pair< Label::PixelType, Label::PixelType > > LabelValueMappingVector
Definition: mitkLabelSetImage.h:742
mitkITKEventObserverGuard.h
mitk::Message
Event/message/notification class.
Definition: mitkMessage.h:452
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20
MitkMultilabelExports.h