gpt4 book ai didi

c# - Unity插件:编码(marshal)C++ double *将C#中的数组翻倍

转载 作者:行者123 更新时间:2023-12-02 17:50:18 33 4
gpt4 key购买 nike

我正在努力使用Unity和使用C++ Dll的插件系统。
基本上,我在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。

您能帮我解决这个问题吗?

非常感谢!

最佳答案

rvecs_ext不应该是3x3矩阵,而是3x1 vector 。因此,当您访问禁止的内存空间时,它会崩溃。

3x1 vector 是旋转矩阵的对角线,其他元素视为空值。为什么呢?看看this

关于c# - Unity插件:编码(marshal)C++ double *将C#中的数组翻倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23496166/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com