我正在使用C ++ Dll与Unity和插件系统进行斗争。 基本上我在C ++中有一个Dll,可以找到相机知识内在参数的外在参数。 当我创建.exe。
时,该函数运行顺畅我试图通过C ++中的Dll在Unity中使用该函数来获取存储在一维双数组中的外部参数。
我已经阅读了很多关于在Dll中导出函数并在C#中导入它们的内容,但是我已经花了4天的时间工作而没有任何作用。
我总是得到同样的错误:运行时错误和Unity崩溃(我认为是内存访问问题)。
这是C ++代码
#include <opencv2\opencv.hpp>
#include <stdio.h>
#ifdef CALIBRATION_EXPORTS
#define CALIBRATION_API __declspec(dllexport)
#else
#define CALIBRATION_API __declspec(dllimport)
#endif
using namespace cv;
using namespace std;
class CALIBRATION_API calib
{
public:
calib();
virtual ~calib();
void calcBoardCornerPosititions(Size boardSize, double squareSize, vector<Point3f>& corners);
Mat Nextimage(VideoCapture camera);
void find_extrinsic(double* Mat_Ext, double mat_int[], double mat_dist[], int size_ext, int size_int, int size_dist);
private :
VideoCapture camera;
};
extern "C" CALIBRATION_API calib* CreateCalib()
{
return new calib();
}
extern "C" CALIBRATION_API void DisposeCalib( calib* pObject)
{
if (pObject != NULL)
{
delete pObject;
pObject = NULL;
}
}
void calib::find_extrinsic(double *Mat_Ext, double mat_int[], double mat_dist[], int size_ext, int size_int, int size_dist)
{
if (size_ext == 16 && size_int == 9 && size_dist == 5)
{
Size boardSize = Size(9, 6);
double squareSize = 245;
//Matrices de résultats param ext
Mat_<double> rvecs_ext(3, 3);
Mat_<double> tvecs_ext(3, 1);
vector<Point3f> objectPoints_ext;
int id = 0;
Mat_<double> cameraMatrix(3, 3);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cameraMatrix.data[i, j] = mat_int[id];
id++;
}
}
Mat_<double> distCoeffs(5, 1);
for (int i = 0; i < 5; i++)
{
distCoeffs.data[i, 1] = mat_dist[i];
}
Mat result = Nextimage(camera);
Size imageSize = result.size();
vector<Point2f> pointBuf;
bool found = findChessboardCorners(result, boardSize, pointBuf, CALIB_CB_FAST_CHECK + CALIB_CB_FILTER_QUADS);
if (found)
{
calcBoardCornerPosititions(boardSize, squareSize, objectPoints_ext);
solvePnP(objectPoints_ext, pointBuf, cameraMatrix, distCoeffs, rvecs_ext, tvecs_ext, false, CV_ITERATIVE);
Mat_Ext[0] = rvecs_ext.at<double>(0, 0);
Mat_Ext[1] = rvecs_ext.at<double>(0, 1);
Mat_Ext[2] = rvecs_ext.at<double>(0, 2);
Mat_Ext[3] = tvecs_ext.at<double>(0, 0);
Mat_Ext[4] = rvecs_ext.at<double>(1, 0);
Mat_Ext[5] = rvecs_ext.at<double>(1, 1);
Mat_Ext[6] = rvecs_ext.at<double>(1, 2);
Mat_Ext[7] = tvecs_ext.at<double>(0, 2);
Mat_Ext[8] = rvecs_ext.at<double>(2, 0);
Mat_Ext[9] = rvecs_ext.at<double>(2, 1);
Mat_Ext[10] = rvecs_ext.at<double>(2, 2);
Mat_Ext[11] = rvecs_ext.at<double>(0, 3);
Mat_Ext[12] = rvecs_ext.at<double>(3, 0);
Mat_Ext[13] = rvecs_ext.at<double>(3, 1);
Mat_Ext[14] = rvecs_ext.at<double>(3, 2);
Mat_Ext[15] = 1;
}
}
}
}
extern"C" CALIBRATION_API void Call_find_extrinsic(calib* pObject, double *Mat_Ext, double mat_int[], double mat_dist[], int size_ext, int size_int, int size_dist)
{
if (pObject != NULL)
{
pObject->find_extrinsic(Mat_Ext, mat_int, mat_dist,size_ext,size_int,size_dist);
}
}
骄傲的C#代码:
[DllImport("Beta.dll")]
public static extern IntPtr CreateCalib();
[DllImport("Beta.dll")]
public static extern void DisposeCalib(IntPtr pCalibObject);
[DllImport("Beta.dll")]
public static extern void Call_find_extrinsic(IntPtr pCalibObject, double[] pMat_Ext, double[] mat_int, double[] mat_dist,
int size_ext, int size_int, int size_dist);
pCalibClass = CreateCalib();
double[]test_ext = new double[16];
Call_find_extrinsic(pCalibClass, test_ext,Intrinsic_mat,Dist_mat,test_ext.Length,Intrinsic_mat.Length,Dist_mat.Length);
我尝试使用find_extrinsic方法计算的值填充test_ext。
你能不能帮我解决这个问题。
非常感谢!
答案 0 :(得分:0)
C ++和C#函数的调用约定不匹配,__ cdecl是C和C ++程序的默认调用约定,而__stdcall是默认情况,如果您未在[DllImport]属性中明确指定它。如果约定不匹配,则会因堆栈损坏而导致崩溃。
请参阅this了解详情。
所以你应该在[DllImport]声明中添加CallingConvention = CallingConvention.Cdecl
。
答案 1 :(得分:0)
rvecs_ext 不应该是3x3矩阵,而是3x1向量。因此,当您访问禁止的内存空间时它会崩溃。
3x1向量是旋转矩阵的对角线,其他元素被视为空。为什么?看看this