CortidQCT  1.2.2.52
ColorToLabelMap.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <array>
16 #include <cassert>
17 #include <functional>
18 #include <unordered_map>
19 
20 namespace CortidQCT {
21 
23 using ColorRGB = std::array<std::uint8_t, 3>;
24 
25 } // namespace CortidQCT
26 
27 namespace std {
28 
29 template <> struct hash<CortidQCT::ColorRGB> {
30 
31  using argument_type = CortidQCT::ColorRGB;
32  using result_type = std::size_t;
33 
34  result_type operator()(argument_type const &comp) const noexcept {
35  auto const r = static_cast<std::uint32_t>(comp[0]);
36  auto const g = static_cast<std::uint32_t>(comp[1]);
37  auto const b = static_cast<std::uint32_t>(comp[2]);
38  auto const val = r << 16 | g << 8 | b;
39 
40  return hash<std::uint32_t>{}(val);
41  }
42 };
43 
44 } // namespace std
45 
46 namespace CortidQCT {
47 
58 template <class Label, class Scalar>
59 using ColorToLabelMap = std::function<Label(Scalar, Scalar, Scalar)>;
60 
62 namespace ColorToLabelMaps {
63 
80 template <class Label, class Scalar>
81 inline Label defaultMap(Scalar red, Scalar green, Scalar blue) {
82  assert(red >= Scalar{0} && red <= Scalar{1});
83  assert(green >= Scalar{0} && green <= Scalar{1});
84  assert(blue >= Scalar{0} && blue <= Scalar{1});
85 
86  auto const redScaled = static_cast<std::uint64_t>(red * Scalar{255});
87  auto const greenScaled = static_cast<std::uint64_t>(green * Scalar{255});
88  auto const blueScaled = static_cast<std::uint64_t>(blue * Scalar{255});
89 
90  auto const combined = blueScaled << 16 | greenScaled << 8 | redScaled;
91 
92  return static_cast<Label>(combined);
93 }
94 
98 struct CustomMap {
99 
100  using LabelType = std::uint64_t;
101 
102  using Table = std::unordered_map<ColorRGB, LabelType>;
103 
111  Table table = {{{{255, 0, 0}}, 0}, {{{0, 255, 0}}, 1},
112  {{{255, 255, 0}}, 2}, {{{0, 0, 255}}, 3},
113  {{{255, 0, 255}}, 4}, {{{0, 255, 255}}, 5}};
114 
117  LabelType undefinedLabel{0};
118 
120  template <class Scalar, class Label>
121  inline operator ColorToLabelMap<Label, Scalar>() const {
122  return [map = *this](Scalar r, Scalar g, Scalar b) -> Label {
123  return map.operator()<Label>(r, g, b);
124  };
125  }
126 
139  template <class Label, class Scalar>
140  inline Label operator()(Scalar red, Scalar green, Scalar blue) const {
141  assert(red >= Scalar{0} && red <= Scalar{1});
142  assert(green >= Scalar{0} && green <= Scalar{1});
143  assert(blue >= Scalar{0} && blue <= Scalar{1});
144 
145  auto const redScaled = static_cast<std::uint8_t>(red * Scalar{255});
146  auto const greenScaled = static_cast<std::uint8_t>(green * Scalar{255});
147  auto const blueScaled = static_cast<std::uint8_t>(blue * Scalar{255});
148 
149  auto const components = ColorRGB{{redScaled, greenScaled, blueScaled}};
150 
151  if (auto const labelIt = table.find(components); labelIt != table.end()) {
152  return static_cast<Label>(labelIt->second);
153  }
154 
155  return Label{static_cast<Label>(undefinedLabel)};
156  }
157 
164  CustomMap &loadFromFile(std::string const &filename);
165 
170  inline static CustomMap fromFile(std::string const &filename) {
171  return CustomMap{}.loadFromFile(filename);
172  }
173 };
174 
175 } // namespace ColorToLabelMaps
176 
177 } // namespace CortidQCT
Name namespace for CortidQCT library.
Definition: CortidQCT.h:23
static CustomMap fromFile(std::string const &filename)
Convenience initializer to load custom map from file.
Definition: ColorToLabelMap.h:170
Label operator()(Scalar red, Scalar green, Scalar blue) const
Converts a RGB color to a label using the pre-defined lookup table.
Definition: ColorToLabelMap.h:140
Definition: ColorToLabelMap.h:27
std::function< Label(Scalar, Scalar, Scalar)> ColorToLabelMap
Color to label map.
Definition: ColorToLabelMap.h:59
Label defaultMap(Scalar red, Scalar green, Scalar blue)
Default color to label map.
Definition: ColorToLabelMap.h:81
std::array< std::uint8_t, 3 > ColorRGB
24-bit RGB color type
Definition: ColorToLabelMap.h:23
CustomMap & loadFromFile(std::string const &filename)
Loads the custom map from a YAML file.
Definition: ColorToLabelMapIO.cpp:22
A customaizable color to label map.
Definition: ColorToLabelMap.h:98