Medical Imaging Interaction Toolkit  2025.12.02
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*/
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);
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 
628  unsigned int GetActiveLayer() const;
629 
632  const Label* GetActiveLabel() const;
633 
639  unsigned int GetNumberOfLabels(unsigned int group) const;
640 
645  unsigned int GetTotalNumberOfLabels() const;
646 
647  unsigned int GetNumberOfGroups() const;
648 
649  protected:
651 
655 
656  void OnLabelModified(const Object* sender, const itk::EventObject&);
657 
659  void AddLabelToMap(LabelValueType labelValue, Label* label, GroupIndexType groupID);
662  void RegisterLabel(Label* label);
664  void ReleaseLabel(Label* label);
665 
668  void ApplyToLabels(const LabelValueVectorType& values, std::function<void(Label*)>&& lambda);
672  void VisitLabels(const LabelValueVectorType& values, std::function<void(const Label*)>&& lambda) const;
673 
675 
676  template <typename ImageType>
677  void CalculateCenterOfMassProcessing(ImageType* input, LabelValueType index);
678 
679  template <typename ImageType>
680  void EraseLabelProcessing(ImageType* input, LabelValueType index);
681 
682  template <typename MultiLabelSegmentationType, typename ImageType>
683  void InitializeByLabeledImageProcessing(MultiLabelSegmentationType* input, const ImageType* other);
684 
688 
689  private:
692  Image::Pointer GenerateNewGroupImage() const;
693 
694  std::vector<Image::Pointer> m_GroupContainer;
695 
696  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
698  LabelMapType m_LabelMap;
699 
700  using GroupNameVectorType = std::vector<std::string>;
702  GroupNameVectorType m_Groups;
703 
706  using GroupToLabelMapType = std::vector<LabelValueVectorType>;
707  /* Dictionary that maps between group id (key) and label values in the group (vector of label value).*/
708  GroupToLabelMapType m_GroupToLabelMap;
709  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
710  /* Dictionary that maps between label value (key) and group id (value)*/
711  LabelToGroupMapType m_LabelToGroupMap;
712 
713  using LabelEventGuardMapType = std::map<LabelValueType, ITKEventObserverGuard>;
714  LabelEventGuardMapType m_LabelModEventGuardMap;
715 
716  LookupTable::Pointer m_LookupTable;
717 
719  bool m_UnlabeledLabelLock;
720 
722  std::shared_mutex m_LabelNGroupMapsMutex;
723 
726  GroupImageDimensionVectorType m_GroupImageDimensions;
727  };
728 
747  const mitk::MultiLabelSegmentation &rightHandSide,
748  ScalarType eps,
749  bool verbose);
750 
767  ScalarType eps,
768  bool verbose);
769 
786  bool orderIsRelevant = false);
787 
788 
789  using LabelValueMappingVector = std::vector < std::pair<Label::PixelType, Label::PixelType> >;
790 
816  const TimeStepType timeStep, LabelValueMappingVector labelMapping = { {1,1} },
819 
824  MITKMULTILABEL_EXPORT void TransferLabelContent(const MultiLabelSegmentation* sourceImage, MultiLabelSegmentation* destinationImage, LabelValueMappingVector labelMapping = { {1,1} },
827 
828 
857  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
860  bool destinationBackgroundLocked = false,
861  LabelValueMappingVector labelMapping = { {1,1} },
864 
869  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
872  bool destinationBackgroundLocked = false,
873  LabelValueMappingVector labelMapping = { {1,1} },
876 
877 } // namespace mitk
878 
879 #endif
#define MITKMULTILABEL_EXPORT
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...
Image class for storing images.
Definition: mitkImage.h:70
A data structure describing a label.
Definition: mitkLabel.h:90
unsigned short PixelType
Definition: mitkLabel.h:94
The LookupTable class mitk wrapper for a vtkLookupTable.
Event/message/notification class.
Definition: mitkMessage.h:453
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)
void UpdateGroupImage(GroupIndexType groupID, const mitk::Image *sourceImage, TimeStepType timestep, TimeStepType sourceTimestep=0)
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 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)
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)
Class for defining the data type of pixels.
Definition: mitkPixelType.h:52
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
Definition: mitkLabel.h:307
MITKCORE_EXPORT const ScalarType eps
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
Definition: mitkLabel.h:306
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
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
double ScalarType
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.