【问题标题】:Opencv store to databaseOpencv 存储到数据库
【发布时间】:2010-07-06 21:28:19
【问题描述】:

有没有办法将 opencv 对象保存到数据库(如 oracle、mysql a.s.o)而不是 xml 文件?

CvSave 就像一个黑匣子。

【问题讨论】:

  • Open是为了开源,你有源代码,没有黑盒
  • 谢谢埃里克,你是对的。我的意思是,也许有比重新编译 opencv 更简单的方法。有人已经处理过opencv和数据库吗?
  • 我很确定您是否拥有 xml 文件,然后您可以解析该 xml 文件并将其保存到数据库中。

标签: c++ database opencv


【解决方案1】:

你的问题很好。 保存到 XML 需要更多的磁盘空间并且加载速度较慢。 我自己也遇到了问题,写了一个短代码,将 Mat 保存到光盘中, 您更改它以保存其他对象。

// Save matrix to binary file
int saveMat( const string& filename, const Mat& M){
    if (M.empty()){
       return 0;
    }
    ofstream out(filename.c_str(), ios::out|ios::binary);
    if (!out)
       return 0;

    int cols = M.cols;
    int rows = M.rows;
    int chan = M.channels();
    int eSiz = (M.dataend-M.datastart)/(cols*rows*chan);

    // Write header
    out.write((char*)&cols,sizeof(cols));
    out.write((char*)&rows,sizeof(rows));
    out.write((char*)&chan,sizeof(chan));
    out.write((char*)&eSiz,sizeof(eSiz));

    // Write data.
    if (M.isContinuous()){
       out.write((char *)M.data,cols*rows*chan*eSiz);
    }
    else{
       return 0;
    }
    out.close();
    return 1;
}

/****************************************************************************/
// Read matrix from binary file
int readMat( const string& filename, Mat& M){
    ifstream in(filename.c_str(), ios::in|ios::binary);
    if (!in){
       M = NULL_MATRIX;
       return 0;
    }
    int cols;
    int rows;
    int chan;
    int eSiz;

    // Read header
    in.read((char*)&cols,sizeof(cols));
    in.read((char*)&rows,sizeof(rows));
    in.read((char*)&chan,sizeof(chan));
    in.read((char*)&eSiz,sizeof(eSiz));

    // Determine type of the matrix 
    int type = 0;
    switch (eSiz){
    case sizeof(char):
         type = CV_8UC(chan);
         break;
    case sizeof(float):
         type = CV_32FC(chan);
         break;
    case sizeof(double):
         type = CV_64FC(chan);
         break;
    }

    // Alocate Matrix.
    M = Mat(rows,cols,type,Scalar(1));  

    // Read data.
    if (M.isContinuous()){
       in.read((char *)M.data,cols*rows*chan*eSiz);
    }
    else{
       return 0;
    }
    in.close();
    return 1;
}

【讨论】:

  • 也许添加一条注释,说明您的代码不处理 CV_8S 等签名类型
  • 这不回答实际问题,但非常有用。
猜你喜欢
  • 2015-04-30
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 2013-05-14
相关资源
最近更新 更多