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

问题描述 投票:1回答:1

我正在使用共享库在我的C ++应用程序中实现Matlab的功能匹配算法。问题是尽管我将四个参数传递给函数,但我仅获得一个参数的值。

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++ matlab shared-libraries parameter-passing
1个回答
1
投票

与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);

© www.soinside.com 2019 - 2024. All rights reserved.