本篇文章為大家展示了如何在c++中通過調用python傳輸圖片,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
如下所示:
#include <Python.h> #include <arrayobject.h> #include "opencv2/imgcodecs.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/videoio.hpp" #include <opencv2/highgui.hpp> #include <opencv2/video.hpp> #include "opencv2/video/background_segm.hpp" //using namespace std; int init_numpy() { import_array(); }
初始化:
Py_SetPythonHome(L"D:\\Users\\Lenovo\\Anaconda3\\envs\\python35"); Py_Initialize(); init_numpy(); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); pModule = NULL; pFunc = NULL; pModule =PyImport_ImportModule("demo"); pFunc =PyObject_GetAttrString(pModule, "load_model"); PyEval_CallObject(pFunc,NULL);
傳輸代碼:
cv::Mat img =cv::imread("d:\\1.jpg", CV_LOAD_IMAGE_COLOR); int m, n; n = img.cols *3; m = img.rows; unsigned char *data = (unsigned char*)malloc(sizeof(unsignedchar) * m * n); int p = 0; for (int i = 0; i < m;i++) { for (int j = 0; j < n; j++) { data[p]= img.at<unsignedchar>(i, j); p++; } } npy_intp Dims[2]= { m, n }; //給定維度信息 PyObject*PyArray = PyArray_SimpleNewFromData(2, Dims, NPY_UBYTE, data); PyObject*ArgArray = PyTuple_New(1); PyTuple_SetItem(ArgArray,0, PyArray); PyObject *pDict= nullptr; pDict =PyModule_GetDict(pModule); PyObject*pFuncFive = PyDict_GetItemString(pDict, "load_image"); //PyObject_CallObject(pFuncFive, ArgArray); PyObject*pReturn = PyObject_CallObject(pFuncFive, ArgArray); int result; PyArg_Parse(pReturn,"i", &result); CString strtemp; strtemp.Format(_T("%d"), result); MessageBox(strtemp);
Python部分:
importcv2 import numpyas np w=227 h=227 c=3 sess = None def arrayreset(array): # for i inrange(array.shape[1]/3): # pass a = array[:,0:len( array[0] -2 ):3] b = array[:, 1:len( array[0] - 2 ):3] c = array[:, 2:len( array[0] - 2 ):3] a = a[:, :, None] b = b[:, :, None] c = c[:, :, None] m = np.concatenate((a,b,c),axis=2) return m def load_model(): global sess sess = tf.Session() saver = tf.train.import_meta_graph( './model/model.ckpt.meta') saver.restore( sess, tf.train.latest_checkpoint('./model/') ) def load_image(image): img = arrayreset(image)
其實還可以用imencode來解決:本文尚未完善
Mat image = imread("d:\\11.jpeg", CV_LOAD_IMAGE_COLOR); IplImage iplimage = image; vector<uchar> buff;//buffer for coding vector<int> param = vector<int>(2); param[0] = CV_IMWRITE_JPEG_QUALITY; param[1] = 95;//default(95) 0-100 imencode(".jpg", image, buff, param); std::string str_encode(buff.begin(), buff.end());
上述內容就是如何在c++中通過調用python傳輸圖片,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯網站建設公司行業資訊頻道。
另外有需要云服務器可以了解下創新互聯建站newbst.com,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文名稱:如何在c++中通過調用python傳輸圖片-創新互聯
文章源于:http://newbst.com/article0/dggeoo.html
成都網站建設公司_創新互聯,為您提供Google、網站改版、網頁設計公司、搜索引擎優化、App設計、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯