Medical Imaging Interaction Toolkit  2024.06.99-60d9b802
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(Label* label, GroupIndexType groupID, bool addAsClone = true, bool correctLabelValue = true);
99 
120  mitk::Label* AddLabelWithContent(Label* label, const Image* labelContent, GroupIndexType groupID, LabelValueType contentLabelValue, bool addAsClone = true, bool correctLabelValue = true);
121 
129  mitk::Label* AddLabel(const std::string& name, const Color& color, GroupIndexType groupID);
130 
137  void RenameLabel(LabelValueType labelValue, const std::string& name, const Color& color);
138 
146  void RemoveLabel(LabelValueType labelValue);
147 
155  void RemoveLabels(const LabelValueVectorType& vectorOfLabelPixelValues);
156 
164  void EraseLabel(LabelValueType labelValue);
165 
172  void EraseLabels(const LabelValueVectorType& labelValues);
173 
182  void RemoveGroup(GroupIndexType group);
183 
185  bool ExistLabel(LabelValueType value) const;
186 
193  bool ExistLabel(LabelValueType value, GroupIndexType groupIndex) const;
194 
200  bool ExistGroup(GroupIndexType index) const;
201 
205  GroupIndexType GetGroupIndexOfLabel(LabelValueType value) const;
206 
217  mitk::Label::ConstPointer GetLabel(LabelValueType value) const;
218  mitk::Label::Pointer GetLabel(LabelValueType value);
219 
222  const ConstLabelVectorType GetLabels() const;
223  const LabelVectorType GetLabels();
224 
227  const LabelValueVectorType GetAllLabelValues() const;
228 
235  const LabelVectorType GetLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = true);
236 
241  const ConstLabelVectorType GetConstLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = false) const;
242 
244  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const ConstLabelVectorType& labels);
246  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const LabelVectorType& labels);
247 
249  static ConstLabelVectorType ConvertLabelVectorConst(const LabelVectorType& labels);
250 
258  const LabelValueVectorType GetLabelValuesByGroup(GroupIndexType index) const;
259 
268  const LabelValueVectorType GetLabelValuesByName(GroupIndexType index, const std::string_view name) const;
269 
273  std::vector<std::string> GetLabelClassNames() const;
274 
279  std::vector<std::string> GetLabelClassNamesByGroup(GroupIndexType index) const;
280 
285  LabelValueType GetUnusedLabelValue() const;
286 
287  itkGetConstMacro(UnlabeledLabelLock, bool);
288  itkSetMacro(UnlabeledLabelLock, bool);
289  itkBooleanMacro(UnlabeledLabelLock);
290 
293  void SetAllLabelsVisible(bool visible);
294 
298  void SetAllLabelsVisibleByGroup(GroupIndexType group, bool visible);
299 
304  void SetAllLabelsVisibleByName(GroupIndexType group, const std::string_view name, bool visible);
305 
308  bool IsLabelLocked(LabelValueType value) const;
309 
312  void SetAllLabelsLocked(bool locked);
313 
317  void SetAllLabelsLockedByGroup(GroupIndexType group, bool locked);
318 
323  void SetAllLabelsLockedByName(GroupIndexType group, const std::string_view name, bool locked);
324 
336  void ReplaceGroupLabels(const GroupIndexType groupID, const ConstLabelVectorType& newLabels);
337 
338  void ReplaceGroupLabels(const GroupIndexType groupID, const LabelVectorType& newLabels);
339 
342  mitk::Image* GetGroupImage(GroupIndexType groupID);
343 
346  const mitk::Image* GetGroupImage(GroupIndexType groupID) const;
347 
352  const std::string& GetGroupName(GroupIndexType groupID) const;
353 
356  void SetGroupName(GroupIndexType groupID, const std::string& name);
357 
358  itkGetModifiableObjectMacro(LookupTable, mitk::LookupTable);
359  void SetLookupTable(LookupTable* lut);
363  void UpdateLookupTable(PixelType pixelValue);
364 
365  protected:
366 
367  void OnLabelModified(const Object* sender, const itk::EventObject&);
368 
370  void AddLabelToMap(LabelValueType labelValue, Label* label, GroupIndexType groupID);
371  void RemoveLabelFromMap(LabelValueType labelValue);
373  void RegisterLabel(Label* label);
375  void ReleaseLabel(Label* label);
376 
379  void ApplyToLabels(const LabelValueVectorType& values, std::function<void(Label*)>&& lambda);
383  void VisitLabels(const LabelValueVectorType& values, std::function<void(const Label*)>&& lambda) const;
384 
386 
387  private:
388  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
390  LabelMapType m_LabelMap;
391 
392  using GroupNameVectorType = std::vector<std::string>;
394  GroupNameVectorType m_Groups;
395 
398  using GroupToLabelMapType = std::vector<LabelValueVectorType>;
399  /* Dictionary that maps between group id (key) and label values in the group (vector of label value).*/
400  GroupToLabelMapType m_GroupToLabelMap;
401  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
402  /* Dictionary that maps between label value (key) and group id (value)*/
403  LabelToGroupMapType m_LabelToGroupMap;
404 
405  using LabelEventGuardMapType = std::map<LabelValueType, ITKEventObserverGuard>;
406  LabelEventGuardMapType m_LabelModEventGuardMap;
407 
408  LookupTable::Pointer m_LookupTable;
409 
411  bool m_UnlabeledLabelLock;
412 
414  std::shared_mutex m_LabelNGroupMapsMutex;
415 
416  public:
417 
418 
421  // END FUTURE MultiLabelSegmentation
424 
429  const mitk::Image* GetGroupImageWorkaround(GroupIndexType groupID) const;
430 
433  void UpdateCenterOfMass(PixelType pixelValue);
434 
441  void Initialize(const mitk::Image *image) override;
442 
445  void ClearBuffer();
446 
453  void MergeLabel(PixelType pixelValue, PixelType sourcePixelValue);
454 
461  void MergeLabels(PixelType pixelValue, const std::vector<PixelType>& vectorOfSourcePixelValues);
462 
468  unsigned int GetActiveLayer() const;
469 
470  Label* GetActiveLabel();
471 
472  const Label* GetActiveLabel() const;
473 
479  unsigned int GetNumberOfLabels(unsigned int layer) const;
480 
485  unsigned int GetTotalNumberOfLabels() const;
486 
493  void InitializeByLabeledImage(mitk::Image::Pointer image);
494 
495  void MaskStamp(mitk::Image *mask, bool forceOverwrite);
496 
497  void SetActiveLayer(unsigned int layer);
498  void SetActiveLabel(LabelValueType label);
499 
500  unsigned int GetNumberOfLayers() const;
501 
507  GroupIndexType AddLayer(ConstLabelVector labels = {});
508 
518  GroupIndexType AddLayer(mitk::Image* layerImage, ConstLabelVector labels = {});
519 
520  protected:
521  mitkCloneMacro(Self);
522 
523  LabelSetImage();
524  LabelSetImage(const LabelSetImage &other);
525  ~LabelSetImage() override;
526 
527  template <typename TPixel, unsigned int VImageDimension>
528  void LayerContainerToImageProcessing(itk::Image<TPixel, VImageDimension> *source, unsigned int layer);
529 
530  template <typename TPixel, unsigned int VImageDimension>
531  void ImageToLayerContainerProcessing(const itk::Image<TPixel, VImageDimension> *source, unsigned int layer) const;
532 
533  template <typename ImageType>
534  void CalculateCenterOfMassProcessing(ImageType *input, LabelValueType index);
535 
536  template <typename ImageType>
537  void EraseLabelProcessing(ImageType *input, PixelType index);
538 
539  template <typename ImageType>
540  void MergeLabelProcessing(ImageType *input, PixelType pixelValue, PixelType index);
541 
542  template <typename ImageType>
543  void MaskStampProcessing(ImageType *input, mitk::Image *mask, bool forceOverwrite);
544 
545  template <typename LabelSetImageType, typename ImageType>
546  void InitializeByLabeledImageProcessing(LabelSetImageType *input, ImageType *other);
547 
550  LabelValueVectorType GetUsedLabelValues() const;
551 
552  std::vector<Image::Pointer> m_LayerContainer;
553 
556  };
557 
575  MITKMULTILABEL_EXPORT bool Equal(const mitk::LabelSetImage &leftHandSide,
576  const mitk::LabelSetImage &rightHandSide,
577  ScalarType eps,
578  bool verbose);
579 
595  const mitk::LabelSetImage::ConstLabelVectorType& rightHandSide,
596  ScalarType eps,
597  bool verbose);
598 
599 
603  namespace MultiLabelSegmentation
604  {
605  enum class MergeStyle
606  {
607  Replace, //The old label content of a label value will be replaced by its new label content.
608  //Therefore pixels that are labeled might become unlabeled again.
609  //(This means that a lock of the value is also ignored).
610  Merge //The union of old and new label content will be generated.
611  };
612 
613  enum class OverwriteStyle
614  {
615  RegardLocks, //Locked labels in the same spatial group will not be overwritten/changed.
616  IgnoreLocks //Label locks in the same spatial group will be ignored, so these labels might be changed.
617  };
618  }
619 
620  using LabelValueMappingVector = std::vector < std::pair<Label::PixelType, Label::PixelType> >;
621 
647  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const LabelSetImage* sourceImage, LabelSetImage* destinationImage,
648  const TimeStepType timeStep, LabelValueMappingVector labelMapping = { {1,1} },
651 
656  MITKMULTILABEL_EXPORT void TransferLabelContent(const LabelSetImage* sourceImage, LabelSetImage* destinationImage, LabelValueMappingVector labelMapping = { {1,1} },
659 
660 
689  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
690  const TimeStepType timeStep, mitk::Label::PixelType sourceBackground = LabelSetImage::UNLABELED_VALUE,
692  bool destinationBackgroundLocked = false,
693  LabelValueMappingVector labelMapping = { {1,1} },
696 
701  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
704  bool destinationBackgroundLocked = false,
705  LabelValueMappingVector labelMapping = { {1,1} },
708 
709 } // namespace mitk
710 
711 #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:555
itk::SmartPointer< const 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:605
mitk::LabelSetImage::m_ActiveLayer
int m_ActiveLayer
Definition: mitkLabelSetImage.h:554
mitk::LabelSetImage::m_LayerContainer
std::vector< Image::Pointer > m_LayerContainer
Definition: mitkLabelSetImage.h:552
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:613
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:385
mitkMultiLabelEvents.h
mitk::LabelValueMappingVector
std::vector< std::pair< Label::PixelType, Label::PixelType > > LabelValueMappingVector
Definition: mitkLabelSetImage.h:620
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