Grillteller
Grillteller

Reputation: 941

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. The problem is that I am only getting values for one argument though I am passing four arguments to the function.

The 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:

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 e.g.:

Printing sizes of mwArray
100 
0
0
0

Is it possible to pass multiple arguments to the function? Do I have to define the mwArray more specifically?

Upvotes: 1

Views: 146

Answers (1)

Grillteller
Grillteller

Reputation: 941

I needed to pass a different first argument to the function in contrast to the example on the Matlab website. The function is defined as:
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). The correct function call is:
fm_test(4, FSC_1, FSC_2, NBCS_1, NBCS_2, path_1, path_2);

Upvotes: 1

Related Questions