【发布时间】:2015-08-18 02:13:43
【问题描述】:
我目前正在尝试序列化和反序列化 openCV Mat,以便我可以使用 Boost 将帧从客户端发送到服务器。我遇到的问题是,当我反序列化图像时,它会给出不同颜色的重复重叠图像。我不确定为什么会这样。任何帮助将非常感激。很抱歉,我没有足够的徽章,无法发布图片。
cv::Mat 自定义序列化的头文件
#ifndef cv__Mat_Serialization_serialization_h
#define cv__Mat_Serialization_serialization_h
BOOST_SERIALIZATION_SPLIT_FREE(cv::Mat)
namespace boost {
namespace serialization {
template<class Archive>
void save(Archive & ar, const cv::Mat& mat, const unsigned int version) {
size_t elementSize = mat.elemSize();
size_t elementType = mat.type();
ar << mat.cols;
ar << mat.rows;
ar << elementSize;
ar << elementType;
for(int y = 0; y < mat.rows*mat.cols*(elementSize); y++) {
ar << mat.data[y];
}
}
template<class Archive>
void load(Archive & ar, cv::Mat& mat, const unsigned int version) {
int cols = 0;
int rows = 0;
size_t elementSize;
size_t elementType;
ar >> cols;
ar >> rows;
ar >> elementSize;
ar >> elementType;
mat.create(rows,cols,static_cast<int>(elementType));
for(int y = 0; y < mat.rows*mat.cols*(elementSize); y++) {
ar >> mat.data[y];
}
}
}
}
#endif
主代码
#include "serialization.h"
using namespace std;
using namespace cv;
using namespace boost;
Mat frame;
void saveMat(Mat& m, string filename);
void loadMat(Mat& m, string filename);
int main(int argc, const char * argv[]) {
// insert code here...
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); //Capture using any camera connected to your system
cvNamedWindow("serialization", 2); //Create window
while(1) {
frame = cvQueryFrame(capture);
saveMat(frame, "archive.bin");
cv::Mat frame2;
loadMat(frame2, "archive.bin");
IplImage tmp = frame2;
cvShowImage("serialization", &tmp);
}
return 0;
}
void saveMat(Mat& m, string filename) {
ofstream ofs(filename.c_str());
archive::binary_oarchive oa(ofs);
oa << m;
}
void loadMat(Mat& m, string filename) {
ifstream ifs(filename.c_str());
archive::binary_iarchive ia(ifs);
ia >> m;
}
enter code here
【问题讨论】:
标签: c++ opencv serialization boost