将OpenCV :: Mat作为BLOB存储到SQLite DB

时间:2018-11-28 02:16:21

标签: c++ sqlite opencv blob

是否可以将图像从opencv :: Mat存储到sqlite数据库(作为BLOB或其他类型)? 我可以存储其他变量,例如字符串,但不能存储cv :: Mat。 这是我的功能代码

void SQLiteWrapper::WriteDetectInfo(std::string markName, std::string dmgClass, cv::Mat in)
{
    char *zErrMsg = 0;
    std::ostringstream strQuery;

    strQuery << "INSERT INTO DetectedDamage(Markname, Class, Image)";
    strQuery << "VALUES('" << markName;
    strQuery << "','" << dmgClass;
    strQuery << "'," << in;
    strQuery << ");";
    std::string query = strQuery.str();
    int rc = sqlite3_exec(DB3, query.c_str(), SQLiteWrapper::CallBack, (void*)CB_WRITE_DETECT_INFO, &zErrMsg);
}

1 个答案:

答案 0 :(得分:0)

要将图像存储为BLOB,您需要先将mat转换为char*。为此,您可以使用cv::imencode()。然后您可以将blob数据插入为:

int rc = 0;
char* buffer = new char[size]; // Your image data from `cv::imencode()`

sqlite3_stmt *strQuery = NULL;
sqlite3_prepare_v2(db,"INSERT INTO DetectedDamage(Markname, Class, Image) VALUES (NULL, NULL, ?)", -1, &strQuery, NULL);

sqlite3_bind_blob(strQuery, 1, buffer, size, SQLITE_STATIC);
if (rc != SQLITE_OK) {
    std::cerr << "bind failed!" << std::endl;
} else {
    rc = sqlite3_step(strQuery);
    if (rc != SQLITE_DONE)
        std::cerr << "execution failed!" << sqlite3_errmsg(db) << std::endl;
}