Unity3D中的EmguCV Capture Mat错误

时间:2015-08-04 19:45:51

标签: c# unity3d emgucv

我正在尝试使用Unity3d和EmguCV 3.0捕获网络摄像头,但我遇到了一些奇怪的问题。首先,我试图通过以下方式进行简单的捕获:

Capture cap = new Capture(0);
Mat currentFrame = cap.QueryFrame();

但不幸的是,这会引发错误:

error CS0029: Cannot implicitly convert type `Emgu.CV.Mat' to `Emgu.CV.Mat'

这对我来说没有任何意义,我试图施展它,但这也不起作用。文档显示QueryFrame返回Mathttp://www.emgu.com/wiki/files/3.0.0/document/html/18b6eba7-f18b-fa87-8bf2-2acff68988cb.htm

3 个答案:

答案 0 :(得分:0)

您是否考虑过使用“GetPixels32”从webCamTexture获取Color32数组?然后可以将该数组转换为Mat类型(需要查看Mat构造函数)。我和你在同一条船上 - 一直试图让Unity中的EMGU正确导出到iOS应用程序的xcode。

看起来有一个'Mat.SetTo(data)',它接受一个数组并将数据应用到mat实例。

答案 1 :(得分:0)

不是隐式转换错误的答案,但您可以使用下面的代码,这应该添加到原始图像UI对象:

private WebCamTexture webcamTexture;
private Color32[] colors;

private int width = 640;
private int height = 480;

private Texture2D tex;
private byte[] bytes;

void Start ()
{
WebCamDevice[] devices = WebCamTexture.devices;
int cameraCount = devices.Length;

if (cameraCount > 0)
{
    webcamTexture = new WebCamTexture(devices[0].name, width, height);
    webcamTexture.Play();

    colors = new Color32[webcamTexture.width * webcamTexture.height];
    bytes = new byte[colors.Length*3];

    tex = new Texture2D (webcamTexture.width, webcamTexture.height, TextureFormat.RGB24, false);

    gameObject.GetComponent<RawImage> ().texture = tex;

    CvInvoke.CheckLibraryLoaded();
}
else {
    Debug.LogError("No Camera found!");
}
}

void Update ()
{
if (webcamTexture.didUpdateThisFrame) {

    webcamTexture.GetPixels32(colors);

    GCHandle imageHandle = GCHandle.Alloc(colors, GCHandleType.Pinned);
    GCHandle matHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    using(Image<Bgra, byte> image = new Image<Bgra, byte>(webcamTexture.width, webcamTexture.height, webcamTexture.width * 4, imageHandle.AddrOfPinnedObject())){
        using(Mat mat = new Mat(webcamTexture.height, webcamTexture.width, DepthType.Cv8U, 3, matHandle.AddrOfPinnedObject(), webcamTexture.width*3)){
            CvInvoke.CvtColor(image, mat, ColorConversion.Bgra2Bgr);
        }
    }
    imageHandle.Free();
    matHandle.Free();

    tex.LoadRawTextureData(bytes);
    tex.Apply();
}

接下来的挑战是将Bgra图像转换为灰色图像,通过更改我可以获得图像的转换方法和深度类型,但它是宽度的1/3 ......

答案 2 :(得分:0)

这是解决您问题的方法。

Capture capWebcam = new Capture();
Image<Bgr, byte> imgSceneColor = capWebcam.QueryFrame().ToImage<Bgr, byte>();

当您需要来自imgSceneColor的Mat()时,您可以像这样使用.Mat属性。

Mat imgMat = imgSceneColor.Mat;