Ask Your Question
0

How to export cv::Mat to mysql?

asked 2017-01-04 01:13:41 -0600

Is anyone know how to export data in cv::Mat to mysql(A blob type variable is used to store images)?

edit retag flag offensive close merge delete

Comments

you don't even say, what language you're using...

berak gravatar imageberak ( 2017-01-05 19:56:20 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2017-01-05 13:55:51 -0600

pklab gravatar image

A MySQL Connector/C++ is needed and is out of scope here... but... because the setBlob() MySql API works with std::istream (see here) my answer shows how to serialize a cv::Mat into a std::istream.

Helper struct for later use

struct matInfo
{
    matInfo(int _cols = -1, int _rows = -1, int _type = CV_8U) :
        cols(_cols), rows(_rows), type(_type){};
    int cols, rows, type;
    size_t Size() {return CV_ELEM_SIZE(type) * rows * cols;}
    size_t RowSize(){ return CV_ELEM_SIZE(type) * cols; }
};

Write cv::Mat to std::ostream

bool MatSerialize(ostream &out, const Mat& mat)
{
    matInfo mi( mat.cols, mat.rows, mat.type() );
    std::ostream::sentry s(out);
    if (!(s && out.good()))
        return false;
    out.write((char*)(&mi), sizeof(matInfo));
    if (mat.isContinuous())
        out.write((char*)mat.data, mi.Size());
    else
    {
        size_t rowsz = mi.RowSize();
        for (int r = 0; r < mi.rows; ++r)
            out.write((char*)mat.ptr<char>(r), rowsz);
    }
    out.flush();
    return out.good();
}
/** \brief Useful stream operator. stringstream is used to avoid conflict with cv::operator<< */
stringstream& operator<< (stringstream &out, const cv::Mat& mat)
{
    MatSerialize(out, mat);
    return out;
}

Your main

...
mat= imread("Lena.jpg");
//serialize cv::Mat. stringstream is need if stream operator is used
std::stringstream ss;
ss << mat;// or call MatSerialize(ss, mat);

//map an input stream over our buffer
std::istream is(ss.rdbuf());

//call the MySql API on a valid statement like below
stmt->setBlob(pos, &is);

to get back a cv::Mat from a stream the de-serialization is needed

/** \brief Decode a cv::mat from istream*/
bool MatDeserialize(std::istream& in, cv::Mat& mat)
{
    std::istream::sentry s(in);
    if (!(s && in.good()))
    {
        mat = Mat();
        return false;
    }
    matInfo mi;
    in.read((char*)(&mi), sizeof(matInfo));
    if (mi.Size() < 0)
    {
        mat = Mat();
        return false;
    }
    mat = Mat(mi.rows, mi.cols, mi.type);
    in.read((char*)mat.data, mi.Size());
    return in.good();
}
/** \brief Useful stream operator*/
istream& operator>>(std::istream& in, cv::Mat& mat)
{
    MatDeserialize(in, mat);
    return in;
}

and use it in your main as below

// deserialize (get back) the image
Mat mat2;
is >> mat2; //or call MatDeserialize(is,mat2)

it should works with all image format/pixel type

edit flag offensive delete link more
1

answered 2017-01-04 02:08:53 -0600

LBerger gravatar image

there is no interface to mysql in opencv. You have to write it yourself

edit flag offensive delete link more

Comments

StevenPuttemans gravatar imageStevenPuttemans ( 2017-01-05 03:18:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-04 01:13:41 -0600

Seen: 1,751 times

Last updated: Jan 05 '17