将blob图像转换为Mat图像

Convert blob images to Mat images

本文关键字:图像 Mat 转换 blob      更新时间:2023-10-16

我的人脸识别项目使用mysql数据库。在这里,我将图像存储到表中,图像存储为blob图像。当选择回这些图像时,我需要使用文件指针将这些blob图像写入单独的图像文件,它将存储到程序所在的文件夹中。为了使用这些图像,我需要从文件夹中再次读取。然后只有我可以将这些图像用于另一个功能。但我需要直接从数据库中使用这些blob图像(当我们从数据库中选择它时,我需要将它传递给另一个函数)。这样我就可以减少从文件夹中再次读取的操作。我该如何转换bl。所以,为了将DB图像直接传递给另一个函数,我认为我们需要转换它的类型或其他什么。

void SelectImage()
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
char temp[900];
char filename[50];
unsigned long *lengths;
FILE *fp;
my_ulonglong    numRows;
unsigned int    numFields;
int mysqlStatus = 0;
MYSQL_RES *mysqlResult = NULL;
conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "root", "athira@iot", "Athira", 0, NULL, 0);
int state=mysql_query(conn, "SELECT * FROM ima1");
mysqlResult=mysql_store_result(conn);
if(mysqlResult)
{
    numRows=mysql_num_rows(mysqlResult);
}
cout<<"rows:"<<numRows<<endl;   
for(int i=1;i<=numRows;i++){
    sprintf(temp,"SELECT data FROM ima1 WHERE id=%d",i);
    sprintf(filename,"Gen_Image%d.jpeg",i);
    fp = fopen(filename, "wb");// open a file for writing. 
    cout<<temp<<" to "<<filename<<endl;
    mysql_query(conn, temp);//select an image with id
    result = mysql_store_result(conn);
    row = mysql_fetch_row(result);//row contains row data
    lengths = mysql_fetch_lengths(result);//this is the length of th image
    fwrite(row[0], lengths[0], 1, fp);//writing image to a file
    cout<<"selected..."<<endl;
    img.create(100,100,CV_16UC1);
    memcpy(img.data,row.data,lengths);
    mysql_free_result(result);
    fclose(fp);
}
  mysql_close(conn);
  }
I tried to convert it to Mat type but it's showing error..  
error is this,
 error: request for member ‘data’ in ‘row’, which is of non-class type ‘MYSQL_ROW {aka char**}’

如果您的图像是使用某种图像格式(JPEG、PNG、BMP…)存储的,而不是作为原始像素数据存储的,那么您应该查看OpenCV中的imdecode函数。

如果您的数据存储为原始像素,那么您必须确保BLOB数据的内存布局可以使用OpenCV用于布局矩阵的步长功能来表示。