c++ 調(diào)用python傳輸圖片實(shí)例
如下所示:
#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)
其實(shí)還可以用imencode來(lái)解決:本文尚未完善
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++ 調(diào)用python傳輸圖片實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Qt使用QListWidget實(shí)現(xiàn)自定義Item
這篇文章主要為大家詳細(xì)介紹了Qt如何使用QListWidget實(shí)現(xiàn)自定義Item的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)
這篇文章主要介紹了C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)的相關(guān)資料,需要的朋友可以參考下2015-06-06C語(yǔ)言根據(jù)協(xié)議分割獲取字符串單元的實(shí)現(xiàn)代碼
今天小編就為大家分享一篇關(guān)于C語(yǔ)言根據(jù)協(xié)議分割獲取字符串單元的實(shí)現(xiàn)代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法示例
百馬百擔(dān)是道經(jīng)典的算法題,下面這篇文章主要給大家介紹了利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12C++如何實(shí)現(xiàn)BitMap數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了C++如何實(shí)現(xiàn)BitMap數(shù)據(jù)結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07