0

I'm trying to call OpenCV methods through Unity. I compiled the dll in Visual Studio and imported into Unity and it does work.

I'm allocating texture data in C# as a byte array and passing it to C++ method as a pointer. C++ code looks like this

extern "C" void __declspec(dllexport) __stdcall  Init(ushort* data, int width, int height)
{
    // I tested this part and it works. After calling this method all of the bytes are changed in my byte array in C#. 
    for (int i = 0; i < width * height * 3; i++)
       data[i] = 0;

    // To initialize Mat I'm using this constructor. 
    Mat src(height, width, CV_8UC3, data);

    Mat bw;
    // Calling OpenCV method to convert colors. This part doesn't do anything to my byte array.
    cvtColor(src, bw, COLOR_RGB2GRAY);
}

And C# side

internal static class OpenCVInterop
{
    [DllImport("ConsoleApplication2")]
    internal static extern void Init(IntPtr data, int width, int height);
}

public class OpenCVFaceDetection : MonoBehaviour
{
    void Start()
    {
        byte[] buffer = inputTexture.GetRawTextureData();
        int width = inputTexture.width;
        int height = inputTexture.height;

        unsafe
        {
            fixed (byte* p = buffer)
            {
                IntPtr ptr = (IntPtr)p;
                OpenCVInterop.Init(ptr, width, height);
            }
        }
    }
}

From what I understand OpenCV methods should take my data and operate on it so after cvtColor method is called my byte array should be changed but it's not happening. I spent countless hours searching the web and testing different things and im stuck.

Powderek
  • 251
  • 3
  • 13
  • 1
    It looks like you want to copy the `cv::Mat` back to your pointer. This is done with `std::memcpy` then use `SetPixels32` on the c# side to update the Texture. See duplicate for full example. – Programmer Sep 22 '18 at 14:04
  • @Programmer ok I used `imshow("Unity Texture", src);` to test and the image is correctly converted to gray and showed. I tried using `memcpy(data, src.data, size);` but it crashes the Editor. Also why do I have to copy the data when im using the pointer? Shouldn't `Mat` operate on the data I provide? I wanted to avoid copying data. – Powderek Sep 22 '18 at 14:44
  • 1
    You have to use `unsigned char*` instead of `ushort*`. Also, not sure how you got your size. If your code is not working, copy exactly what's in the answer from the duplicate. It shows complete way to do this and it works. – Programmer Sep 22 '18 at 14:50
  • Yes, the size was wrong. I converted to grayscale so the size was 3 times smaller than original. Turns out I was only missing `memcpy`. Now it finally works! Cheers! – Powderek Sep 22 '18 at 16:27

0 Answers0