【问题标题】:Passing Images to opencv from unity从统一将图像传递给opencv
【发布时间】:2018-05-03 01:47:42
【问题描述】:

我创建了一个 dll 来将两个图像从 unity 传递到 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() 函数正确获取图像时,我发现它产生了一个完整的黑色图像。

【问题讨论】:

  • 你需要开始评论 C# 和 C++ 函数的内容。将两个函数都设为空函数,然后查看是否存在问题。如果问题仍然存在,请将 C++ 中的 findPose 函数设为不返回任何内容的 void 函数,然后检查崩溃是否仍在发生。您可以使用它来查明问题的真正原因。
  • 注释掉时。它完美地工作,返回构造函数中定义的 0。
  • 继续。继续一一取消注释,直到崩溃。注释该行,然后再次取消注释以确保是问题所在,然后发布导致崩溃的那行代码。
  • 我不知道您是如何调用的以及您传递给calcOpticalFlowPyrLK 函数的内容。那部分代码甚至不在您的问题中,所以我认为没有该代码就无法提供帮助,但我怀疑您将 null 传递给它
  • 好吧,检查你传递给该函数的所有变量是否为空。使用我的Debug.Log 插件来节省您的时间。您应该能够在编辑器中看到 C++ 变量。例如if(img_1 == null){Debug::Log("img_1 is null", Color::Red);}。如果您仍然遇到错误,请注释掉calcOpticalFlowPyrLK,然后使用上面的Debug::Log 确保一切正常。

标签: c# c++ opencv unity3d dllexport


【解决方案1】:

我终于明白了。

是的,该代码运行良好。并且我的 C++ 代码或 C# 代码中没有错误。对我来说导致完全黑色图像的原因是,我在统一的strat() 函数中调用了我的dll 函数,它只执行一次。所以我发现在我的案例中,获取到 dll 的典型第一张图像大部分是黑色的,当我在 dll 中使用imshow() 函数进行测试时证明了这一点。它抛出错误的原因是因为我将该黑色图像传递给 calcOpticalFlowPyrLK() 函数,该函数没有任何要跟踪的特征点。

所以我在 dll 中创建了另一个函数来检查是否获取了真实图像,然后通过检查特征点将其传递给我的代码。

类似:

featureDetection(frame_1, points1);

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

现在一切都会完美无缺。衷心感谢帮助我解决这个问题的@Programmer。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多