I connected OpenCV 4.1.0 to Unity3d project creating DLL with Android Studio (like said in this tut LINK ). I would like to convert a Texture in grayscale using the DLL on Android platform.
I have a Plane gameObject in the scene with a texture.
View Scene
Texture Settings
Here the code in:
C++
extern "C" {
uint8_t *resultBuffer;
int bufferWidth;
int bufferHeight;
int InitCV_Internal(int width, int height) {
size_t size = width * height * 4;
resultBuffer = new uint8_t[size];
bufferWidth = width;
bufferHeight = height;
return 0;
}
// Convert the image in grayscale.
uint8_t *SubmitFrame_Internal(int width, int height, uint8_t *buffer) {
Mat inFrame = Mat(height, width, CV_8UC4, buffer);
Mat outFrame(inFrame.rows, inFrame.cols, CV_8UC4, Scalar(0,0,0));
cvtColor(inFrame, outFrame, COLOR_RGBA2GRAY);
size_t size = bufferWidth * bufferHeight * 4;
memcpy(resultBuffer, outFrame.data, size);
inFrame.release();
outFrame.release();
return resultBuffer;
}
C#
NativeLibAdapter class is to handle the DLL functions.
using System.Runtime.InteropServices;
using System;
using UnityEngine;
public class NativeLibAdapter {
[DllImport("native-lib")]
private static extern int InitCV_Internal(int width, int height);
[DllImport("native-lib")]
private static extern IntPtr SubmitFrame_Internal(int width, int height, IntPtr buffer);
public static int InitCVAdapter(int width, int height)
{
int result = InitCV_Internal(width, height);
return result;
}
public static IntPtr SubmitFrameAdapter(int width, int height, IntPtr buffer)
{
IntPtr bufferResult = SubmitFrame_Internal(width, height, buffer);
return bufferResult;
}
}
Test class is attached to Plane object of the scene.
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
private Texture2D texIn;
private Color32[] pixel32;
private GCHandle pixelHandle;
private IntPtr pixelPtr;
public Text text;
void Start()
{
texIn = (Texture2D)GetComponent<Renderer>().material.mainTexture;
InitTexture();
}
void InitTexture()
{
pixel32 = texIn.GetPixels32();
// Pin pixel32 array
pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
// Get the pinned address
pixelPtr = pixelHandle.AddrOfPinnedObject();
NativeLibAdapter.InitCVAdapter(texIn.width, texIn.height);
// MatToTexture2D
IntPtr result = NativeLibAdapter.SubmitFrameAdapter(texIn.width, texIn.height, pixelPtr);
int bufferSize = texIn.width * texIn.height * 4;
byte[] rawData = new byte[bufferSize];
if (result != IntPtr.Zero)
{
Marshal.Copy(result, rawData, 0, bufferSize);
texIn.LoadRawTextureData(rawData);
texIn.Apply();
// text.text = result.ToString();
}
}
void OnApplicationQuit()
{
// Free handle
pixelHandle.Free();
}
}
After Build
On the left my result on android device, on the right the result I expected (generated with photoshop). The text "-1068498944" I inserted only to debug.
What's wrong? Any ideas? Thank you so much!