保存
#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;
ret