c++ 调用python传输图片实例

所属分类: 软件编程 / C 语言 阅读数: 90
收藏 0 赞 0 分享

如下所示:

#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)

其实还可以用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传输图片实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多