store exact cv::Mat image in sqlite3 database

2019-04-12 02:00发布

is there any way to store exact cv::Mat format data in sqlite3 using Qt.. as i will be using the same cv::Mat format in future..

i tried converting image to unsigned char* and them storing it.. but this didn't worked for me.. any other technique ??

1条回答
孤傲高冷的网名
2楼-- · 2019-04-12 02:21

You can serialize cv::Mat to QByteArray (see kde/libkface):

QByteArray mat2ByteArray(const cv::Mat &image)
{
    QByteArray byteArray;
    QDataStream stream( &byteArray, QIODevice::WriteOnly );
    stream << image.type();
    stream << image.rows;
    stream << image.cols;
    const size_t data_size = image.cols * image.rows * image.elemSize();
    QByteArray data = QByteArray::fromRawData( (const char*)image.ptr(), data_size );
    stream << data;
    return byteArray;
}

Then store to DB. To convert from QByteArray after reading from DB:

cv::Mat byteArray2Mat(const QByteArray & byteArray)
{
    QDataStream stream(byteArray);
    int matType, rows, cols;
    QByteArray data;
    stream >> matType;
    stream >> rows;
    stream >> cols;
    stream >> data;
    cv::Mat mat( rows, cols, matType, (void*)data.data() );
    return mat.clone();
}

It works for me.

查看更多
登录 后发表回答