gcswoosh
gcswoosh

Reputation: 1299

Sending Opencv Mat through a TCP connection in C++

I'm trying to send OpenCv Mat over TCP connections. Up to now I was able to set a up a simple TCP connection following the boost::asio tutorial.

I'd like to know if I can simple modify that example and put a cv::Mat image_ in the place of the string message_ inside the tcp connection object or if I should use a different strategy to provide images to the client.

Upvotes: 3

Views: 2115

Answers (2)

schorsch_76
schorsch_76

Reputation: 862

Just try this to improve the serialization of cv::Mat

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

    boost::serialization::binary_object data(m.data, data_size);
    ar & data;
}

/** Serialization support for cv::Mat */
template<class Archive>
void load(Archive & ar, ::cv::Mat& m, const unsigned int version)
{
    int cols, rows;
    size_t elem_size, elem_type;
    ar & cols;
    ar & rows;
    ar & elem_size;
    ar & elem_type;
    m.create(rows, cols, elem_type);
    size_t data_size = m.cols * m.rows * elem_size;

    boost::serialization::binary_object data(m.data, data_size);
    ar & data;
}

boost::serialization::binary_object data can handle serialization of binary data with a much better performance.

Upvotes: 3

nils
nils

Reputation: 2524

Since you already use Boost, you might as well look into the module serialization. Below is an example that converts cv::Mat to std::string:

#include <opencv2/core.hpp>

#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>

#include <cassert>
#include <string>
#include <vector>

BOOST_SERIALIZATION_SPLIT_FREE( cv::Mat )

namespace boost {
namespace serialization {

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

    ar & m.cols;
    ar & m.rows;
    ar & elemSize;
    ar & elemType;

    const size_t dataSize = m.cols * m.rows * m.elemSize();
    for ( size_t i = 0; i < dataSize; ++i )
        ar & m.data[ i ];
}


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, static_cast< int >( elemType ) );
    const size_t dataSize = m.cols * m.rows * elemSize;
    for (size_t i = 0; i < dataSize; ++i)
        ar & m.data[ i ];
}

} // namespace serialization
} // namespace boost

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

    return oss.str();
}

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( int argc, char ** argv )
{
    cv::Mat eye = cv::Mat::eye( 3, 3, CV_32FC1 );

    std::string serialized = save( eye );
    std::cout << "serialized   = " << serialized << std::endl;

    cv::Mat deserialized;
    load( deserialized, serialized.c_str() );
    std::cout << "deserialized = \n\n" << deserialized << std::endl;

    return 0;
}

Upvotes: 2

Related Questions