Medical Imaging Interaction Toolkit  2023.12.99-b826bd4b
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 
212  const mitk::Label* GetLabel(LabelValueType value) const;
213  mitk::Label* GetLabel(LabelValueType value);
214 
217  const ConstLabelVectorType GetLabels() const;
218  const LabelVectorType GetLabels();
219 
222  const LabelValueVectorType GetAllLabelValues() const;
223 
230  const LabelVectorType GetLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = true);
231 
236  const ConstLabelVectorType GetConstLabelsByValue(const LabelValueVectorType& labelValues, bool ignoreMissing = false) const;
237 
239  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const ConstLabelVectorType& labels);
241  static LabelValueVectorType ExtractLabelValuesFromLabelVector(const LabelVectorType& labels);
242 
244  static ConstLabelVectorType ConvertLabelVectorConst(const LabelVectorType& labels);
245 
253  const LabelValueVectorType GetLabelValuesByGroup(GroupIndexType index) const;
254 
263  const LabelValueVectorType GetLabelValuesByName(GroupIndexType index, const std::string_view name) const;
264 
268  std::vector<std::string> GetLabelClassNames() const;
269 
274  std::vector<std::string> GetLabelClassNamesByGroup(GroupIndexType index) const;
275 
280  LabelValueType GetUnusedLabelValue() const;
281 
282  itkGetConstMacro(UnlabeledLabelLock, bool);
283  itkSetMacro(UnlabeledLabelLock, bool);
284  itkBooleanMacro(UnlabeledLabelLock);
285 
288  void SetAllLabelsVisible(bool visible);
289 
293  void SetAllLabelsVisibleByGroup(GroupIndexType group, bool visible);
294 
299  void SetAllLabelsVisibleByName(GroupIndexType group, const std::string_view name, bool visible);
300 
303  bool IsLabelLocked(LabelValueType value) const;
304 
307  void SetAllLabelsLocked(bool locked);
308 
312  void SetAllLabelsLockedByGroup(GroupIndexType group, bool locked);
313 
318  void SetAllLabelsLockedByName(GroupIndexType group, const std::string_view name, bool locked);
319 
331  void ReplaceGroupLabels(const GroupIndexType groupID, const ConstLabelVectorType& newLabels);
332 
333  void ReplaceGroupLabels(const GroupIndexType groupID, const LabelVectorType& newLabels);
334 
337  mitk::Image* GetGroupImage(GroupIndexType groupID);
338 
341  const mitk::Image* GetGroupImage(GroupIndexType groupID) const;
342 
347  const std::string& GetGroupName(GroupIndexType groupID) const;
348 
351  void SetGroupName(GroupIndexType groupID, const std::string& name);
352 
353  itkGetModifiableObjectMacro(LookupTable, mitk::LookupTable);
354  void SetLookupTable(LookupTable* lut);
358  void UpdateLookupTable(PixelType pixelValue);
359 
360  protected:
361 
362  void OnLabelModified(const Object* sender, const itk::EventObject&);
363 
365  void AddLabelToMap(LabelValueType labelValue, Label* label, GroupIndexType groupID);
366  void RemoveLabelFromMap(LabelValueType labelValue);
368  void RegisterLabel(Label* label);
370  void ReleaseLabel(Label* label);
371 
374  void ApplyToLabels(const LabelValueVectorType& values, std::function<void(Label*)>&& lambda);
378  void VisitLabels(const LabelValueVectorType& values, std::function<void(const Label*)>&& lambda) const;
379 
381 
382  private:
383  using LabelMapType = std::map<LabelValueType, Label::Pointer>;
385  LabelMapType m_LabelMap;
386 
387  using GroupNameVectorType = std::vector<std::string>;
389  GroupNameVectorType m_Groups;
390 
393  using GroupToLabelMapType = std::vector<LabelValueVectorType>;
394  /* Dictionary that maps between group id (key) and label values in the group (vector of label value).*/
395  GroupToLabelMapType m_GroupToLabelMap;
396  using LabelToGroupMapType = std::map<LabelValueType, GroupIndexType>;
397  /* Dictionary that maps between label value (key) and group id (value)*/
398  LabelToGroupMapType m_LabelToGroupMap;
399 
400  using LabelEventGuardMapType = std::map<LabelValueType, ITKEventObserverGuard>;
401  LabelEventGuardMapType m_LabelModEventGuardMap;
402 
403  LookupTable::Pointer m_LookupTable;
404 
406  bool m_UnlabeledLabelLock;
407 
409  std::shared_mutex m_LabelNGroupMapsMutex;
410 
411  public:
412 
413 
416  // END FUTURE MultiLabelSegmentation
419 
424  const mitk::Image* GetGroupImageWorkaround(GroupIndexType groupID) const;
425 
428  void UpdateCenterOfMass(PixelType pixelValue);
429 
436  void Initialize(const mitk::Image *image) override;
437 
440  void ClearBuffer();
441 
448  void MergeLabel(PixelType pixelValue, PixelType sourcePixelValue);
449 
456  void MergeLabels(PixelType pixelValue, const std::vector<PixelType>& vectorOfSourcePixelValues);
457 
463  unsigned int GetActiveLayer() const;
464 
465  Label* GetActiveLabel();
466 
467  const Label* GetActiveLabel() const;
468 
474  unsigned int GetNumberOfLabels(unsigned int layer) const;
475 
480  unsigned int GetTotalNumberOfLabels() const;
481 
488  void InitializeByLabeledImage(mitk::Image::Pointer image);
489 
490  void MaskStamp(mitk::Image *mask, bool forceOverwrite);
491 
492  void SetActiveLayer(unsigned int layer);
493  void SetActiveLabel(LabelValueType label);
494 
495  unsigned int GetNumberOfLayers() const;
496 
502  GroupIndexType AddLayer(ConstLabelVector labels = {});
503 
513  GroupIndexType AddLayer(mitk::Image* layerImage, ConstLabelVector labels = {});
514 
515  protected:
516  mitkCloneMacro(Self);
517 
518  LabelSetImage();
519  LabelSetImage(const LabelSetImage &other);
520  ~LabelSetImage() override;
521 
522  template <typename TPixel, unsigned int VImageDimension>
523  void LayerContainerToImageProcessing(itk::Image<TPixel, VImageDimension> *source, unsigned int layer);
524 
525  template <typename TPixel, unsigned int VImageDimension>
526  void ImageToLayerContainerProcessing(const itk::Image<TPixel, VImageDimension> *source, unsigned int layer) const;
527 
528  template <typename ImageType>
529  void CalculateCenterOfMassProcessing(ImageType *input, LabelValueType index);
530 
531  template <typename ImageType>
532  void EraseLabelProcessing(ImageType *input, PixelType index);
533 
534  template <typename ImageType>
535  void MergeLabelProcessing(ImageType *input, PixelType pixelValue, PixelType index);
536 
537  template <typename ImageType>
538  void MaskStampProcessing(ImageType *input, mitk::Image *mask, bool forceOverwrite);
539 
540  template <typename LabelSetImageType, typename ImageType>
541  void InitializeByLabeledImageProcessing(LabelSetImageType *input, ImageType *other);
542 
545  LabelValueVectorType GetUsedLabelValues() const;
546 
547  std::vector<Image::Pointer> m_LayerContainer;
548 
551  };
552 
570  MITKMULTILABEL_EXPORT bool Equal(const mitk::LabelSetImage &leftHandSide,
571  const mitk::LabelSetImage &rightHandSide,
572  ScalarType eps,
573  bool verbose);
574 
590  const mitk::LabelSetImage::ConstLabelVectorType& rightHandSide,
591  ScalarType eps,
592  bool verbose);
593 
594 
598  namespace MultiLabelSegmentation
599  {
600  enum class MergeStyle
601  {
602  Replace, //The old label content of a label value will be replaced by its new label content.
603  //Therefore pixels that are labeled might become unlabeled again.
604  //(This means that a lock of the value is also ignored).
605  Merge //The union of old and new label content will be generated.
606  };
607 
608  enum class OverwriteStyle
609  {
610  RegardLocks, //Locked labels in the same spatial group will not be overwritten/changed.
611  IgnoreLocks //Label locks in the same spatial group will be ignored, so these labels might be changed.
612  };
613  }
614 
615  using LabelValueMappingVector = std::vector < std::pair<Label::PixelType, Label::PixelType> >;
616 
642  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const LabelSetImage* sourceImage, LabelSetImage* destinationImage,
643  const TimeStepType timeStep, LabelValueMappingVector labelMapping = { {1,1} },
646 
651  MITKMULTILABEL_EXPORT void TransferLabelContent(const LabelSetImage* sourceImage, LabelSetImage* destinationImage, LabelValueMappingVector labelMapping = { {1,1} },
654 
655 
684  MITKMULTILABEL_EXPORT void TransferLabelContentAtTimeStep(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
685  const TimeStepType timeStep, mitk::Label::PixelType sourceBackground = LabelSetImage::UNLABELED_VALUE,
687  bool destinationBackgroundLocked = false,
688  LabelValueMappingVector labelMapping = { {1,1} },
691 
696  MITKMULTILABEL_EXPORT void TransferLabelContent(const Image* sourceImage, Image* destinationImage, const mitk::ConstLabelVector& destinationLabelVector,
699  bool destinationBackgroundLocked = false,
700  LabelValueMappingVector labelMapping = { {1,1} },
703 
704 } // namespace mitk
705 
706 #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:550
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:600
mitk::LabelSetImage::m_ActiveLayer
int m_ActiveLayer
Definition: mitkLabelSetImage.h:549
mitk::LabelSetImage::m_LayerContainer
std::vector< Image::Pointer > m_LayerContainer
Definition: mitkLabelSetImage.h:547
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:608
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:380
mitkMultiLabelEvents.h
mitk::LabelValueMappingVector
std::vector< std::pair< Label::PixelType, Label::PixelType > > LabelValueMappingVector
Definition: mitkLabelSetImage.h:615
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