c++ - 将 C++ 中的多个参数传递给 MatLab 共享库函数

标签 c++ matlab shared-libraries parameter-passing

我已经在 Matlab 中实现了一个特征匹配算法,并尝试在我的 C++ 应用程序中使用共享库来使用它。问题是虽然我将四个参数传递给函数,但我只获取一个参数的值。

Matlab 函数:

[c, d, e, f] = fm_test("C:\0.jpg", "C:\1.jpg");

function [FSC_1, FSC_2, NBCS_1, NBCS_2] = fm_test(path_im1, path_im2)
...
% Performed feature matching - output are 4 matrices with format n x 2 single
FSC_1 = matchedPoints1(inliersIndex, :);
FSC_2 = matchedPoints2(inliersIndex, :);  
NBCS_1 = matchedPoints1(inliers_NBCS, :);
NBCS_2 = matchedPoints2(inliers_NBCS, :);
end

我正在使用库编译器为 C++ 创建共享库并调用函数:

mclmcrInitialize();
    //const char *args[] = { "-nojvm" };
    //const int count = sizeof(args) / sizeof(args[0]);
    if (!mclInitializeApplication(NULL, 0)) {

        std::cerr << "Could not initialize the application properly" << std::endl;
        return -1;
    }

    if (!fm_testInitialize()) {
        std::cerr << "Could not initialize the library properly" << std::endl;
        return -1;
    }
    else {
        try {
            for (size_t i = 0; i < cameras.size() - 1; ++i){

            mwArray FSC_1, FSC_2, NBCS_1, NBCS_2;
            mwArray path_1 = cameras[i].image_path.c_str();
            mwArray path_2 = cameras[i+1].image_path.c_str();
            fm_test(1, FSC_1, FSC_2, NBCS_1, NBCS_2, path_1, path_2);

            // Convert mwArray to vector<double>
            std::cout << " Printing sizes of mwArray" << std::endl;
            std::cout << FSC_1.NumberOfElements() << std::endl; 
            std::cout << FSC_2.NumberOfElements() << std::endl;
            std::cout << NBCS_1.NumberOfElements() << std::endl;
            std::cout << NBCS_2.NumberOfElements() << std::endl;
            }
    
        }
        catch (const mwException& e) {
            std::cerr << e.what() << std::endl;
            return -2;
        }
        catch (...) {
            std::cerr << "Unexpected error thrown" << std::endl;
            return -3;
        }
        fm_testTerminate();
        
    }

结果例如:

Printing sizes of mwArray
100 
0
0
0

是否可以将多个参数传递给函数?我是否必须更具体地定义 mwArray

最佳答案

与 Matlab 网站上的示例相比,我需要向函数传递不同的第一个参数。 函数定义为:
extern LIB_fm_test_CPP_API void MW_CALL_CONV fm_test(int nargout, mwArray& FSC_1, mwArray& FSC_2, mwArray& NBCS_1, mwArray& NBCS_2, const mwArray& path_im1, const mwArray& path_im2);

第一个参数必须更改为我作为输出的参数数量(在我的例子中是 4)。正确的函数调用是:
fm_test(4, FSC_1, FSC_2, NBCS_1, NBCS_2, path_1, path_2);

关于c++ - 将 C++ 中的多个参数传递给 MatLab 共享库函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58587733/

相关文章:

python - 从 Python 代码运行来自 Windows 的命令

C++0x:std::function::target 和模板参数出错

c++ - 相当于 `find_if` 的二进制搜索

c++ - long long int 数组指针 该数组的算法

c - 破解库中的标准函数,然后调用 native 库函数

matlab - 如何在 MRI 图像中将头骨与大脑分割开

matlab - 当两个数值相同的对象具有不同的类时,为什么 MATLAB 的单元测试方法 verifyEqual 会失败?

python - Shiny 的 Python 3D 表面图的 Phong 着色

c - 如何保护我加载的插件?

git - Jenkins 抛出错误 "Library expected to contain at least one of src or vars directories"