-->

从统一传递图像至的OpenCV(Passing Images to opencv from unit

2019-09-30 17:57发布

我创建了一个DLL通过从统一两个图像的OpenCV,并返回一个结构。 但我发现了一个运行时C ++错误。 这是我在C ++中使用的代码:

    struct rigidTransform
    {
        rigidTransform(double eular_angle_x, double eular_angle_y, double eular_angle_z, double eular_dis_x, double eular_dis_y, double eular_dis_z) : Eular_angle_x(eular_angle_x), Eular_angle_y(eular_angle_y), Eular_angle_z(eular_angle_z), Eular_dis_x(eular_dis_x), Eular_dis_y(eular_dis_y), Eular_dis_z(eular_dis_z) {}
        double Eular_angle_x;
        double Eular_angle_y;
        double Eular_angle_z;

        double Eular_dis_x;
        double Eular_dis_y;
        double Eular_dis_z;
    };
        struct Color32
    {
        uchar r;
        uchar g;
        uchar b;
        uchar a;
    };

    extern "C" rigidTransform __declspec(dllexport) __stdcall findPose(Color32* img1, Color32* img2,  int width, int height)
    {
    Mat Img1(height, width, CV_8UC4, img1), Img2(height, width, CV_8UC4, img2);
    //my pose estimation code here

    return Tr;
    }

而在统一使用我的C#代码为:

    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using UnityEngine;

    public struct regidTransform
    {
        public double eular_angle_x;
        public double eular_angle_y;
        public double eular_angle_z;
        public double eular_dis_x;
        public double eular_dis_y;
        public double eular_dis_z;
    }



public class UI : MonoBehaviour
{
    [DllImport("test2", EntryPoint = "findPose", CallingConvention = CallingConvention.Cdecl)]
    public unsafe static extern regidTransform findPose(Color32[] img1, Color32[] img2, int width, int height);

    public regidTransform results_;


    public WebCamTexture webcamTexture;
    public Color32[] img1;
    public Color32[] img2;

    void Start()
    {

        results_ = new regidTransform();




        webcamTexture = new WebCamTexture();
        webPlane.GetComponent<MeshRenderer>().material.mainTexture = webcamTexture;
        webcamTexture.Play();

        if(webcamTexture.isPlaying)
        {
            img1 = webcamTexture.GetPixels32();
            System.Array.Reverse(img1);
            img2 = webcamTexture.GetPixels32();
            System.Array.Reverse(img2);


            unsafe
            {

                    results_ = findPose(img1, img2, webcamTexture.width, webcamTexture.height);


            }
        }




    }


    void Update()
    {


    }
}

当我运行这段代码,它抛出一个错误说:

运行时错误!

程序:

此应用程序已请求运行时终止它在一个不寻常的方式。 请联系技术支持队伍。

请帮助我什么错误我在此代码做了。

请多多关照!

更新:

随着@Programmer的帮助下,我发现,当调用的代码是越来越坠毁calcOpticalFlowPyrLK() 并检查时,如果我得到正确的图像使用imshow()函数,我发现它会导致一个完整的黑色图像。

Answer 1:

最后我想通了。

是的,这代码运行良好。 并且在我的C ++代码或C#代码没有错误。 这导致一个完整的黑色形象对我来说是个中原因,我呼吁在侧我的DLL机能的研究strat()统一的功能,只执行一次。 所以,我在我的案例中发现,该收购对DLL典型的第一图象将主要一个黑色,这是当我测试证明了imshow()的DLL中的功能。 它会引发错误的原因是因为我传递的是黑色图像calcOpticalFlowPyrLK()函数,它不具有任何特征点进行跟踪。

所以我创造了另一个函数内部DLL来检查,如果真像被检查的特征点它传递给我的代码之前获得的。

就像是:

featureDetection(frame_1, points1);

if (points1.size() > MIN_NUM_FEATURES)
{
    //Pass the frame to my code.
}

现在,事情会很好地工作。 我真诚地感谢@Programmer谁帮我想出解决办法。



文章来源: Passing Images to opencv from unity