From 06ce954befd1c6eed05116b775fc434b4d388352 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Aug 2017 23:32:28 -0700 Subject: [PATCH] Image scoring, remove spike-filled API pit --- colorchecker.h | 41 ++++++++++++++++++++++++++++++++++------- piphoto.cc | 4 ++-- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/colorchecker.h b/colorchecker.h index a3a7761..133154b 100644 --- a/colorchecker.h +++ b/colorchecker.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "color.h" #include "colors.h" @@ -64,15 +65,41 @@ std::array FindClosest(const Image& image } template -void HighlightClosest(Image* image) { - auto closest = FindClosest(*image); +uint32_t ScoreImage(const Image& image) { + std::array diff; + diff.fill(UINT32_MAX); + + for (uint32_t y = 0; y < Y; ++y) { + const auto& row = image.at(y); + + for (uint32_t x = 0; x < X; ++x) { + const auto& pixel = row.at(x); + + for (uint32_t cc = 0; cc < kColorCheckerSrgb.size(); ++cc) { + auto pixel_diff = pixel.Difference(kColorCheckerSrgb.at(cc)); + if (pixel_diff < diff.at(cc)) { + diff.at(cc) = pixel_diff; + } + } + } + } + + return std::accumulate(diff.begin(), diff.end(), UINT32_C(0)); +} + +template +std::unique_ptr> HighlightClosest(const Image& image) { + auto out = std::make_unique>(image); + + auto closest = FindClosest(*out); for (uint32_t cc = 0; cc < kColorCheckerSrgb.size(); ++cc) { const auto& coord = closest.at(cc); const auto& color = kColorCheckerSrgb.at(cc); - image->DrawSquare({std::max(5U, coord.x) - 5, std::max(5U, coord.y) - 5}, kBlack, 10); - image->DrawSquare({std::max(6U, coord.x) - 6, std::max(6U, coord.y) - 6}, color, 12); - image->DrawSquare({std::max(7U, coord.x) - 7, std::max(7U, coord.y) - 7}, color, 14); - image->DrawSquare({std::max(8U, coord.x) - 8, std::max(8U, coord.y) - 8}, color, 16); - image->DrawSquare({std::max(9U, coord.x) - 9, std::max(9U, coord.y) - 9}, kWhite, 18); + out->DrawSquare({std::max(5U, coord.x) - 5, std::max(5U, coord.y) - 5}, kBlack, 10); + out->DrawSquare({std::max(6U, coord.x) - 6, std::max(6U, coord.y) - 6}, color, 12); + out->DrawSquare({std::max(7U, coord.x) - 7, std::max(7U, coord.y) - 7}, color, 14); + out->DrawSquare({std::max(8U, coord.x) - 8, std::max(8U, coord.y) - 8}, color, 16); + out->DrawSquare({std::max(9U, coord.x) - 9, std::max(9U, coord.y) - 9}, kWhite, 18); } + return out; } diff --git a/piphoto.cc b/piphoto.cc index 9e0f159..a2e2652 100644 --- a/piphoto.cc +++ b/piphoto.cc @@ -9,6 +9,6 @@ int main() { auto image = PiRaw2::FromJpeg(ReadFile("test.jpg")); auto lut = MinimalLut3d::Identity(); auto image2 = lut->MapImage(*image); - HighlightClosest(image2.get()); - WriteFile("test.png", image2->ToPng()); + std::cout << "Score: " << ScoreImage(*image2) << std::endl; + WriteFile("test.png", HighlightClosest(*image2)->ToPng()); }