c++中怎么通过调用python传输图片

这篇文章给大家介绍c++中怎么通过调用python传输图片,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联公司专注于企业成都营销网站建设、网站重做改版、枝江网站定制设计、自适应品牌网站建设、H5开发商城开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为枝江等各大城市提供网站开发制作服务。

如下所示:

#include #include #include "opencv2/imgcodecs.hpp"#include "opencv2/imgproc.hpp"#include "opencv2/videoio.hpp"#include #include #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(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 buff;//buffer for coding vector param = vector(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传输图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


新闻名称:c++中怎么通过调用python传输图片
URL地址:http://hbruida.cn/article/gdhhco.html