如何在c++中通过调用python传输图片

  介绍

本篇文章为大家展示了如何在c++中通过调用python传输图片,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<>强如下所示:

# include  & lt; Python.h>      # include  & lt; arrayobject.h>      ,      # include “opencv2/imgcodecs.hpp"      # include “opencv2/imgproc.hpp"      # include “opencv2/videoio.hpp"      # include  & lt; opencv2/highgui.hpp>      # include  & lt; opencv2/video.hpp>      # include “opencv2/视频/background_segm.hpp"//using  namespace 性传播疾病;      int  init_numpy (), {      ,import_array ();      }

<强>初始化:

, Py_SetPythonHome (L" D: \ \ \ \用户联想\ \ Anaconda3 \ \ env \ \ python35");      ,Py_Initialize ();      ,init_numpy ();      ,PyRun_SimpleString (“import  sys");      ,PyRun_SimpleString (“sys.path.append (& # 39;。/& # 39;)“);=,pModule 零;=,pFunc 零;=,pModule  PyImport_ImportModule (“demo");=,pFunc  PyObject_GetAttrString (pModule,“load_model");      ,PyEval_CallObject (pFunc, NULL);

<强>传输代码:

,简历:Mat  img =简历::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 小姐:=,0;,小姐:& lt;, m,我+ +)      ,{      for 才能;(int  j =, 0;, j  & lt;, n;, j + +)      {才能      ,,数据[p]=, img.at (i, j);      ,,p + +;      ,,}      ,}      ,npy_intp 降低[2]=,{,m, n },,//给定维度信息      ,PyObject * PyArray =, PyArray_SimpleNewFromData(2,,会变暗,NPY_UBYTE,,数据);      ,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 (ArgArray pFuncFive也);      ,int 结果;      ,PyArg_Parse (pReturn,“我,,,结果);      ,CString  strtemp;      ,strtemp.Format (_T (“% d"),结果);      之前,对话框(strtemp);

<强> Python部分:

importcv2   import  numpyas  np      w=227   h=227   c=3   时间=sess 没有   def  arrayreset(数组):   ,# for 小姐:inrange (array.shape [1]/3):   ,#通过=,,a  array [: 0: len(数组[0]大敌;2,):3)=,,b  array [:,, 1: len(数组[0]大敌;作用;2,):3)=,,c  array [:,, 2: len(数组[0]大敌;作用;2,):3)   ,a =, (:,,,,,)=,b  b (:,,,,,)=,c  c (:,,,,,)=,,m  np.concatenate ((a, b, c),轴=2)   return  m   def  load_model ():   global 税=,,sess  tf.Session ()=,,saver  tf.train.import_meta_graph(& # 39;。/模型/model.ckpt.meta& # 39;)   ,saver.restore(,税,tf.train.latest_checkpoint(& # 39;。/模型/& # 39;),)      def  load_image(图片):=,,img  arrayreset(图片)

其实还可以用imencode来解决:本文尚未完善

Mat  image =, imread (“d: \ \ 11. jpeg",, CV_LOAD_IMAGE_COLOR);         IplImage  IplImage =,形象;         vector (2);         参数[0],=,CV_IMWRITE_JPEG_QUALITY;         参数[1],=,95;//默认(95),0 - 100,      imencode (“.jpg",形象,迷,参数);   std:: string  str_encode (buff.begin (),, buff.end ());

上述内容就是如何在c++中通过调用python传输图片,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

如何在c++中通过调用python传输图片