保存图像数据为字节文件
将OpenCV Mat
结构中的data
数组保存为字节文件
关键
- 字节文件的读写;
unsigned char *
和char *
的转换
实现
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
void bin_write() {
std::string img_path = "../lena.jpg";
cv::Mat img = cv::imread(img_path, cv::IMREAD_COLOR);
std::ofstream file;
file.open("lena_512w_512h.bin", std::ios::out | std::ios::binary);
long length = img.rows * img.cols * img.channels();
file.write(reinterpret_cast<const char *>(img.data), length);
file.close();
}
void bin_read() {
cv::Mat img(512, 512, CV_8UC3);
long length = img.rows * img.cols * img.channels();
std::string bin_path = "lena_512w_512h.bin";
std::ifstream file;
file.open(bin_path, std::ios::in | std::ios::binary);
file.read(reinterpret_cast<char *>(img.data), length);
file.close();
cv::imshow("img", img);
cv::waitKey(0);
}