将图像从 Unity 传递到 opencv
Passing Images to opencv from unity
我创建了一个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()
{
}
}
当我运行此代码时,它会抛出一个错误,指出:
运行时错误!
程序:
此应用程序已请求运行时在 不寻常的方式。请联系应用程序支持团队。
请帮助我解决我在这段代码中犯了什么错误。
提前感谢你!
更新:
在@Programmer的帮助下,我发现调用calcOpticalFlowPyrLK()
时代码崩溃了。当我检查我是否使用imshow()
功能正确获取图像时,我发现它会产生一个完整的黑色图像。
终于我想通了。
是的,代码运行良好。 并且我的C++代码或 C# 代码中没有错误。对我来说,导致一个完整的黑色图像的原因是,我将我的dll功能称为strat()
统一的函数,它只执行一次。所以我发现在我的案例中,获取到 dll 的典型第 1 张图像大多是黑色的,当我在 dll 中使用imshow()
函数进行测试时证明了这一点。它抛出错误的原因是因为我将黑色图像传递给calcOpticalFlowPyrLK()
函数,该函数没有任何要跟踪的特征点。
所以我在 dll 中创建了另一个函数,用于在通过检查功能点将其传递给我的代码之前检查是否获取了真实图像。
像这样:
featureDetection(frame_1, points1);
if (points1.size() > MIN_NUM_FEATURES)
{
//Pass the frame to my code.
}
现在事情将完美地运作。我衷心感谢帮助我解决这个问题的@Programmer。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- OpenCV C++.快速计算混淆矩阵
- Unity在虚幻引擎4中的"Vector3.Slerp"等效C++?
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 哪些库可以通过Opencv调整曝光率
- 是什么导致了Unity 3D中的"错误线程异常"?
- 安装opencv失败-粘贴CMakeError.log的内容
- C++中的openCV Mat访问冲突
- OpenCV Android C++ imwrite not found
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 在 Android 上使用 OpenCV 和 Unity
- 如何在 Unity Android 应用程序中的 OpenCV 中使用 ARCore 相机图像
- 将图像从 Unity 传递到 opencv
- 为什么 Unity 在与 OpenCV 链接时无法加载安卓原生库