18 #include <unordered_map> 32 using result_type = std::size_t;
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;
40 return hash<std::uint32_t>{}(val);
58 template <
class Label,
class Scalar>
62 namespace ColorToLabelMaps {
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});
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});
90 auto const combined = blueScaled << 16 | greenScaled << 8 | redScaled;
92 return static_cast<Label
>(combined);
100 using LabelType = std::uint64_t;
102 using Table = std::unordered_map<ColorRGB, LabelType>;
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}};
117 LabelType undefinedLabel{0};
120 template <
class Scalar,
class Label>
122 return [map = *
this](Scalar r, Scalar g, Scalar b) -> Label {
123 return map.operator()<Label>(r, g, b);
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});
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});
149 auto const components =
ColorRGB{{redScaled, greenScaled, blueScaled}};
151 if (
auto const labelIt = table.find(components); labelIt != table.end()) {
152 return static_cast<Label
>(labelIt->second);
155 return Label{
static_cast<Label
>(undefinedLabel)};
164 CustomMap &loadFromFile(std::string
const &filename);
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