标记未定义的颜色,并且颜色匹配系统中不存在样品



通过颜色传感器,我使用欧几里得距离(最近距离(方法将塑料色板与数组中预定义的颜色调色板进行匹配。当识别颜色时,线性致动器移动。即使对于相当相似的柔和颜色,这也很好用。

但是,如何针对以下情况进行编码: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;
}

表中何时存在颜色可以通过最佳匹配的距离来决定。当它的距离大于某个阈值时,则返回一些指示"未找到"的值,例如 -1 或 255。

还要存储传感器在没有样品的情况下检测到的任何内容(在校准期间(,当这是最佳匹配时,返回一些指示"无样品"的值,例如 0。

最新更新