2017-10-17 170 views
1

我在Unity工作中遇到了一些問題,我試圖從Webcam紋理中獲取圖像,並將其保存在一個字節[]中。問題出現在我顯示圖像時,看不到正確的圖像,它看起來像某種類型的網格網格。我認爲問題在於我用來傳遞像素。我希望你能幫助我。 enter image description herewebcamtexture的圖像問題

void Update() 
{ 

    //texto.text = texto.text + width +"/"+ height + "/" + ini.ToString()+ " mal: "+ testo().ToString()+ " SizeIMG: "+ imgData.Length + " NUEVO: "+ nuevo +"\n"; 

    imgData = null; 
    imgData = new byte[width * height * 3]; 
    resultado = null; 
    resultado = new byte[width * height * 3]; 

    color = webcam.GetPixels32(); 
    for (int i = 0; i < color.Length; i += 3) 
    { 
     imgData[(i * 3)] = color[i].r; 
     imgData[(i * 3) + 1] = color[i].g; 
     imgData[(i * 3) + 2] = color[i].b; 

    } 
    color = null; 

    //video(imgData, resultado); 

    //ProcessFrame(imgData, resultado, 0, 0, 0,0, nuevo); 
    nuevo = false; 

    textura2 = new Texture2D(width, height, TextureFormat.RGB24, false); 
    textura2.LoadRawTextureData(imgData); 
    textura2.Apply(); 

    //Left IMAGE 
    renderer.material.mainTexture = textura2; 
    textura2 = null; 

    RightImage.GetComponent<Renderer>().material.mainTexture = webcam; 

    if (kont == 30) 
    { 
     texto.text = ""; 
     kont = 0; 
    } 
    kont++; 

    Resources.UnloadUnusedAssets(); 
} 
+0

你確定圖像是每像素3位而不是4嗎? –

+0

我認爲它是3位,RGB格式,但我不確定。 –

+0

btw,圖像尺寸和color.length的值是什麼? –

回答

1

我覺得問題在於如何將您的索引編入到imgData數組中。除了乘以3之外,你增加了3。

int bytesPerPixel = 3; 
const int indexR = 0; 
const int indexG = 1; 
const int indexB = 2; 
for(var i = 0; i < color.Length; i++) 
{ 
    imgData[(i * bytesPerPixel) + indexR] = color[i].r; 
    imgData[(i * bytesPerPixel) + indexG] = color[i].g; 
    imgData[(i * bytesPerPixel) + indexB] = color[i].b; 
} 
+0

oooooooo它的作品,現在我看到了失敗。我在3中加入3。謝謝 –

+0

@UrkoSanchezOrtiz沒問題!如果已解決問題,請隨時註冊並標記爲已接受 –