简体   繁体   English

将C++中的多个arguments传递给MatLab共享库ZC1C425268E68385D1AB5074C17A94F1

[英]Passing multiple arguments in C++ to MatLab shared library function

I've implemented a feature matching algorithm in Matlab and trying to use it in my C++ application using shared libraries.我在 Matlab 中实现了一个特征匹配算法,并尝试在我的 C++ 应用程序中使用共享库。 The problem is that I am only getting values for one argument though I am passing four arguments to the function.问题是尽管我将四个 arguments 传递给 function,但我只获得了一个参数的值。

The Matlab function: Matlab function:

[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

I am using the Library Compiler to create shared libraries for C++ and call the function:我正在使用库编译器为 C++ 创建共享库并调用 function:

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();
        
    }

The result is eg:结果是例如:

Printing sizes of mwArray
100 
0
0
0

Is it possible to pass multiple arguments to the function?是否可以将多个 arguments 传递给 function? Do I have to define the mwArray more specifically?我必须更具体地定义mwArray吗?

I needed to pass a different first argument to the function in contrast to the example on the Matlab website.与 Matlab 网站上的示例相比,我需要将不同的第一个参数传递给 function。 The function is defined as: function 定义为:
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);

The first argument has to be changed to the number of arguments I have as output (in my case 4).第一个参数必须更改为我拥有的 arguments 的数量为 output(在我的情况下为 4)。 The correct function call is:正确的 function 调用是:
fm_test(4, FSC_1, FSC_2, NBCS_1, NBCS_2, path_1, path_2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM