资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在c++中通过调用python传输图片-创新互联

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

创新互联,为您提供成都网站建设网站制作公司、网站营销推广、网站开发设计,对服务火锅店设计等多个行业拥有丰富的网站建设及推广经验。创新互联网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!

如下所示:

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

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 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传输图片,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联网站建设公司行业资讯频道。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:如何在c++中通过调用python传输图片-创新互联
转载源于:http://www.cdkjz.cn/article/dggeoo.html
十二年 建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220