Communication between opencv c ++ and Unity3D

0

I have a problem that I hope you can help me solve:

I have to try to pass a cv :: Mat from c ++ to C # to convert it into a Texture2D. That is, I from c ++ capture the image of my Webcam using opencv. Once I have obtained the frame in cv :: Mat format, I have to pass it to C # to be able to show it in a Texture2D. The problem is that I do not get that communication. I add the code so you can advise me. Greetings and thanks

C ++:

__declspec(dllexport)
void video(unsigned char  *ptr, int wid, int high)
{
    VideoCapture camera;
    if (!camera.open(0))
    {
        return;
    }
    Mat frame;
    camera >> frame;
    if (!frame.empty())
    {
        ptr = frame.data;
        wid = frame.cols;
        high = frame.rows;
        imshow("CAMARA", frame);
    }
}

Unity C #

[DllImport("NativoPrincipio.dll")]public static extern void video(out IntPtr Image, out int wid, out int hig);
            IntPtr im_ptr = IntPtr.Zero;
            webcamTexture.Stop();
            int wid = webcamTexture.width;
            int hei = webcamTexture.height;
            video(out im_ptr, out wid, out hei);
            //byte* src = (byte*)im_ptr.ToPointer();
            Texture2D tex = Texture2D.CreateExternalTexture(wid, hei, TextureFormat.RGB24, false, false, im_ptr);
            File.WriteAllBytes("C:\Users\usortiz\Desktop\pepe.png", (byte[])tex.EncodeToPNG());
            GetComponent<Renderer>().material.mainTexture = tex;
    
asked by Urko Sanchez Ortiz 25.08.2017 в 10:50
source

1 answer

0
void video(unsigned char  *ptr, int wid, int high)
//                              ^^^      ^^^  

[DllImport("NativoPrincipio.dll")]public static extern void video(out IntPtr Image, out int wid, out int hig);
//                                                                                  ^^^          ^^^

If your idea is that the function video can return in wid and high values it is necessary that these parameters are references or pointers. Maybe it works simply by converting them into references:

void video(unsigned char  *ptr, int &wid, int &high) 
    
answered by 25.08.2017 в 10:56