将WebCamTexture转换为OpenCV的IplImage会给我一个灰色图像

时间:2015-02-07 16:19:40

标签: c# unity3d emgucv

正如标题所示,我使用Emgucv和Unity进行人脸检测。 在从网站下载的包中,有一个例子,给定一个Texture2D,返回一个图像,在所代表的主题的表面上有一个矩形。 一切看起来都很简单但是当我试图实时检测到一张脸时出现问题。将WebCamTexture转换为Texture2D似乎一切正常但是当我尝试使用TextureConverter(从Emgucv给出)将Texture2D转换为Image(包装OpenCV的IplImage的类)时,它给了我一个灰色图像。 这是我使用的简单代码:

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

    if (cameraCount == 0)
    {
        Image<Bgr, Byte> img = new Image<Bgr, byte>(640, 240);
        CvInvoke.PutText(img, String.Format("{0} camera found", devices.Length), new System.Drawing.Point(10, 60),
                         Emgu.CV.CvEnum.FontFace.HersheyDuplex,
                         1.0, new MCvScalar(0, 255, 0));
        Texture2D texture = TextureConvert.ImageToTexture2D(img, FlipType.Vertical);

        this.guiTexture.texture = texture;
        this.guiTexture.pixelInset = new Rect(-img.Width/2, -img.Height/2, img.Width, img.Height);
    }
    else
    {
        webcamTexture = new WebCamTexture(devices[0].name);

        baseRotation = transform.rotation;
        webcamTexture.Play();
        data = new Color32[webcamTexture.width * webcamTexture.height];
        CvInvoke.CheckLibraryLoaded();
    }

    tx2d = new Texture2D(webcamTexture.width, webcamTexture.height);
    tx2d.UpdateExternalTexture(webcamTexture.GetNativeTexturePtr());

    String fileName = "haarcascade_frontalface_alt2";
    filePath = Path.Combine(Application.persistentDataPath, fileName + ".xml");

    {
        //updateTextureWithString("start move cascade xml");
        TextAsset cascadeModel = Resources.Load<TextAsset>(fileName);

        #if UNITY_METRO
        UnityEngine.Windows.File.WriteAllBytes(filePath, cascadeModel.bytes);
        #else
        File.WriteAllBytes(filePath, cascadeModel.bytes);
        #endif
        //updateTextureWithString("File size: " + new FileInfo(filePath).Length);
    }
}

void Update()
{
    if (webcamTexture != null && webcamTexture.didUpdateThisFrame && tx2d != null)
    {           
        //OLD: this method is very expansive. Is preferable to use the reference to the array of pixels -> tx2d.UpdateExternalTexture(webcamTexture.GetNativeTexturePtr());
        //tx2d.SetPixels(webcamTexture.GetPixels());
        //tx2d.Apply();

        img = TextureConvert.Texture2dToImage<Rgba, Byte>(tx2d, FlipType.Vertical);

        using (CascadeClassifier classifier = new CascadeClassifier(filePath))
        using (Image<Gray, Byte> gray = img.Convert<Gray, byte>())
        {
            //updateTextureWithString("classifier create ok");
            Rectangle[] faces = null;
            try
            {                       
                faces = classifier.DetectMultiScale(gray);
                Debug.Log("faces: " + faces.Length.ToString());

                //updateTextureWithString("face detected");
                foreach (Rectangle face in faces)
                {
                    CvInvoke.Rectangle(img, face, new MCvScalar(0, 255, 0));
                }

            }
            catch (Exception e)
            {
                Debug.Log (e.ToString());
                //updateTextureWithString(e.Message);
                return;
            }


            //updateTextureWithString(String.Format("{0} face found on image of {1} x {2}", faces.Length, img.Width, img.Height));
        }

        renderer.material.mainTexture = TextureConvert.ImageToTexture2D(img, FlipType.None);    
    }

请注意,如果我使用被注释为OLD的代码,则Texture2D转换得很好。

我现在有第二个问题。我注意到,如果我将一个超过512x512的图像分配给分类器Unity崩溃,并且没有错误被&#34;抛出&#34;。

有人可以帮助我吗?

0 个答案:

没有答案