首页 > 解决方案 > 我想从 C++ 代码将二维数组传递给八度函数“imregionalmax”

问题描述

我编写了一个 C++ 代码,我在其中读取一个 CSV 文件并将其存储到一个二维数组,然后将其传递给八度函数imregionalmax。但是代码给出了分段错误(核心转储)。请在这件事上帮助我。如果可能,请提供 C++ 代码。我在下面附上我的代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>

#define ROW 100
#define COL 1000

int main (int argc, char **argv) {

    std::ifstream f;
    f.open ("Mat30.csv");
    if (! f.is_open()) {    /* validate file open for reading */
        std::cerr << "error: file open failed '" << argv[1] << "'.\n";
        return 1;
    }
    std::string line, val;                  
    std::vector<std::vector<float>> array;

    while (std::getline (f, line)) {        /* read each line */
        std::vector<float> v;                 /* row vector v */
        std::stringstream s (line);         /* stringstream line */
        while (getline (s, val, ','))       /* get each value (',' delimited) */
            v.push_back (std::stof (val));  /* add to row vector */
        array.push_back (v);                /* add row vector to array */


    string_vector argv_octave (2);
    argv_octave(0) = "embedded";
    argv_octave(1) = "-q";
    octave_main (1, argv_octave.c_str_vec (), 2);

    octave_value_list in1 , in2;

    for(octave_idx_type i = 0 ; i < ROW ; i++) {
        for(octave_idx_type j = 0 ; j < COL ; j++)
            in1(j) = octave_value(array[i][j]);
        in2(i) = in1;
    }
    
    // segmentation fault (core dumped) occurs here. Execution is not going further
    octave_value_list out = feval ("imregionalmax", in2, 1);
    return 0;
}

我正在使用 ubuntu 18.04 并且 Octave 安装在我的机器中我正在通过编写终端“ mkoctfile --link-stand-alone file_name.cpp -o file_name && ./file_name ”来运行代码

标签: c++apilinkeroctave

解决方案


推荐阅读