使用C ++中的zeromq将序列化Mat对象发送到另一台计算机

时间:2016-02-26 13:22:17

标签: c++ sockets opencv zeromq

您好我正在尝试使用zeromq和boost将mat对象发送到另一台计算机。

这是我的serialization.h文件

#include <iostream>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>

BOOST_SERIALIZATION_SPLIT_FREE(cv::Mat)
namespace boost {
    namespace serialization {

        /*** Mat ***/
        template<class Archive>
        void save(Archive & ar, const cv::Mat& m, const unsigned int version)
        {
            size_t elemSize = m.elemSize(), elemType = m.type();

            ar & m.cols;
            ar & m.rows;
            ar & elemSize;
            ar & elemType; // element type.
            size_t dataSize = m.cols * m.rows * m.elemSize();


            for (size_t dc = 0; dc < dataSize; ++dc) {
                ar & m.data[dc];
            }
        }

        template<class Archive>
        void load(Archive & ar, cv::Mat& m, const unsigned int version)
        {
            int cols, rows;
            size_t elemSize, elemType;

            ar & cols;
            ar & rows;
            ar & elemSize;
            ar & elemType;

            m.create(rows, cols, elemType);
            size_t dataSize = m.cols * m.rows * elemSize;

            //cout << "reading matrix data rows, cols, elemSize, type, datasize: (" << m.rows << "," << m.cols << "," << m.elemSize() << "," << m.type() << "," << dataSize << ")" << endl;

            for (size_t dc = 0; dc < dataSize; ++dc) {
                ar & m.data[dc];
            }
        }

    }
}

这是我如何序列化Mat对象并发送它。

#include <zmq.hpp>
#include <string>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <fstream>
#include "Serialization.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

using namespace std;
using namespace cv;


std::string save( const cv::Mat & mat )
{
    std::ostringstream oss;
    boost::archive::text_oarchive toa( oss );
    toa << mat;

    return oss.str();
}



int main () {
    Mat img = imread("/Users/Rodrane/Downloads/barbara.pgm", 0);   // Read the file


    std::string serialized = save(img);


    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REQ);

    std::cout << "Connecting to hello world server…" << std::endl;
    socket.connect ("tcp://localhost:5555");

    //  Do 10 requests, waiting each time for a response
    for (int request_nbr = 0; request_nbr != 10; request_nbr++) {
        zmq::message_t request (sizeof(serialized));
        memcpy (request.data (), &serialized, sizeof(serialized));
        std::cout << "Sending Hello " << request_nbr << "…" << std::endl;
        socket.send (request);

        //  Get the reply.
        zmq::message_t reply;
        socket.recv (&reply);
        std::cout << "Received World " << request_nbr << std::endl;
    }
    return 0;



}

这就是我收到序列化对象并尝试显示它的方式。

#include <zmq.hpp>
#include <string>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <fstream>
#include "Serialization.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

using namespace std;

void load( cv::Mat & mat, const char * data_str )
{
    std::stringstream ss;
    ss << data_str;

    boost::archive::text_iarchive tia( ss );
    tia >> mat;
}

int main () {
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REP);
    socket.bind ("tcp://*:5555");
    cv::Mat object;

    while (true) {
        zmq::message_t recivedData;
        socket.recv (&recivedData);
        std::string rpl = std::string(static_cast<char*>(recivedData.data()), recivedData.size());
        const char *cstr = rpl.c_str();
        load(object,cstr);
        imshow("asdasd",object);
        //  Send reply back to client
        zmq::message_t reply (8);
        memcpy (reply.data (), "Recieved", 8);
        socket.send (reply);
  }



}

当我运行这两个项目时,我检索到一个错误

  

libc ++ abi.dylib:以未捕获的类型异常终止   zmq :: error_t:系统调用中断

因为它发生在我运行客户端项目的时候,我假设服务器接收数据但是它已损坏或者反序列化它有问题

1 个答案:

答案 0 :(得分:2)

我没有查看您的所有代码,但这是100%错误:

sizeof(std::string)

std::string与所持字符串的大小无关,您无法从zmq::message_t request (serialized.length()); memcpy (request.data (), serialized.c_str(), serialized.length()); 对象进行memcpy。你想要做的是:

extract-payload = "false"

也可能是其他错误,但这只是突出。