为此,我使用了 opencv4nodejs 和 nodejs,
我正在尝试获取图像 RGB 值并替换特定索引中的特定 RGB 值并创建一个二维数组。
const color_map = [[255,255,0], [255,0,0], [0,255,255], [0,255,0], [0,0,0]];
const input_image = cv.imread("Data/IMG/train_labels/0.png");
let index = 0
function form_2D_label(mat) {
const image = mat.cvtColor(cv.COLOR_BGR2RGB);
const imageBuffer = mat.getData();
const ui8 = new Uint8Array(imageBuffer);
const imageData = new Array((image.rows * image.cols))
for (let i = 0; i < ui8.length; i += 3) {
imageData[index] = [ui8[i], ui8[i + 1], ui8[i + 2]];
for (const [index, element] of color_map.entries()) { // enumerate color map
// console.log(index, element);
// I am trying todo if imageData[index] value = [255, 255, 0] as 0, if [255, 0, 0] as 1, if [0, 255, 255] as 2 like this..
}
console.log(imageData[index]) // [255, 255, 0] / [255, 0, 0] like this
index++;
}
return imageData;
}
const test = form_2D_label(input_image);
console.log(test);
电流输出
[
[ 0, 0, 0 ], [ 255, 0, 0 ], [ 0, 0, 0 ], [ 255, 0, 0 ], [ 0, 0, 0 ],[255, 255, 0]
]
预计一个
[
[ 4, 1, 4, 1, 4, 0 ]
]