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.