ITK  6.0.0
Insight Toolkit
itkLabelToRGBFunctor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkLabelToRGBFunctor_h
19 #define itkLabelToRGBFunctor_h
20 
21 #include <vector>
22 #include "itkNumericTraits.h"
23 
24 namespace itk
25 {
26 namespace Functor
27 {
49 template <typename TLabel, typename TRGBPixel>
51 {
52 public:
54 
56  {
57  using ValueType = typename TRGBPixel::ValueType;
58 
59  // the following colors are from "R", and named:
60  // "red" "green3" "blue" "cyan"
61  //"magenta" "darkorange1" "darkgreen" "blueviolet"
62  //"brown4" "navy" "yellow4" "violetred1"
63  //"salmon4" "turquoise4" "sienna3" "darkorchid1"
64  //"springgreen4" "mediumvioletred" "orangered3" "lightseagreen"
65  //"slateblue" "deeppink1" "aquamarine4" "royalblue1"
66  //"tomato3" "mediumblue" "violetred4" "darkmagenta"
67  //"violet" "red4"
68  // They are a good selection of distinct colors for plotting and
69  // overlays.
70  constexpr size_t numColors = 30;
71  constexpr unsigned char colors[numColors][3] = {
72  { 255, 0, 0 }, { 0, 205, 0 }, { 0, 0, 255 }, { 0, 255, 255 }, { 255, 0, 255 }, { 255, 127, 0 },
73  { 0, 100, 0 }, { 138, 43, 226 }, { 139, 35, 35 }, { 0, 0, 128 }, { 139, 139, 0 }, { 255, 62, 150 },
74  { 139, 76, 57 }, { 0, 134, 139 }, { 205, 104, 57 }, { 191, 62, 255 }, { 0, 139, 69 }, { 199, 21, 133 },
75  { 205, 55, 0 }, { 32, 178, 170 }, { 106, 90, 205 }, { 255, 20, 147 }, { 69, 139, 116 }, { 72, 118, 255 },
76  { 205, 79, 57 }, { 0, 0, 205 }, { 139, 34, 82 }, { 139, 0, 139 }, { 238, 130, 238 }, { 139, 0, 0 }
77  };
78 
79  for (auto & color : colors)
80  {
81  AddColor(color[0], color[1], color[2]);
82  }
83 
84  // provide some default value for external use (outside
85  // LabelToRGBImageFilter)
86  // Inside LabelToRGBImageFilter, the values are always initialized
88  m_BackgroundColor.Fill(ValueType{});
89  m_BackgroundValue = TLabel{};
90  }
91 
92  inline TRGBPixel
93  operator()(const TLabel & p) const
94  {
95  // value is background
96  // return a gray pixel with the same intensity than the label pixel
97  if (p == m_BackgroundValue)
98  {
99  return m_BackgroundColor;
100  }
101 
102  // else, return a colored pixel from the color table
103  return m_Colors[p % m_Colors.size()];
104  }
105 
106  void
107  AddColor(unsigned char r, unsigned char g, unsigned char b)
108  {
109  TRGBPixel rgbPixel;
111 
112  using ValueType = typename TRGBPixel::ValueType;
113 
114  ValueType m = NumericTraits<ValueType>::max();
115 
116  rgbPixel[0] = static_cast<ValueType>(static_cast<double>(r) / 255 * m);
117  rgbPixel[1] = static_cast<ValueType>(static_cast<double>(g) / 255 * m);
118  rgbPixel[2] = static_cast<ValueType>(static_cast<double>(b) / 255 * m);
119  m_Colors.push_back(rgbPixel);
120  }
121 
122  // Empty the color LUT
123  void
125  {
126  m_Colors.clear();
127  }
128 
129  // Get number of colors in the LUT
130  unsigned int
132  {
133  return static_cast<unsigned int>(m_Colors.size());
134  }
135 
136  bool
137  operator==(const Self & other) const
138  {
140  m_Colors == other.m_Colors;
141  }
142 
144 
145  void
147  {
148  m_BackgroundValue = v;
149  }
150 
151  void
152  SetBackgroundColor(TRGBPixel rgb)
153  {
154  m_BackgroundColor = rgb;
155  }
156 
157  ~LabelToRGBFunctor() = default;
158 
159  std::vector<TRGBPixel> m_Colors;
160 
161  TRGBPixel m_BackgroundColor;
162 
164 };
165 } // end namespace Functor
166 } // end namespace itk
167 
168 #endif
itk::Functor::LabelToRGBFunctor::GetNumberOfColors
unsigned int GetNumberOfColors() const
Definition: itkLabelToRGBFunctor.h:131
itk::Functor::LabelToRGBFunctor::m_Colors
std::vector< TRGBPixel > m_Colors
Definition: itkLabelToRGBFunctor.h:159
itk::Functor::LabelToRGBFunctor
Functor for converting labels into RGB triplets.
Definition: itkLabelToRGBFunctor.h:50
itk::Functor::LabelToRGBFunctor::AddColor
void AddColor(unsigned char r, unsigned char g, unsigned char b)
Definition: itkLabelToRGBFunctor.h:107
itk::NumericTraits::SetLength
static void SetLength(T &m, const unsigned int s)
Definition: itkNumericTraits.h:187
itk::Functor::LabelToRGBFunctor::LabelToRGBFunctor
LabelToRGBFunctor()
Definition: itkLabelToRGBFunctor.h:55
itk::Functor::LabelToRGBFunctor::SetBackgroundColor
void SetBackgroundColor(TRGBPixel rgb)
Definition: itkLabelToRGBFunctor.h:152
itk::Functor::LabelToRGBFunctor::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
itk::Functor::LabelToRGBFunctor::operator()
TRGBPixel operator()(const TLabel &p) const
Definition: itkLabelToRGBFunctor.h:93
itk::Functor::LabelToRGBFunctor::~LabelToRGBFunctor
~LabelToRGBFunctor()=default
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:169
itk::Functor::LabelToRGBFunctor::ResetColors
void ResetColors()
Definition: itkLabelToRGBFunctor.h:124
itk::Functor::LabelToRGBFunctor::m_BackgroundColor
TRGBPixel m_BackgroundColor
Definition: itkLabelToRGBFunctor.h:161
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::Functor::LabelToRGBFunctor::SetBackgroundValue
void SetBackgroundValue(TLabel v)
Definition: itkLabelToRGBFunctor.h:146
itkNumericTraits.h
itk::Functor::LabelToRGBFunctor::m_BackgroundValue
TLabel m_BackgroundValue
Definition: itkLabelToRGBFunctor.h:163
itk::Functor::LabelToRGBFunctor::operator==
bool operator==(const Self &other) const
Definition: itkLabelToRGBFunctor.h:137
AddImageFilter
Definition: itkAddImageFilter.h:81