c++ - 在 C++ 中将 cv::Mat 的 vector 写入二进制文件

标签 c++ opencv binary ifstream ofstream

如标题所示,我有 std::vector<cv::Mat> matrices我想写入/读取二进制文件。

现在,关注 this回答,我应该做的就是写作:

 ofstream fout("matrices.bin", ios::out | ios::binary);
 size_t size = matrices.size();
 fout.write((char*)&size, sizeof(size));
 fout.write((char*)&matrices[0], v.size() * sizeof(cv::Mat));
 fout.close();

但是,以下this回答,写作cv::Mat objects 似乎有点棘手,在答案中 matReadmatWrite做这份工作。所以我想知道我是否应该做类似的事情而不是上面的代码:

ofstream fout("matrices.bin", ios::out | ios::binary);
size_t size = matrices.size();
fout.write((char*)&size, sizeof(size));
for(size_t i = 0 ; i < matrices.size() ; i++)
  matWrite("matrices.bin", matrices[i]);

然而,自matWrite() 起,这段代码就不起作用了。覆盖 matrices.bin在每个周期,所以我应该附加 matrices[i] 的大小作为写入矩阵本身之前的偏移量。

我该怎么办?

更新:

我想出了这个解决方案,重写了 matWritematRead带有可选参数,用于在写入期间附加矩阵并从某个点开始读取:

void matwrite(const std::string& filename, const cv::Mat& mat, const bool append = false) {

    std::ofstream fs;
    if(append)
        fs.open(filename.c_str(), std::fstream::binary | std::fstream::app);
    else
        fs.open(filename.c_str(), std::fstream::binary);

//the rest of matwrite is the same...

}

cv::Mat matRead(const std::string& filename, size_t &offset = 0)
{
    std::ifstream fs(filename, std::fstream::binary);
    fs.seekg(offset);
    ...
    offset += 4 * sizeof(int) + CV_ELEM_SIZE(type) * rows * cols; //update offset //move offset of 4 ints and mat size
    return mat;
}

函数调用方式:

//writing:
for(size_t i = 0 ; i<v.size() ; i++)
  writemat(filename, v[i], true);
//reading:
size_t offset = 0;
for(size_t i = 0 ; i<size ; i++){ // size = v.size() during writing
  cv::Mat mat = matRead(filename, offset);
  v.push_back(mat);
}

最佳答案

您可以修改 matread and matwrite 的代码如果 Mat 则与 vector 一起使用, 而不是单个 Mat .函数 vecmatreadvecmatwrite下面允许写一个std::vector<cv::Mat>到一个文件,然后读回 vector :

#include <opencv2\opencv.hpp>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

void vecmatwrite(const string& filename, const vector<Mat>& matrices)
{
    ofstream fs(filename, fstream::binary);

    for (size_t i = 0; i < matrices.size(); ++i)
    {
        const Mat& mat = matrices[i];

        // Header
        int type = mat.type();
        int channels = mat.channels();
        fs.write((char*)&mat.rows, sizeof(int));    // rows
        fs.write((char*)&mat.cols, sizeof(int));    // cols
        fs.write((char*)&type, sizeof(int));        // type
        fs.write((char*)&channels, sizeof(int));    // channels

        // Data
        if (mat.isContinuous())
        {
            fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart));
        }
        else
        {
            int rowsz = CV_ELEM_SIZE(type) * mat.cols;
            for (int r = 0; r < mat.rows; ++r)
            {
                fs.write(mat.ptr<char>(r), rowsz);
            }
        }
    }
}

vector<Mat> vecmatread(const string& filename)
{
    vector<Mat> matrices;
    ifstream fs(filename, fstream::binary);

    // Get length of file
    fs.seekg(0, fs.end);
    int length = fs.tellg();
    fs.seekg(0, fs.beg);

    while (fs.tellg() < length)
    {
        // Header
        int rows, cols, type, channels;
        fs.read((char*)&rows, sizeof(int));         // rows
        fs.read((char*)&cols, sizeof(int));         // cols
        fs.read((char*)&type, sizeof(int));         // type
        fs.read((char*)&channels, sizeof(int));     // channels

        // Data
        Mat mat(rows, cols, type);
        fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);

        matrices.push_back(mat);
    }
    return matrices;
}


int main()
{
    vector<Mat> matrices;

    // Fill vector...
    Mat1f m1(3,3);
    randu(m1, 0, 1);

    Mat3b m2(4, 5);
    randu(m2, Scalar(0,0,0), Scalar(256,256,256));

    Mat2d m3(2, 3);
    randu(m3, Scalar(0, 0), Scalar(1, 1));

    matrices.push_back(m1);
    matrices.push_back(m2);
    matrices.push_back(m3);

    // Write the vector to file
    vecmatwrite("test.bin", matrices);

    // Read the vector from file
    vector<Mat> matrices2 = vecmatread("test.bin");

    return 0;
}

关于c++ - 在 C++ 中将 cv::Mat 的 vector 写入二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41201641/

相关文章:

python - 如何创建关键点来计算 SIFT?

java - 将位数组值转换为二进制数字符串

c++11 随机 double 使用 uniform_real_distribution

c++ - 如何在 C++ 正则表达式中匹配换行符?

java - 解析后除草

opencv - 如何使用opencv平滑/模糊2张相似的图像

opencv - 了解视频中有多少人

c++ - 内联函数体的替换何时执行

c++ - 为什么需要为每个 Visual C++ 版本构建特殊的库(二进制文件)?

java - 确定日期是否是二进制的