Serialization of cv::Mat giving strange result

核能气质少年 提交于 2021-02-10 03:06:12

问题


I'm currently trying to serialize and deserialize an openCV Mat so that i can send the frame from a client to server using Boost. The problem i am having is that when i deserialise the image it gives duplicate overlapping images in different colours. I am not sure why this is happening. Any help would be much appreciated. I'm sorry I cannot post an image as i do not have enough badges.

header file for custom serialisation of 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

code for main

#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

回答1:


I've used your exact code, adding a waitKey(...) to show the output, no problem:

You should use size_t instead of int in the for loops, though. And you could reduce lifetimes of variables a bit:

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;
}

If you have this problem, I imagine your capture source may have a different image representation, and you may want to do something like

    cvtColor(frame2, frame3, CV_BGR2RGB);

etc.



来源:https://stackoverflow.com/questions/32062497/serialization-of-cvmat-giving-strange-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!