25 static const std::vector<NamedColour> colours = {
26 {
"red", {53.2, 80.1, 67.2}}, {
"orange", {74.9, 23.9, 78.9}},
27 {
"yellow", {97.1, -21.6, 94.5}}, {
"green", {87.7, -86.2, 83.2}},
28 {
"cyan", {91.1, -48.1, -14.1}}, {
"blue", {32.3, 79.2, -107.9}},
29 {
"purple", {29.8, 58.9, -36.5}}, {
"magenta", {60.3, 98.3, -60.8}},
30 {
"pink", {83.6, 24.1, 3.3}}, {
"brown", {37.5, 26.4, 40.9}},
56double colourCost(
const cv::Vec3d & lab,
const std::string & name)
60 if (colour.name != name)
64 const double chroma = std::hypot(lab[1], lab[2]);
66 std::abs(std::atan2(lab[2], lab[1]) - std::atan2(colour.lab[2], colour.lab[1]));
67 if (difference > CV_PI)
69 difference = 2.0 * CV_PI - difference;
73 return difference * 60.0 + 0.5 * std::abs(lab[0] - colour.lab[0]) +
74 0.25 * std::abs(chroma - std::hypot(colour.lab[1], colour.lab[2]));
76 return std::numeric_limits<double>::infinity();
109 const std::vector<cv::Vec3d> & seen,
const std::vector<std::string> & names)
111 if (seen.size() < names.size() || names.empty())
115 std::vector<std::vector<double>> cost(names.size(), std::vector<double>(seen.size(), 0.0));
116 for (
size_t n = 0; n < names.size(); ++n)
118 for (
size_t i = 0; i < seen.size(); ++i)
123 std::vector<int> current(names.size(), -1);
124 std::vector<int> best;
125 std::vector<bool> used(seen.size(),
false);
126 double bestSum = std::numeric_limits<double>::max();
127 assign(cost, 0, 0.0, current, used, bestSum, best);
void assign(const std::vector< std::vector< double > > &cost, size_t name, double sum, std::vector< int > ¤t, std::vector< bool > &used, double &bestSum, std::vector< int > &best)