13#include <MachineLearning/FgColor.h>
14#include <opencv2/core/core.hpp>
19#define EXPECT_EQ(a__, b__) \
21 if ((a__) != (b__)) { \
22 std::cerr << "Incorrect " << #a__ << " (" << (a__) << "), should equal " \
23 << (b__) << std::endl; \
30 namespace MachineLearning
32 static const int paleness = 32;
34 static uint8_t channel2pale(uint8_t c) {
35 return c * (255 - 2 * paleness) / 255 + paleness;
38 static cv::Scalar color2scalar(
const Color& color) {
39 return cv::Scalar(channel2pale(color.b), channel2pale(color.g),
40 channel2pale(color.r));
43 static float value2snap(
float value) {
44 return std::max({ pow(10, floor(log10(value))),
45 pow(10, floor(log10(value / 2))) * 2,
46 pow(10, floor(log10(value / 5))) * 5 });
51 Trans(
void* buffer) :
Trans(*(cv::Mat*)buffer) {}
53 Trans(cv::Mat& buffer) : original_(buffer), alpha_(0), interim_(NULL) {}
55 Trans(cv::Mat& buffer,
int alpha) :
Trans(buffer) { setup(alpha); }
59 cv::Mat& get()
const {
return (interim_ != NULL ? *interim_ : original_); }
61 void setup(
int alpha) {
62 bool transparent = (alpha != 255);
64 interim_ =
new cv::Mat();
65 original_.copyTo(*interim_);
73 auto weight = alpha_ / 255.f;
74 cv::addWeighted(*interim_, weight, original_, 1 - weight, 0, original_);
80 cv::Mat& with(
int alpha) {
81 if (alpha != alpha_) {
88 cv::Mat& with(
const Color& color) {
return with(color.a); }