将mwArray转换为std :: vector <double>

时间:2019-10-23 08:37:10

标签: c++ matlab opencv shared-libraries feature-extraction

我正在使用Matlab中编码的特征匹配算法。我使用Matlab的库编译器为C ++创建了共享库。
结果,我得到一个包含n个特征点和点坐标x,y(n行,2列)的mwArray。现在,我想将mwArray转换为std::vector<double>或什至更好的std::vector<cv::Point2d>,以便我可以继续。

我尝试使用方法GetData(),但不知道必须使用哪些参数。 这是代码:

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

    }

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法将矩阵复制到向量中

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