c# - 将图像从 unity 传递到 opencv

标签 c# c++ opencv unity3d dllexport

我创建了一个 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;
    }

而我在unity中使用的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()
    {


    }
}

当我运行这段代码时,它会抛出一条错误消息:

Runtime Error!

Program:

This Application has requested the Runtime to terminate it in an unusual way. Please contact the application support team.

请帮助我解决我在这段代码中犯的错误。

在此先感谢您!

UPDATED:

在@Programmer 的帮助下,我发现调用 calcOpticalFlowPyrLK() 时代码崩溃了。当检查我是否使用 imshow() 函数正确获取图像时,我发现它生成了一个完整的黑色图像。

最佳答案

我终于明白了。

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

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

类似于:

featureDetection(frame_1, points1);

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

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

关于c# - 将图像从 unity 传递到 opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381893/

相关文章:

c# - SqlDataReader 没有返回所有行

c# - EF 4 和 POCO 的最佳实践/入门解决方案

c++ - 为什么我的 DLL 被 MSE 检测为可疑?

c++ - 如何在 QGraphicsView 的边框上制作标尺

python - 使用 OpenCV 确定一个点是否在 ROI 内

c# - 将匿名类型的对象作为参数传递给方法

c# - 比较地址的两个字符串时如何获得百分比准确度匹配?

c++ - Lambda 与排序函数

python - 代码检测网络摄像头时,cv2 模块未捕获图像

c++ - 用于 cv::Mat 参数的 OpenCV std::vector