RGB颜色值为双倍 - 不准确度太大

时间:2015-09-12 00:19:38

标签: c++ colors double precision googletest

我目前正在改造一个名为RGBConverter(ValueAnimator#ofArgb(int...)original)的漂亮Arduino库,用于将RGB色彩空间中的颜色值转换为其他颜色。我还想使用 gtest 添加一些测试,因为当涉及以浮点数表示的颜色时,准确性确实很重要。这不仅适用于人眼的颜色感知,也适用于处理光线的各种传感器。

在创建基本测试用例时,我将单个RGB颜色值组件(例如红色,从0到10)循环为整数并将其转换为双重表示形式,我对此过程的不准确程度感到非常失望。

以下是我的测试用例的代码:

TEST(UnitCoversion, IntToDouble_single) {
  double x = 0.00000, x_res;
  int color = 0;
  for(; color < 10; color++) {
    printf("%d -> (expected: %.5f | actual: %.5f", color, x, x_res);
    RGBConverter::rgbIntToDouble_single(color, &x_res);
    EXPECT_NEAR(x, x_res, 0.0001);
    x+=0.004;
  }
}

这是rgbIntToDouble(...)的函数体:

void RGBConverter::rgbIntToDouble_single(int x, double *_x) {
  *_x = x/255.;
}

毋庸置疑,测试失败了:

The difference between x and x_res is 0.00015686274509803949, which exceeds 0.0001, where
x evaluates to 0.0080000000000000002,
x_res evaluates to 0.0078431372549019607, and
0.0001 evaluates to 0.0001.
3 -> (expected: 0.01200 | actual: 0.00784/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00023529411764705924, which exceeds 0.0001, where
x evaluates to 0.012,
x_res evaluates to 0.011764705882352941, and
0.0001 evaluates to 0.0001.
4 -> (expected: 0.01600 | actual: 0.01176/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00031372549019607898, which exceeds 0.0001, where
x evaluates to 0.016,
x_res evaluates to 0.015686274509803921, and
0.0001 evaluates to 0.0001.
5 -> (expected: 0.02000 | actual: 0.01569/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00039215686274509873, which exceeds 0.0001, where
x evaluates to 0.02,
x_res evaluates to 0.019607843137254902, and
0.0001 evaluates to 0.0001.
6 -> (expected: 0.02400 | actual: 0.01961/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00047058823529411847, which exceeds 0.0001, where
x evaluates to 0.024,
x_res evaluates to 0.023529411764705882, and
0.0001 evaluates to 0.0001.
7 -> (expected: 0.02800 | actual: 0.02353/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00054901960784313822, which exceeds 0.0001, where
x evaluates to 0.028000000000000001,
x_res evaluates to 0.027450980392156862, and
0.0001 evaluates to 0.0001.
8 -> (expected: 0.03200 | actual: 0.02745/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00062745098039215796, which exceeds 0.0001, where
x evaluates to 0.032000000000000001,
x_res evaluates to 0.031372549019607843, and
0.0001 evaluates to 0.0001.
9 -> (expected: 0.03600 | actual: 0.03137/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00070588235294118118, which exceeds 0.0001, where
x evaluates to 0.036000000000000004,
x_res evaluates to 0.035294117647058823, and
0.0001 evaluates to 0.0001.
[  FAILED  ] UnitCoversion.IntToDouble_single (1 ms)
[----------] 1 test from UnitCoversion (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] UnitCoversion.IntToDouble_single

 1 FAILED TEST

正如您所看到的,在浮点后的2-3位小数后,我的结果非常差。我发现这种行为太靠近浮动了。但是我正在使用双打。

我的问题是:我在这里做错了吗?我确实意识到总结浮点数会及时累积舍入误差,但是这里它从一开始就开始了!使用 0.1 0.01 作为 EXPECT_NEAR absoulte错误参数的值几乎没有任何测试......并且错误这样的幅度确实影响了颜色感知(用一堆LED测试)。如果可能,我该如何改进?如果是这样的话我也可以切换到花车。至少在记忆方面它会更有效率。

0 个答案:

没有答案