什么是c。使用OpenCV的imde code将其读取BLOB SQLite和德$ C $二值图像的最佳方式? [英] What is the best way to read binary image from blob SQLite and decode it using OpenCV imdecode?

查看:243
本文介绍了什么是c。使用OpenCV的imde code将其读取BLOB SQLite和德$ C $二值图像的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储和C ++从SQLite数据库读取的图像文件,存储工作正常,但我失败了读取和转换字节OpenCV的CV ::使用imde code垫。这里是我的code:

I'm storing and reading image files from SQLite database in C++, the storing is working fine, but I have failed reading and converting the bytes to OpenCV cv::Mat using imdecode. Here is my code:

std::vector<cv::Mat> images;
std::vector<string> names;
std::vector<int> ids;

sqlite3 *db;
if (sqlite3_open("fr.db", &db) != SQLITE_OK)
{
    printf("Open database failed\n");
    return 0;
}

sqlite3_stmt *statement;
const char* sql = "SELECT * FROM Friends";
if (sqlite3_prepare_v2(db, sql, strlen(sql), &statement, 0) != SQLITE_OK)
{
    printf("Open database failed\n");
    return 0;
}

int result = 0;
while (true)
{
    result = sqlite3_step(statement);

    if (result == SQLITE_ROW)
    {
        int id = sqlite3_column_int(statement, 0);
        ids.push_back(id);

        int size = sqlite3_column_bytes(statement, 1);
        std::vector<byte> data((byte)sqlite3_column_blob(statement, 1), size);
        images.push_back(cv::imdecode(data, CV_LOAD_IMAGE_COLOR));

        char* name = (char*)sqlite3_column_text(statement, 2);
        names.push_back(name);
    }
    else
    {
        break;
    }
}

问题是,imde code不建CV ::垫,但它返回空单..
先谢谢了。

The problem is that the imdecode does not build the cv::Mat but it returns empty one .. Thanks in advance.

推荐答案

现在的问题是在这条线:

The problem is in this line:

std::vector<byte> data((byte)sqlite3_column_blob(statement, 1), size);

在这里你是不是构建矢量正常。

您应该使用的范围构造的:

template <class InputIterator>
     vector (InputIterator first, InputIterator last,
             const allocator_type& alloc = allocator_type()); 

和您的code会那么:

and your code will be then:

// Get the size of the vector
int size = sqlite3_column_bytes(statement, 1);

// Get the pointer to data
uchar* p = (uchar*)sqlite3_column_blob(statement,1);

// Initialize the vector with the data
vector<uchar> data(p, p+size);

这篇关于什么是c。使用OpenCV的imde code将其读取BLOB SQLite和德$ C $二值图像的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆