Medical Imaging Interaction Toolkit  2026.06.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 <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*/
65 
66  enum class MergeStyle
67  {
68  Replace,
71  Merge
72  };
73 
74  enum class OverwriteStyle
75  {
77  RegardLocks,
79  IgnoreLocks
80  };
81 
85  bool IsSliceSet(int s = 0, int t = 0, int n = 0) const override;
86 
90  bool IsVolumeSet(int t = 0, int n = 0) const override;
91 
95  bool IsChannelSet(int n = 0) const override;
96 
100  unsigned int GetDimension() const;
101  using GroupImageDimensionVectorType = std::vector<unsigned int>;
107 
124  mitk::Label* AddLabel(Label* label, GroupIndexType groupID, bool addAsClone = true, bool correctLabelValue = true);
125 
146  mitk::Label* AddLabelWithContent(Label* label, const Image* labelContent, GroupIndexType groupID, LabelValueType contentLabelValue, bool addAsClone = true, bool correctLabelValue = true);
147 
155  mitk::Label* AddLabel(const std::string& name, const Color& color, GroupIndexType groupID);
156 
163  void RenameLabel(LabelValueType labelValue, const std::string& name, const Color& color);
164 
174  void UpdateLabel(LabelValueType labelValue, const Label* templateLabel);
175 
183  void RemoveLabel(LabelValueType labelValue);
184 
192  void RemoveLabels(const LabelValueVectorType& vectorOfLabelPixelValues);
193 
201  void EraseLabel(LabelValueType labelValue);
202 
209  void EraseLabels(const LabelValueVectorType& labelValues);
210 
226  void MergeLabel(LabelValueType targetLabelValue, LabelValueType sourceLabelValue,
227  OverwriteStyle overwriteStyle = OverwriteStyle::RegardLocks);
228 
242  void MergeLabels(LabelValueType targetLabelValue, const LabelValueVectorType& sourceLabelValues,
243  OverwriteStyle overwriteStyle = OverwriteStyle::RegardLocks);
244 
252 
263 
272  void InsertGroup(GroupIndexType groupID, ConstLabelVector labels = {}, std::string name = "");
273 
287  void InsertGroup(GroupIndexType groupID, mitk::Image* layerImage, ConstLabelVector labels = {}, std::string name = "");
288 
298 
299 
301  bool ExistLabel(LabelValueType value) const;
302 
309  bool ExistLabel(LabelValueType value, GroupIndexType groupIndex) const;
310 
316  bool ExistGroup(GroupIndexType index) const;
317 
322 
333  mitk::Label::ConstPointer GetLabel(LabelValueType value) const;
334  mitk::Label::Pointer GetLabel(LabelValueType value);
335 
340 
344 
351  const LabelVectorType GetLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = true);
352 
357  const ConstLabelVectorType GetConstLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = false) const;
358 
363 
366 
375 
381  const LabelValueVectorType GetLabelValuesByName(const std::string_view name) const;
382 
391  const LabelValueVectorType GetLabelValuesByName(GroupIndexType index, const std::string_view name) const;
392 
403  std::optional<GroupIndexType> index = std::make_optional<GroupIndexType>()) const;
404 
408  std::vector<std::string> GetLabelClassNames() const;
409 
414  std::vector<std::string> GetLabelClassNamesByGroup(GroupIndexType index) const;
415 
421 
422  itkGetConstMacro(UnlabeledLabelLock, bool);
423  itkSetMacro(UnlabeledLabelLock, bool);
424  itkBooleanMacro(UnlabeledLabelLock);
425 
428  void SetAllLabelsVisible(bool visible);
429 
433  void SetAllLabelsVisibleByGroup(GroupIndexType group, bool visible);
434 
439  void SetAllLabelsVisibleByName(GroupIndexType group, const std::string_view name, bool visible);
440 
443  bool IsLabelLocked(LabelValueType value) const;
444 
447  void SetAllLabelsLocked(bool locked);
448 
452  void SetAllLabelsLockedByGroup(GroupIndexType group, bool locked);
453 
458  void SetAllLabelsLockedByName(GroupIndexType group, const std::string_view name, bool locked);
459 
471  void ReplaceGroupLabels(const GroupIndexType groupID, const ConstLabelVectorType& newLabels);
472 
473  void ReplaceGroupLabels(const GroupIndexType groupID, const LabelVectorType& newLabels);
474 
488  void ReplaceGroupLabels(std::map<MultiLabelSegmentation::GroupIndexType, MultiLabelSegmentation::ConstLabelVectorType> newGroupLabels);
489 
502  void ReplaceLabels(const ConstLabelVectorType& labelsUpdates);
503 
504  void ReplaceLabels(const LabelVectorType& newLabels);
505 
509 
512  const mitk::Image* GetGroupImage(GroupIndexType groupID) const;
513 
523  void UpdateGroupImage(GroupIndexType groupID, const mitk::Image* sourceImage, TimeStepType timestep, TimeStepType sourceTimestep = 0, int sourceAccessOptions = ImageAccessorBase::DefaultBehavior);
524 
535  void ClearGroupImage(GroupIndexType groupID, TimeStepType timestep);
536 
547 
552  const std::string& GetGroupName(GroupIndexType groupID) const;
553 
556  void SetGroupName(GroupIndexType groupID, const std::string& name);
557 
564 
567  void Initialize() override;
568 
583  void Initialize(const mitk::Image* templateImage, bool resetLabels = true, bool ensure1stGroup = true);
584 
599  void Initialize(const mitk::TimeGeometry* geometry, bool resetLabels = true, bool ensure1stGroup = true);
600 
608 
610  itk::ModifiedTimeType GetMTime() const override;
611 
615 
616  using BaseData::IsEmpty;
617 
620  bool IsEmpty(const Label* label, TimeStepType t = 0) const;
621  bool IsEmpty(LabelValueType pixelValue, TimeStepType t = 0) const;
622 
629  unsigned int GetActiveLayer() const;
630 
640  const Label* GetActiveLabel() const;
641 
647  unsigned int GetNumberOfLabels(unsigned int group) const;
648 
653  unsigned int GetTotalNumberOfLabels() const;
654 
655  unsigned int GetNumberOfGroups() const;
656 
657  protected:
659 
663 
664  void OnLabelModified(const Object* sender, const itk::EventObject&);
665 
667  void AddLabelToMap(LabelValueType labelValue, Label* label, GroupIndexType groupID);
670  void RegisterLabel(Label* label);
672  void ReleaseLabel(Label* label);
673 
676  void ApplyToLabels(const LabelValueVectorType& values, std::function<void(Label*)>&& lambda);
680  void VisitLabels(const LabelValueVectorType& values, std::function<void(const Label*)>&& lambda) const;
681 
683 
684  template <typename ImageType>
685  void CalculateCenterOfMassProcessing(ImageType* input, LabelValueType index);
686 
687  template <typename ImageType>
688  void EraseLabelProcessing(ImageType* input, LabelValueType index);
689 
690  template <typename MultiLabelSegmentationType, typename ImageType>
691  void InitializeByLabeledImageProcessing(MultiLabelSegmentationType* input, const ImageType* other);
692 
696 
697  private:
700  Image::Pointer GenerateNewGroupImage() const;
701 
702  std::vector<Image::Pointer> m_GroupContainer;
703 
704  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
706  LabelMapType m_LabelMap;
707 
708  using GroupNameVectorType = std::vector<std::string>;
710  GroupNameVectorType m_Groups;
711 
714  using GroupToLabelMapType = std::vector<LabelValueVectorType>;
715  /* Dictionary that maps between group id (key) and label values in the group (vector of label value).*/
716  GroupToLabelMapType m_GroupToLabelMap;
717  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
718  /* Dictionary that maps between label value (key) and group id (value)*/
719  LabelToGroupMapType m_LabelToGroupMap;
720 
721  using LabelEventGuardMapType = std::map<LabelValueType, ITKEventObserverGuard>;
722  LabelEventGuardMapType m_LabelModEventGuardMap;
723 
724  LookupTable::Pointer m_LookupTable;
725 
727  bool m_UnlabeledLabelLock;
728 
730  std::shared_mutex m_LabelNGroupMapsMutex;
731 
734  GroupImageDimensionVectorType m_GroupImageDimensions;
735  };
736 
755  const mitk::MultiLabelSegmentation &rightHandSide,
756  ScalarType eps,
757  bool verbose);
758 
775  ScalarType eps,
776  bool verbose);
777 
794  bool orderIsRelevant = false);
795 
796 
797  using LabelValueMappingVector = std::vector < std::pair<Label::PixelType, Label::PixelType> >;
798 
824  const TimeStepType timeStep, LabelValueMappingVector labelMapping = { {1,1} },
827 
832  MITKMULTILABEL_EXPORT void TransferLabelContent(const MultiLabelSegmentation* sourceImage, MultiLabelSegmentation* destinationImage, LabelValueMappingVector labelMapping = { {1,1} },
835 
836 
865  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
868  bool destinationBackgroundLocked = false,
869  LabelValueMappingVector labelMapping = { {1,1} },
872 
877  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
880  bool destinationBackgroundLocked = false,
881  LabelValueMappingVector labelMapping = { {1,1} },
884 
885 } // namespace mitk
886 
887 #endif
#define MITKMULTILABEL_EXPORT
virtual bool IsEmpty() const
Check whether the object contains data at any time step.
@ DefaultBehavior
Default behavior: wait for locked memory to be released.
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
A data structure describing a label in a multi-label segmentation.
Definition: mitkLabel.h:101
unsigned short PixelType
Pixel type used for label values (unsigned short).
Definition: mitkLabel.h:106
MITK wrapper for a vtkLookupTable.
Event/message/notification class.
Definition: mitkMessage.h:521
MultiLabelSegmentation class for handling labels and layers in a segmentation session.
void ClearGroupImage(GroupIndexType groupID, TimeStepType timestep)
clears all label pixel content from the indicated group.
itkGetModifiableObjectMacro(LookupTable, mitk::LookupTable)
void InsertGroup(GroupIndexType groupID, ConstLabelVector labels={}, std::string name="")
Inserts a new group to the MultiLabelSegmentation. The new group will be set as the active one,...
@ RegardLocks
Locked labels in the same spatial group will not be overwritten/changed.
void RemoveGroup(GroupIndexType group)
Removes a whole group including all its labels.
void RemoveLabels(const LabelValueVectorType &vectorOfLabelPixelValues)
Removes labels from the mitk::MultiLabelSegmentation. The label is removed from the labelset and the ...
void ClearGroupImage(GroupIndexType groupID)
clears all label pixel content from the indicated group.
void SetAllLabelsLocked(bool locked)
bool ExistGroup(GroupIndexType index) const
Returns true if the spatial group exists in the MultiLabelSegmentation instance.
void ReplaceGroupLabels(const GroupIndexType groupID, const LabelVectorType &newLabels)
bool IsEmpty(LabelValueType pixelValue, TimeStepType t=0) const
static PixelType GetPixelType()
void RenameLabel(LabelValueType labelValue, const std::string &name, const Color &color)
allows to adapt name and color of a certain label
void EraseLabelProcessing(ImageType *input, LabelValueType index)
void CalculateCenterOfMassProcessing(ImageType *input, LabelValueType index)
static const LabelValueType UNLABELED_VALUE
const std::string & GetGroupName(GroupIndexType groupID) const
const GroupImageDimensionVectorType & GetDimensions() const
Returns a vector of that contains the size (in pixel) for all dimensions of the MultiLabelSegmentatio...
void RemoveLabel(LabelValueType labelValue)
Removes the label with the given value. The label is removed from the labelset and the pixel with the...
ConstLabelVector ConstLabelVectorType
static LabelValueVectorType ExtractLabelValuesFromLabelVector(const LabelVectorType &labels)
void ReplaceGroupLabels(const GroupIndexType groupID, const ConstLabelVectorType &newLabels)
Replaces the labels of a group with a given vector of labels.
const LabelValueVectorType GetLabelValuesByGroup(GroupIndexType index) const
Returns a vector of all label values located on the specified group.
void RemoveLabelFromMap(LabelValueType labelValue)
LabelValueType GetUnusedLabelValue() const
std::vector< LabelValueType > LabelValueVectorType
LabelValueVectorType GetUsedLabelValues() const
mitk::Label * AddLabel(const std::string &name, const Color &color, GroupIndexType groupID)
Adds a new label to a group of the image by providing name and color.
mitk::Label::PixelType LabelValueType
bool IsVolumeSet(int t=0, int n=0) const override
Check whether volume at time t in channel n is set.
GroupIndexType AddGroup(ConstLabelVector labels={})
Adds a new group to the MultiLabelSegmentation. The new group will be set as the active one,...
void SetAllLabelsVisibleByName(GroupIndexType group, const std::string_view name, bool visible)
static LabelValueVectorType ExtractLabelValuesFromLabelVector(const ConstLabelVectorType &labels)
GroupIndexType GetGroupIndexOfLabel(LabelValueType value) const
mitk::Label::Pointer GetLabel(LabelValueType value)
Message AfterChangeLayerEvent
AfterchangeLayerEvent (e.g. used for GUI integration) As soon as active labelset was changed,...
const LabelValueVectorType GetAllLabelValues() const
void Initialize(const mitk::Image *templateImage, bool resetLabels=true, bool ensure1stGroup=true)
Initialize new (or re-initialize) the segmentation based on the properties and geometric information ...
void VisitLabels(const LabelValueVectorType &values, std::function< void(const Label *)> &&lambda) const
const LabelVectorType GetLabels()
void InitializeByLabeledImageProcessing(MultiLabelSegmentationType *input, const ImageType *other)
std::vector< unsigned int > GroupImageDimensionVectorType
void Initialize(const mitk::TimeGeometry *geometry, bool resetLabels=true, bool ensure1stGroup=true)
Initialize new (or re-initialize) the segmentation based on the passed time geometry information....
void InitializeByLabeledImage(const mitk::Image *image)
Initialize a new mitk::MultiLabelSegmentation by a given image. For all distinct pixel values of the ...
bool ExistLabel(LabelValueType value) const
Returns true if the value exists in the MultiLabelSegmentation instance.
static ConstLabelVectorType ConvertLabelVectorConst(const LabelVectorType &labels)
unsigned int GetDimension() const
Get dimension of the MultiLabelSegmentation instance.
unsigned int GetActiveLayer() const
Gets the ID of the currently active group.
const ConstLabelVectorType GetLabels() const
const mitk::Image * GetGroupImage(GroupIndexType groupID) const
void UpdateLookupTable(LabelValueType pixelValue)
void ReplaceLabels(const ConstLabelVectorType &labelsUpdates)
Replaces the labels in the segmentation by their passed counterparts.
void UpdateGroupImage(GroupIndexType groupID, const mitk::Image *sourceImage, TimeStepType timestep, TimeStepType sourceTimestep=0, int sourceAccessOptions=ImageAccessorBase::DefaultBehavior)
void UpdateCenterOfMass(LabelValueType pixelValue)
void MergeLabels(LabelValueType targetLabelValue, const LabelValueVectorType &sourceLabelValues, OverwriteStyle overwriteStyle=OverwriteStyle::RegardLocks)
Merges the content of a source labels into the target label.
unsigned int GetTotalNumberOfLabels() const
Returns the number of all labels summed up across all layers.
void EraseLabel(LabelValueType labelValue)
Erases the label with the given value from the labelset image. The label itself will not be erased fr...
MultiLabelSegmentation(const MultiLabelSegmentation &other)
void SetAllLabelsLockedByName(GroupIndexType group, const std::string_view name, bool locked)
void MergeLabel(LabelValueType targetLabelValue, LabelValueType sourceLabelValue, OverwriteStyle overwriteStyle=OverwriteStyle::RegardLocks)
Merges the content of a source label into the target label.
void ClearGroupImages(TimeStepType timestep)
clears all label pixel content of all groups in one time step.
mitk::Label::ConstPointer GetLabel(LabelValueType value) const
Returns the mitk::Label with the given value.
const LabelValueVectorType GetLabelValuesByCoordinates(const Point3D &coordinates, TimeStepType timeStep=0, std::optional< GroupIndexType > index=std::make_optional< GroupIndexType >()) const
Returns a vector of all label values that are covering passed position in world coordinates.
void AddLabelToMap(LabelValueType labelValue, Label *label, GroupIndexType groupID)
void InsertGroup(GroupIndexType groupID, mitk::Image *layerImage, ConstLabelVector labels={}, std::string name="")
Inserts a new group to the MultiLabelSegmentation. The new group will be set as the active one,...
void ApplyToLabels(const LabelValueVectorType &values, std::function< void(Label *)> &&lambda)
std::vector< std::string > GetLabelClassNamesByGroup(GroupIndexType index) const
bool IsEmpty(const Label *label, TimeStepType t=0) const
Checks if a label is empty at a given time step (does not contain any pixels).
bool IsChannelSet(int n=0) const override
Check whether the channel n is set.
mitkClassMacro(MultiLabelSegmentation, SlicedData)
bool ExistLabel(LabelValueType value, GroupIndexType groupIndex) const
Checks if a label belongs in a certain spatial group.
void OnLabelModified(const Object *sender, const itk::EventObject &)
void EraseLabels(const LabelValueVectorType &labelValues)
Erases a list of labels with the given values from the labelset image.
unsigned int GetNumberOfLabels(unsigned int group) const
Get the number of all existing mitk::Labels for a given group.
std::vector< std::string > GetLabelClassNames() const
void ReplaceGroupLabels(std::map< MultiLabelSegmentation::GroupIndexType, MultiLabelSegmentation::ConstLabelVectorType > newGroupLabels)
Replaces the labels of all passed group IDs (map key) with a given vector of labels (map value).
void SetActiveLabel(LabelValueType label)
Sets the active label by its value.
void ClearGroupImages()
clears all label pixel content of all groups.
const Label * GetActiveLabel() const
void SetAllLabelsVisibleByGroup(GroupIndexType group, bool visible)
void UpdateLabel(LabelValueType labelValue, const Label *templateLabel)
allows to update the properties of a certain label If a property exists in the destination label only...
itk::ModifiedTimeType GetMTime() const override
const LabelValueVectorType GetLabelValuesByName(const std::string_view name) const
Returns a vector of all label values of the segmentation having a certain name.
void ReplaceLabels(const LabelVectorType &newLabels)
const ConstLabelVectorType GetConstLabelsByValue(const LabelValueVectorType &labelValues, bool ignoreMissing=false) const
Returns a vector with const pointers to all labels in the MultiLabelSegmentation indicated by the pas...
mitk::Image * GetGroupImage(GroupIndexType groupID)
void ReleaseLabel(Label *label)
bool IsLabelLocked(LabelValueType value) const
bool IsSliceSet(int s=0, int t=0, int n=0) const override
Check whether slice s at time t in channel n is set.
unsigned int GetNumberOfGroups() const
GroupIndexType AddGroup(mitk::Image *layerImage, ConstLabelVector labels={})
Adds a group based on a provided mitk::Image.
void RegisterLabel(Label *label)
const LabelValueVectorType GetLabelValuesByName(GroupIndexType index, const std::string_view name) const
Returns a vector of all label values located on the specified group having a certain name.
mitk::Label * AddLabelWithContent(Label *label, const Image *labelContent, GroupIndexType groupID, LabelValueType contentLabelValue, bool addAsClone=true, bool correctLabelValue=true)
Adds a label instance to a group of the multi label image including its pixel content.
mitk::Label * AddLabel(Label *label, GroupIndexType groupID, bool addAsClone=true, bool correctLabelValue=true)
Adds a label instance to a group of the multi label image.
void SetLookupTable(LookupTable *lut)
const LabelVectorType GetLabelsByValue(const LabelValueVectorType &labelValues, bool ignoreMissing=true)
Returns a vector with pointers to all labels in the MultiLabelSegmentation indicated by the passed la...
void SetAllLabelsVisible(bool visible)
void SetGroupName(GroupIndexType groupID, const std::string &name)
void SetAllLabelsLockedByGroup(GroupIndexType group, bool locked)
Describes the data type of image pixels.
Definition: mitkPixelType.h:75
Super class of data objects consisting of slices.
Find image slices visible on a given plane.
std::vector< std::pair< Label::PixelType, Label::PixelType > > LabelValueMappingVector
std::vector< Label::ConstPointer > ConstLabelVector
Type alias for a vector of const Label smart pointers.
Definition: mitkLabel.h:552
MITKCORE_EXPORT const ScalarType eps
Epsilon value for floating point comparison (vnl_math::eps * 100).
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)
std::vector< Label::Pointer > LabelVector
Type alias for a vector of mutable Label smart pointers.
Definition: mitkLabel.h:549
itk::RGBPixel< float > Color
Standard RGB color typedef using float components.
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)
std::size_t TimeStepType
Type for time step indices (non-negative integers starting from 0).
double ScalarType
Scalar type used throughout MITK for geometric computations.
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.