EmguCV + Unity Open WebCam是错误;

时间:2014-07-17 03:03:05

标签: unity3d webcam emgucv

我统一使用EmguCV打开网络摄像头。 但它的fps很低。

这是我的代码↓

private Texture2D texture;
private Capture capture;
private Color32[] color = new Color32[640*480];
// Use this for initialization
void Start () {
    texture = new Texture2D (640, 480);
    capture = new Capture ();
}

// Update is called once per frame
void Update () {

    Image<Bgr, Byte> currentFrame = capture.QueryFrame();
    Bitmap bitmapCurrentFrame = currentFrame.ToBitmap();
    Image<Bgra, Byte> img = new Image<Bgra, Byte> (bitmapCurrentFrame);


    for(int y=0; y<480; y++){
        for(int x=0; x<640; x++){

            int index = y+x*480;
            print(index+";"+x+";"+y);
            //byte b  = img.Data[x,y,0];
            color[index].r = img.Data[x,y,2];
            color[index].g = img.Data[x,y,1];
            color[index].b = img.Data[x,y,0];
            color[index].a = 0xff;
        }
    }
    texture.SetPixels32 (color);
    texture.Apply (false);
    renderer.material.mainTexture = texture;
}

我不知道为什么fps这么低......

为什么我的老板喜欢使用Unity的EmguCV,为什么他不使用Unity-WebCamTexture ......

好的,我非常感谢你的阅读。

希望,我能得到一些答案。

1 个答案:

答案 0 :(得分:0)

看看Texture2D.LoadRawTextureData。它没有适当的文档,所以这是一个片段:

Texture2D tex = new Texture2D(width, height, format, false, true);
tex.LoadRawTextureData(buffer);
tex.Apply(false, true);

Buffer必须采用正确的硬件格式。对于format变量,请查看the list of formats that unity accepts