Medical Imaging Interaction Toolkit  2023.12.99-63768887
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 
59 
62  // FUTURE MultiLabelSegmentation:
63  // Section that already contains declarations used in the new class.
64  // So this part of the interface will stay after refactoring towards
65  // the new MultiLabelSegmentation class (see T28524). This section was introduced
66  // because some of the planned features are already urgently needed.
70  itkNewMacro(Self);
71 
73 
74  using GroupIndexType = std::size_t;
78  using LabelValueVectorType = std::vector<LabelValueType>;
79 
81 
98  mitk::Label* AddLabel(mitk::Label* label, GroupIndexType groupID, bool addAsClone = true, bool correctLabelValue = true);
99 
107  mitk::Label* AddLabel(const std::string& name, const Color& color, GroupIndexType groupID);
108 
115  void RenameLabel(LabelValueType labelValue, const std::string& name, const Color& color);
116 
124  void RemoveLabel(LabelValueType labelValue);
125 
133  void RemoveLabels(const LabelValueVectorType& vectorOfLabelPixelValues);
134 
142  void EraseLabel(LabelValueType labelValue);
143 
150  void EraseLabels(const LabelValueVectorType& labelValues);
151 
160  void RemoveGroup(GroupIndexType group);
161 
163  bool ExistLabel(LabelValueType value) const;
164 
171  bool ExistLabel(LabelValueType value, GroupIndexType groupIndex) const;
172 
178  bool ExistGroup(GroupIndexType index) const;
179 
183  GroupIndexType GetGroupIndexOfLabel(LabelValueType value) const;
184 
190  const mitk::Label* GetLabel(LabelValueType value) const;
191  mitk::Label* GetLabel(LabelValueType value);
192 
195  const ConstLabelVectorType GetLabels() const;
196  const LabelVectorType GetLabels();
197 
200  const LabelValueVectorType GetAllLabelValues() const;
201 
208  const LabelVectorType GetLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = true);
209 
214  const ConstLabelVectorType GetConstLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = false) const;
215 
217  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const ConstLabelVectorType& labels);
219  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const LabelVectorType& labels);
220 
222  static ConstLabelVectorType ConvertLabelVectorConst(const LabelVectorType& labels);
223 
231  const LabelValueVectorType GetLabelValuesByGroup(GroupIndexType index) const;
232 
241  const LabelValueVectorType GetLabelValuesByName(GroupIndexType index, const std::string_view name) const;
242 
246  std::vector<std::string> GetLabelClassNames() const;
247 
252  std::vector<std::string> GetLabelClassNamesByGroup(GroupIndexType index) const;
253 
258  LabelValueType GetUnusedLabelValue() const;
259 
260  itkGetConstMacro(UnlabeledLabelLock, bool);
261  itkSetMacro(UnlabeledLabelLock, bool);
262  itkBooleanMacro(UnlabeledLabelLock);
263 
266  void SetAllLabelsVisible(bool visible);
267 
271  void SetAllLabelsVisibleByGroup(GroupIndexType group, bool visible);
272 
277  void SetAllLabelsVisibleByName(GroupIndexType group, const std::string_view name, bool visible);
278 
281  bool IsLabelLocked(LabelValueType value) const;
282 
285  void SetAllLabelsLocked(bool locked);
286 
290  void SetAllLabelsLockedByGroup(GroupIndexType group, bool locked);
291 
296  void SetAllLabelsLockedByName(GroupIndexType group, const std::string_view name, bool locked);
297 
309  void ReplaceGroupLabels(const GroupIndexType groupID, const ConstLabelVectorType& newLabels);
310 
311  void ReplaceGroupLabels(const GroupIndexType groupID, const LabelVectorType& newLabels);
312 
315  mitk::Image* GetGroupImage(GroupIndexType groupID);
316 
319  const mitk::Image* GetGroupImage(GroupIndexType groupID) const;
320 
321  itkGetModifiableObjectMacro(LookupTable, mitk::LookupTable);
322  void SetLookupTable(LookupTable* lut);
326  void UpdateLookupTable(PixelType pixelValue);
327 
328  protected:
329 
330  void OnLabelModified(const Object* sender, const itk::EventObject&);
331 
333  void AddLabelToMap(LabelValueType labelValue, Label* label, GroupIndexType groupID);
334  void RemoveLabelFromMap(LabelValueType labelValue);
336  void RegisterLabel(Label* label);
338  void ReleaseLabel(Label* label);
339 
342  void ApplyToLabels(const LabelValueVectorType& values, std::function<void(Label*)>&& lambda);
346  void VisitLabels(const LabelValueVectorType& values, std::function<void(const Label*)>&& lambda) const;
347 
349 
350  private:
351  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
353  LabelMapType m_LabelMap;
354 
355  using GroupNameVectorType = std::vector<std::string>;
357  GroupNameVectorType m_Groups;
358 
361  using GroupToLabelMapType = std::vector<LabelValueVectorType>;
362  /* Dictionary that maps between group id (key) and label values in the group (vector of label value).*/
363  GroupToLabelMapType m_GroupToLabelMap;
364  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
365  /* Dictionary that maps between label value (key) and group id (value)*/
366  LabelToGroupMapType m_LabelToGroupMap;
367 
368  using LabelEventGuardMapType = std::map<LabelValueType, ITKEventObserverGuard>;
369  LabelEventGuardMapType m_LabelModEventGuardMap;
370 
371  LookupTable::Pointer m_LookupTable;
372 
374  bool m_UnlabeledLabelLock;
375 
377  std::shared_mutex m_LabelNGroupMapsMutex;
378 
379  public:
380 
381 
384  // END FUTURE MultiLabelSegmentation
387 
389  void UpdateCenterOfMass(PixelType pixelValue);
390 
397  void Initialize(const mitk::Image *image) override;
398 
401  void ClearBuffer();
402 
409  void MergeLabel(PixelType pixelValue, PixelType sourcePixelValue);
410 
417  void MergeLabels(PixelType pixelValue, const std::vector<PixelType>& vectorOfSourcePixelValues);
418 
424  unsigned int GetActiveLayer() const;
425 
426  Label* GetActiveLabel();
427 
428  const Label* GetActiveLabel() const;
429 
435  unsigned int GetNumberOfLabels(unsigned int layer) const;
436 
441  unsigned int GetTotalNumberOfLabels() const;
442 
443  mitk::Image::Pointer CreateLabelMask(PixelType index);
444 
452  void InitializeByLabeledImage(mitk::Image::Pointer image);
453 
454  void MaskStamp(mitk::Image *mask, bool forceOverwrite);
455 
456  void SetActiveLayer(unsigned int layer);
457  void SetActiveLabel(LabelValueType label);
458 
459  unsigned int GetNumberOfLayers() const;
460 
466  GroupIndexType AddLayer(ConstLabelVector labels = {});
467 
474  GroupIndexType AddLayer(mitk::Image::Pointer layerImage, ConstLabelVector labels = {});
475 
476  protected:
477  mitkCloneMacro(Self);
478 
479  LabelSetImage();
480  LabelSetImage(const LabelSetImage &other);
481  ~LabelSetImage() override;
482 
483  template <typename TPixel, unsigned int VImageDimension>
484  void LayerContainerToImageProcessing(itk::Image<TPixel, VImageDimension> *source, unsigned int layer);
485 
486  template <typename TPixel, unsigned int VImageDimension>
487  void ImageToLayerContainerProcessing(itk::Image<TPixel, VImageDimension> *source, unsigned int layer) const;
488 
489  template <typename ImageType>
490  void CalculateCenterOfMassProcessing(ImageType *input, LabelValueType index);
491 
492  template <typename ImageType>
493  void EraseLabelProcessing(ImageType *input, PixelType index);
494 
495  template <typename ImageType>
496  void MergeLabelProcessing(ImageType *input, PixelType pixelValue, PixelType index);
497 
498  template <typename ImageType>
499  void MaskStampProcessing(ImageType *input, mitk::Image *mask, bool forceOverwrite);
500 
501  template <typename LabelSetImageType, typename ImageType>
502  void InitializeByLabeledImageProcessing(LabelSetImageType *input, ImageType *other);
503 
506  LabelValueVectorType GetUsedLabelValues() const;
507 
508  std::vector<Image::Pointer> m_LayerContainer;
509 
512  };
513 
531  MITKMULTILABEL_EXPORT bool Equal(const mitk::LabelSetImage &leftHandSide,
532  const mitk::LabelSetImage &rightHandSide,
533  ScalarType eps,
534  bool verbose);
535 
551  const mitk::LabelSetImage::ConstLabelVectorType& rightHandSide,
552  ScalarType eps,
553  bool verbose);
554 
555 
559  namespace MultiLabelSegmentation
560  {
561  enum class MergeStyle
562  {
563  Replace, //The old label content of a label value will be replaced by its new label content.
564  //Therefore pixels that are labeled might become unlabeled again.
565  //(This means that a lock of the value is also ignored).
566  Merge //The union of old and new label content will be generated.
567  };
568 
569  enum class OverwriteStyle
570  {
571  RegardLocks, //Locked labels in the same spatial group will not be overwritten/changed.
572  IgnoreLocks //Label locks in the same spatial group will be ignored, so these labels might be changed.
573  };
574  }
575 
576  using LabelValueMappingVector = std::vector < std::pair<Label::PixelType, Label::PixelType> >;
577 
603  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const LabelSetImage* sourceImage, LabelSetImage* destinationImage,
604  const TimeStepType timeStep, LabelValueMappingVector labelMapping = { {1,1} },
607 
612  MITKMULTILABEL_EXPORT void TransferLabelContent(const LabelSetImage* sourceImage, LabelSetImage* destinationImage, LabelValueMappingVector labelMapping = { {1,1} },
615 
616 
645  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
646  const TimeStepType timeStep, mitk::Label::PixelType sourceBackground = LabelSetImage::UNLABELED_VALUE,
648  bool destinationBackgroundLocked = false,
649  LabelValueMappingVector labelMapping = { {1,1} },
652 
657  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
660  bool destinationBackgroundLocked = false,
661  LabelValueMappingVector labelMapping = { {1,1} },
664 
665 } // namespace mitk
666 
667 #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:51
mitk::ConstLabelVector
std::vector< Label::ConstPointer > ConstLabelVector
Definition: mitkLabel.h:92
mitkLabel.h
mitkImage.h
mitk::MultiLabelSegmentation::OverwriteStyle::RegardLocks
@ RegardLocks
mitk::TransferLabelContentAtTimeStep
MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const LabelSetImage *sourceImage, LabelSetImage *destinationImage, const TimeStepType timeStep, LabelValueMappingVector labelMapping={ {1, 1} }, MultiLabelSegmentation::MergeStyle mergeStyle=MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle overwriteStlye=MultiLabelSegmentation::OverwriteStyle::RegardLocks)
mitk::LabelSetImage::LabelVectorType
LabelVector LabelVectorType
Definition: mitkLabelSetImage.h:77
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.
mitk::LabelSetImage::m_activeLayerInvalid
bool m_activeLayerInvalid
Definition: mitkLabelSetImage.h:511
itk::SmartPointer< Self >
mitk::LookupTable
The LookupTable class mitk wrapper for a vtkLookupTable.
Definition: mitkLookupTable.h:42
mitk::LabelSetImage::AfterChangeLayerEvent
Message AfterChangeLayerEvent
AfterchangeLayerEvent (e.g. used for GUI integration) As soon as active labelset was changed,...
Definition: mitkLabelSetImage.h:58
mitk::Label::PixelType
unsigned short PixelType
Definition: mitkLabel.h:34
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkLookupTable.h
mitk::Label
A data structure describing a label.
Definition: mitkLabel.h:29
mitk::MultiLabelSegmentation::MergeStyle::Replace
@ Replace
mitkMessage.h
mitk::MultiLabelSegmentation::MergeStyle
MergeStyle
Definition: mitkLabelSetImage.h:561
mitk::LabelSetImage::m_ActiveLayer
int m_ActiveLayer
Definition: mitkLabelSetImage.h:510
mitk::LabelSetImage::m_LayerContainer
std::vector< Image::Pointer > m_LayerContainer
Definition: mitkLabelSetImage.h:508
mitk::LabelSetImage::UNLABELED_VALUE
const static LabelValueType UNLABELED_VALUE
Definition: mitkLabelSetImage.h:80
mitk::BaseData
Base of all data objects.
Definition: mitkBaseData.h:42
mitk::LabelVector
std::vector< Label::Pointer > LabelVector
Definition: mitkLabel.h:91
mitk::MultiLabelSegmentation::OverwriteStyle
OverwriteStyle
Definition: mitkLabelSetImage.h:569
mitk::MultiLabelSegmentation::MergeStyle::Merge
@ Merge
mitk::TransferLabelContent
MITKMULTILABEL_EXPORT void TransferLabelContent(const LabelSetImage *sourceImage, LabelSetImage *destinationImage, LabelValueMappingVector labelMapping={ {1, 1} }, MultiLabelSegmentation::MergeStyle mergeStyle=MultiLabelSegmentation::MergeStyle::Replace, MultiLabelSegmentation::OverwriteStyle overwriteStlye=MultiLabelSegmentation::OverwriteStyle::RegardLocks)
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_ActiveLabelValue
LabelValueType m_ActiveLabelValue
Definition: mitkLabelSetImage.h:348
mitkMultiLabelEvents.h
mitk::LabelValueMappingVector
std::vector< std::pair< Label::PixelType, Label::PixelType > > LabelValueMappingVector
Definition: mitkLabelSetImage.h:576
mitk::LabelSetImage::PixelType
mitk::Label::PixelType PixelType
Definition: mitkLabelSetImage.h:70
mitkITKEventObserverGuard.h
mitk::LabelSetImage::GroupIndexType
std::size_t GroupIndexType
Definition: mitkLabelSetImage.h:74
mitk::LabelSetImage::LabelValueVectorType
std::vector< LabelValueType > LabelValueVectorType
Definition: mitkLabelSetImage.h:78
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:43
mitk::LabelSetImage::ConstLabelVectorType
ConstLabelVector ConstLabelVectorType
Definition: mitkLabelSetImage.h:76
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20
mitk::LabelSetImage::LabelValueType
mitk::Label::PixelType LabelValueType
Definition: mitkLabelSetImage.h:75
MitkMultilabelExports.h