简体   繁体   English

将图像从统一传递到opencv

[英]Passing Images to opencv from unity

I've created a dll to pass two images from unity to opencv and return a struct. 我创建了一个dll,以将两个图像从unity传递到opencv并返回一个struct。 But I'm getting a run time c++ error. 但是我遇到了运行时C ++错误。 This is the code I used in 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;
    }

And my C# code used in unity is: 我在统一中使用的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()
    {


    }
}

When I run this code, it throws an error saying: 当我运行此代码时,它将引发错误消息:

Runtime Error! 运行时错误!

Program: 程序:

This Application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact the application support team. 请与应用程序支持团队联系。

Kindly assist me on what wrong I've made in this code. 请协助我解决这段代码中的错误。

Thanking You in advance! 预先感谢您!

UPDATED: 更新:

With the help of @Programmer I found that the code is getting crashed when calling calcOpticalFlowPyrLK() . 在@Programmer的帮助下,我发现调用calcOpticalFlowPyrLK()时代码崩溃了。 And when checked if I'm getting the image correctly using imshow() function, I found it results a complete black image. 当检查使用imshow()函数是否正确获取图像时,我发现它会产生完整的黑色图像。

Finally I figured it out. 终于我明白了。

Yes that code worked well. 是的,代码运行良好。 and there is no error in my C++ code or C# code. 并且我的C ++代码或C#代码没有错误。 The reason that resulted a complete black image for me is, I call my dll fuction in side strat() function of unity, which only execute once. 对我而言导致完整的黑色图像的原因是,我在统一的strat()函数中调用了dll功能,该函数仅执行一次。 So I found in my case, that the typical 1st image acquired to dll will mostly a black one, this was proved when I tested with imshow() function inside the dll. 因此,在我的案例中,我发现,获取到dll的典型第1张图像大部分将是黑色的,这是在dll中使用imshow()函数进行测试时得到证明的。 The reason it throws error is because I'm passing that black image to calcOpticalFlowPyrLK() function, which doesn't have any feature points to track. 引发错误的原因是因为我将该黑色图像传递给了calcOpticalFlowPyrLK()函数,该函数没有任何可跟踪的特征点。

So I created another function inside dll to check if the true image is acquired before passing it to my code by checking for feature points. 因此,我在dll中创建了另一个函数,以检查是否获取了真实图像,然后再通过检查特征点将其传递给我的代码。

Something like: 就像是:

featureDetection(frame_1, points1);

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

Now things will perfectly work. 现在事情将完全正常。 My sincere thanks to the @Programmer who helped me to figure this out. 我衷心感谢@Programmer帮助我弄清楚了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM