来自Unity WebCamTexture的OpenCV4Android Mat

时间:2018-12-17 20:22:12

标签: c# android c++ opencv unity3d

在Unity Game Engine中,我试图将数据从WebCamTexture传递到链接到Android上的OpenCV4Android的本地C ++插件。我尝试遵循this tutorial,但是构造Mat时出了点问题。当我尝试在调用cvtColor时使用它时,对于setSize中的s> = 0,断言失败。我在构造函数之后添加了断言,以测试行或列是否为负数,并且仅是行。

这是C ++代码:

JNIEXPORT void JNICALL Detect(
    JNIEnv* env, jobject jobj, Color32* rawImagePixels, jint imageWidth, jint imageHeight, jint maxDetectedShapes, jintArray outDetectedShapesCountSingleArray, jintArray outPointsPerShapeArray)
{
    Mat frame(imageHeight, imageWidth, CV_8UC4, rawImagePixels);
    assert(frame.cols >= 0);
    assert(frame.rows >= 0);

    vector<vector<Point>> contours;
    // Convert the frame to grayscale for contour detection.
    Mat grayscaleFrame;
    cvtColor(frame, grayscaleFrame, COLOR_BGR2GRAY);
    Mat resizedGray;
}

这是C#代码:

public void TestLibCall()
{
    if(phoneCameraDisplay.camAvailable)
    {
        Color32[] rawImagePixels = phoneCameraDisplay.RenderingCameraTexture.GetPixels32();
        Array.Reverse(rawImagePixels);
        Debug.Log("Input:" + phoneCameraDisplay.RenderingCameraTexture.width + "|" + phoneCameraDisplay.RenderingCameraTexture.height);
        OpenCVInterop.Detect(rawImagePixels, phoneCameraDisplay.RenderingCameraTexture.width, phoneCameraDisplay.RenderingCameraTexture.height, _maxShapeDetectCount, ref _detectedShapesCount, _pointsPerShape);
    }
}

当我调试记录时,WebCamTexture的宽度和高度为正数。不反转Color32阵列也无济于事。有谁知道在构造OpenCV矩阵时会导致哪种行错误的错误?谢谢。

[编辑:]我在Mat构造函数之前添加了其他断言,并且根据Debug.Log,它的imageHeight不大于或等于0,尽管它为1080。

0 个答案:

没有答案