简体   繁体   中英

Convert mwArray to std::vector<double>

I am using a feature matching algorithm that is coded in Matlab. I created shared libraries for C++ using the Library Compiler of Matlab.
As a result I am getting a mwArray consisting of n feature points and the point coordinates x,y (n rows, 2 cols). Now I would like to convert the mwArray into a std::vector<double> or even better a std::vector<cv::Point2d> so I can proceed.

I tried using the methods GetData() but I don't know which arguments I have to use. Here is the code:

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 (!MatchingInitialize()) {
        std::cerr << "Could not initialize the library properly" << std::endl;
        return -1;
    }
    else {
        try {

            // Create the output arrays
            mwArray FSC_1, FSC_2, NBCS_1, NBCS_2;
            mwArray path_1 = "C:\\test\\img_1.jpg";
            mwArray path_2 = "C:\\test\\img_2.jpg";
            Matching(1, FSC_1, FSC_2, NBCS_1, NBCS_2, path_1, path_2);



            // Output that works            
            std::cout << "The value is " << FSC_1 << std::endl;

            // Conversions I tried
            double *FSC_1_Copy = mxGetPr(FSC_1.GetData());

            std::vector<double> FSC_1_Copy = FSC_1.GetData();

            std::vector<double> data_copy;
            FSC_1.GetData(data_copy, FSC_1.RowIndex());

        }
        catch (const mwException& e) {
            std::cerr << e.what() << std::endl;
            return -2;
        }
        catch (...) {
            std::cerr << "Unexpected error thrown" << std::endl;
            return -3;
        }
        MatchingTerminate();

    }

You can copy the matrix into a vector with

// Create a vector with size FSC_1.NumberOfElements()
// FSC_1_Copy allocates memory for an array
std::vector<double> FSC_1_Copy(FSC_1.NumberOfElements());

// Copy up to FSC_1.NumberOfElements() elements from FSC_1 into
// the array of FSC_1_Copy
FSC_1.GetData(FSC_1_Copy.data(), FSC_1.NumberOfElements());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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