2014-04-11 62 views
0

我人臉識別project.Here我存儲圖像到表和圖像存儲爲BLOB images.when選擇相應的圖片後,我需要寫這些斑點圖像使用文件指針分離圖像文件,並將其存儲到程序所在的文件夾中。爲了使用這些圖像,我需要從該文件夾中再次讀取它。然後,只能將該圖像用於其他功能。但是我需要直接從數據庫中使用這些blob圖像(當我們從數據庫中選擇它時,我需要將它傳遞給另一個函數)。這樣我就可以減少像從文件夾中再次讀取操作。如何轉換bl。因此,將數據庫圖像直接傳遞給另一個函數,我認爲我們需要將其轉換爲類型或其他。轉換BLOB圖像太我使用MySQL數據庫的圖像

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", "[email protected]", "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**}’ 
+1

附註:爲什麼你要對每個圖像行進行一個查詢?你的第一個'從ima1'查詢應該是你所需要的。 –

回答

0

如果您的圖片使用一些圖像格式(JPEG,PNG,BMP,...)而不是原始像素數據存儲,那麼你應該尋找在OpenCV中的imdecode功能。

如果您的數據存儲爲原始像素,那麼你必須確保你的BLOB數據的內存佈局可以用步/步幅功能的OpenCV用來佈局其矩陣來表示。

+0

可以請你給我看一個例子。 – user3228547

+0

什麼是例子?對於'imdecode',你只需將一個指針傳遞給緩衝區和一些標誌。對於原始像素的情況下,你將不得不看看'cv :: Mat'中的數據是如何佈局的。 –

+0

我給它這樣的:墊imgg = cvDecodeImageM(行,CV_LOAD_IMAGE_COLOR);再次它顯示錯誤:錯誤:無法轉換「MYSQL_ROW {又名字符**}」到「常量與CvMat *」的說法「1」到「與CvMat * cvDecodeImageM(const CvMat *,int)' – user3228547