【问题标题】:Serialization of cv::Mat giving strange resultcv::Mat 的序列化给出奇怪的结果
【发布时间】: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


    【解决方案1】:

    我已经使用了你的确切代码,添加了waitKey(...) 来显示输出,没问题:

    不过,您应该在 for 循环中使用 size_t 而不是 int。你可以稍微减少变量的生命周期:

    while(1) {
        {
            Mat frame = cvQueryFrame(capture); 
    
            saveMat(frame, "archive.bin");
        }
    
        {
            cv::Mat frame2;
            loadMat(frame2, "archive.bin");
            IplImage tmp = frame2;
    
            cvShowImage("serialization", &tmp);
        }
    
        if(waitKey(30) == 'q') break;
    }
    

    如果你有这个问题,我想你的捕获源可能有不同的图像表示,你可能想做类似的事情

        cvtColor(frame2, frame3, CV_BGR2RGB);
    

    等等

    【讨论】:

    • 非常感谢,原来我必须在调用 saveMat 函数之前添加它。
    • cvtColor(frame, frame, CV_BGR2BGRA);
    • 您知道为什么会这样吗?如果有任何问题,我正在使用 macbook pro 的网络摄像头。
    猜你喜欢
    • 2020-05-27
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多