【问题标题】:Passing multiple arguments in C++ to MatLab shared library function将 C++ 中的多个参数传递给 MatLab 共享库函数
【发布时间】:2019-10-28 08:34:49
【问题描述】:

我在 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 吗?

【问题讨论】:

  • C++函数fm_test是如何定义的?
  • extern LIB_fm_test_CPP_API void MW_CALL_CONV fm_test(int nargout, mwArray&amp; FSC_1, mwArray&amp; FSC_2, mwArray&amp; NBCS_1, mwArray&amp; NBCS_2, const mwArray&amp; path_im1, const mwArray&amp; path_im2);
  • 就是这样!我必须将 int nargout 设置为 4 并且它有效!感谢您朝着正确的方向前进。我只是习惯了调用该函数的 Matlab 网站的示例。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多