1 回答

TA贡献1848条经验 获得超2个赞
你的问题有几个问题。
首先color_map只有 5 个元素,但预期结果的索引从 0 到 5(6 个元素),我认为这是一个错误,你只需要真正的索引。
其次,您的代码中没有index分配的值,所以我假设它是下一个可用索引,并改用push属性。
由于您实际上不想返回多维数组,而只想返回二维索引数组,因此返回imageData.
考虑到您在评论部分解释的条件,即颜色映射值将是您唯一可以尝试做的事情:
const color_map = [[255,255,0], [255,0,0], [0,255,255], [0,255,0], [0,0,0]];
function form_2D_label(mat) {
const image = mat.cvtColor(cv.COLOR_BGR2RGB);
const imageBuffer = mat.getData();
const ui8 = new Uint8Array(imageBuffer);
const imageData = [];
for (let i = 0; i < ui8.length; i += 3) {
imageData.push([ui8[i], ui8[i + 1], ui8[i + 2]]);
console.log(imageData[imageData.length - 1])
}
return [imageData.map(el => color_map.findIndex(color => arrayEquals(color, el)))];
}
function arrayEquals(array1, array2) {
for (let i = 0, l = array2.length; i < l; i++) {
if (array2[i] !== array1[i]) return false;
}
return true;
}
添加回答
举报