日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

linux下opencv读取图片并存储到mysql数据库中

發布時間:2025/3/12 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下opencv读取图片并存储到mysql数据库中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

弄了好久的一段代碼,ide用的eclipse,數據庫連接不會的自己去找吧,網上蠻多的;功能雖然不是很強,但還是蠻有借鑒意義的。

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql.h> #include <iostream>#include <string>#include <opencv/cv.h> #include <opencv/highgui.h> using namespace std;string IntToStr(int num) {stringstream ss;ss.clear();ss<<num;return ss.str(); }int main() {//定義數據庫連接信息const char user[] = "root"; //usernameconst char pswd[] = "phancie"; //passwordconst char host[] = "localhost"; //or"127.0.0.1"const char table[] = "phancie_vision"; //databaseunsigned int port = 3306; //server portMYSQL myCont;MYSQL_RES *result;//MYSQL_ROW sql_row;//MYSQL_FIELD *fd;//char column[32][32];mysql_init(&myCont);if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0)){cout<<"connect succeed!"<<endl;//設置編碼格式,否則在cmd下無法顯示中文mysql_query(&myCont, "SET NAMES GBK");int image_id = 1;char filename[256];//IplImage *image=0;sprintf(filename , "/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/%d.jpg" , image_id );IplImage* src = cvLoadImage(filename);cout<<"\n開始存儲圖片信息 \n";while(src){int orgExists = 1;int orgWidth = src->width;int orgHeight = src->height;int orgDepth = src->depth;int orgWidthStep = src->widthStep;int thumbExists = 0;string path = "/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/"+IntToStr(image_id)+".jpg";const char* orgRelativaPath = path.c_str();//定義數據庫操作語句//將圖片信息插入數據庫中string orgsql = "insert into phvImages(orgExists,orgWidth,orgHeigth,orgDepth,orgRelativePath,orgWidthStep,thumbExists)";orgsql += " values("+IntToStr(orgExists)+","+IntToStr(orgWidth)+","+IntToStr(orgHeight)+","+IntToStr(orgDepth)+",'"+orgRelativaPath+"',"+IntToStr(orgWidthStep)+","+IntToStr(thumbExists)+")";//將std::string類型轉換為const char*const char *sql_insert = orgsql.c_str();cout<<sql_insert<<endl;int res = mysql_query(&myCont,sql_insert);if(!res){printf("插入成功,受影響行數:%lu\n",(ulong)mysql_affected_rows(&myCont));}else{cout<<"query sql failed!"<<endl;}image_id++;sprintf(filename , "/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/%d.jpg" , image_id );src= cvLoadImage(filename);}}else{cout<<"connect failed!"<<endl;}if(result!=NULL) mysql_free_result(result);//釋放結果資源mysql_close(&myCont);//斷開連接return 0;}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的linux下opencv读取图片并存储到mysql数据库中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。