国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

c++ 調用python傳輸圖片實例

2020-01-26 13:22:21
字體:
來源:轉載
供稿:網友

如下所示:

#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"http://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部分:

importcv2import numpyas npw=227h=227c=3sess = Nonedef 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 mdef 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傳輸圖片實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九龙县| 镇赉县| 略阳县| 莫力| 大余县| 京山县| 浠水县| 咸阳市| 永丰县| 夏津县| 晋城| 和龙市| 洛宁县| 湘乡市| 阿坝县| 黑水县| 丹阳市| 邯郸县| 饶阳县| 神木县| 临安市| 静乐县| 略阳县| 长治县| 金昌市| 兴和县| 淳安县| 西充县| 天津市| 肥西县| 尼玛县| 丹阳市| 宜城市| 宜丰县| 清流县| 额济纳旗| 马龙县| 全椒县| 邵武市| 天祝| 明溪县|