亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用opencv4nodejs中的另一個值替換某個RGB值的所有像素

如何用opencv4nodejs中的另一個值替換某個RGB值的所有像素

梵蒂岡之花 2023-01-06 09:46:54
我為此使用了 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 = 0function 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 ]]
查看完整描述

1 回答

?
慕尼黑5688855

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;

}


查看完整回答
反對 回復 2023-01-06
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號