通过颜色传感器,我使用Euclidean distance(最近距离)方法将塑料色板匹配到阵列中的预定义调色板。当识别出颜色时,线性致动器移动。即使对于相当相似的柔和色彩,此效果也很好。
但是,对于以下情况,我该如何编码:1.传感器前面没有色标或2.阵列中没有颜色?我需要生成“没有样本”(1.)或“找不到匹配项”(2.)的消息,并且两种情况下执行器均不移动。
现在,当没有色板在传感器上方时,代码将查找与环境光最接近的等效项,并且执行器移动(1.),当不匹配的色板在传感器上方时,代码将找到一个最接近的等效值,执行器移动(2.)。在这两种情况下,除了输出上述消息外,什么都不会发生。
感谢一些提示!
const int SAMPLES[12][5] = { // Values from colour "training" (averaged raw r, g and b; averaged raw c; actuator movement)
{8771, 6557, 3427, 19408, 10},
{7013, 2766, 1563, 11552, 20},
{4092, 1118, 1142, 6213, 30},
{4488, 1302, 1657, 7357, 40},
{3009, 1846, 2235, 7099, 50},
{2650, 3139, 4116, 10078, 60},
{ 857, 965, 1113, 2974, 70},
{ 964, 2014, 2418, 5476, 80},
{1260, 2200, 1459, 5043, 90},
{4784, 5898, 3138, 14301, 100},
{5505, 5242, 2409, 13642, 110},
{5406, 3893, 1912, 11457, 120}, // When adding more samples no particular order is required
};
byte findColour(int r, int g, int b) {
int distance = 10000; // Raw distance from white to black (change depending on selected integration time and gain)
byte foundColour;
for (byte i = 0; i < samplesCount; i++) {
int temp = sqrt(pow(r - SAMPLES[i][0], 2) + pow(g - SAMPLES[i][1], 2) + pow(b - SAMPLES[i][2], 2)); // Calculate Euclidean distance
if (temp < distance) {
distance = temp;
foundColour = i + 1;
}
}
return foundColour;
}
答案 0 :(得分:2)
当表中是否存在颜色时,可以由最佳匹配距离决定。当它的距离大于某个阈值时,则返回一些表示“未找到”的值,例如-1或255。
还存储传感器在没有样品存在的情况下(校准过程中)所感测到的所有信息,当这是最佳匹配时,然后返回一些表示“无样品”的值,例如0。