store exact cv::Mat image in sqlite3 database

房东的猫 提交于 2020-01-23 02:32:26

问题


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:


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.



来源:https://stackoverflow.com/questions/15198131/store-exact-cvmat-image-in-sqlite3-database

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