【问题标题】:Store OpenCV::Mat to SQLite DB as BLOB将 OpenCV::Mat 作为 BLOB 存储到 SQLite DB
【发布时间】:2019-04-29 20:52:15
【问题描述】:

是否可以将图像从 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);
}

【问题讨论】:

  • 对要插入的列使用带有参数的准备好的语句,并将适当的值绑定到这些列。详情请见sqlite.org/c3ref/stmt.html

标签: c++ sqlite opencv blob


【解决方案1】:

要将图像存储为 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2015-11-19
    • 2011-08-30
    • 2015-04-30
    • 2014-04-07
    • 1970-01-01
    • 2014-04-11
    相关资源
    最近更新 更多