opencv--把cv::Mat数据转为二进制数据的保存和读取

保存

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

void saveMatToBinary(const cv::Mat& mat, const std::string& filename) {
    std::ofstream ofs(filename, std::ios::binary);
    if (!ofs.is_open()) {
        std::cerr << "Failed to open file for writing: " << filename << std::endl;
        return;
    }

    // Write the matrix type and size
    int type = mat.type();
    int rows = mat.rows;
    int cols = mat.cols;
    ofs.write((char*)&type, sizeof(type));
    ofs.write((char*)&rows, sizeof(rows));
    ofs.write((char*)&cols, sizeof(cols));

    // Write the matrix data
    ofs.write((char*)mat.data, mat.total() * mat.elemSize());

    ofs.close();
}

int main() {
    cv::Mat image = cv::imread("path_to_your_image.jpg");
    if (image.empty()) {
        std::cerr << "Failed to load image" << std::endl;
        return -1;
    }

    saveMatToBinary(image, "output.bin");
    return 0;
}

读取

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

cv::Mat loadMatFromBinary(const std::string& filename) {
    std::ifstream ifs(filename, std::ios::binary);
    if (!ifs.is_open()) {
        std::cerr << "Failed to open file for reading: " << filename << std::endl;
        return cv::Mat();
    }

    // Read the matrix type and size
    int type, rows, cols;
    ifs.read((char*)&type, sizeof(type));
    ifs.read((char*)&rows, sizeof(rows));
    ifs.read((char*)&cols, sizeof(cols));

    // Create the matrix
    cv::Mat mat(rows, cols, type);

    // Read the matrix data
    ifs.read((char*)mat.data, mat.total() * mat.elemSize());

    ifs.close();
    return mat;
}

int main() {
    cv::Mat loadedImage = loadMatFromBinary("output.bin");
    if (loadedImage.empty()) {
        std::cerr << "Failed to load image from binary file" << std::endl;
        return -1;
    }

    cv::imshow("Loaded Image", loadedImage);
    cv::waitKey(0);
    return 0;
}

相关推荐

  1. opencv--cv::Mat数据转为二进制数据保存读取

    2024-07-12 23:42:02       20 阅读
  2. pytorch对音频数据读取保存

    2024-07-12 23:42:02       28 阅读
  3. python 几种常见音频数据读取保存方式

    2024-07-12 23:42:02       47 阅读
  4. sqlalchemy读取日志数据保存数据库

    2024-07-12 23:42:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 23:42:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 23:42:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 23:42:02       58 阅读
  4. Python语言-面向对象

    2024-07-12 23:42:02       69 阅读

热门阅读

  1. 扫地机器人如何进行MTBF测试

    2024-07-12 23:42:02       18 阅读
  2. ffmpeg和imagemagick制作gif动图

    2024-07-12 23:42:02       22 阅读
  3. 基于深度学习的PID

    2024-07-12 23:42:02       20 阅读
  4. 【C++】C++中struct结构体和class类的区别

    2024-07-12 23:42:02       16 阅读
  5. CAS详解

    CAS详解

    2024-07-12 23:42:02      16 阅读
  6. Go语言详细教程

    2024-07-12 23:42:02       19 阅读
  7. Windows 安装Zookeeper

    2024-07-12 23:42:02       19 阅读
  8. 初学者必看的 3 个 Python 小项目

    2024-07-12 23:42:02       21 阅读
  9. 【Linux】docker和docker-compose 区别是什么

    2024-07-12 23:42:02       17 阅读
  10. EG800K GPS开发

    2024-07-12 23:42:02       20 阅读