时间:2021-05-20
如下所示:
#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部分:
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传输图片实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
原理python没有办法直接和c++共享内存交互,需要间接调用c++打包好的库来实现流程C++共享内存打包成库python调用C++库往共享内存存图像数据C++
一、问题Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结。二、Python调用C/C++1、Python调用C动态链接库Py
AndroidJNIc/c++调用java的实例近期通过研究SDL源码得出AndroidJNIc/c++调用Java无需新建虚拟机,这样省去很多步骤,这样调用A
相比C++而言,Python适合做原型。本系列的文章介绍如何在Python中用OpenCV图形库,以及与C++调用相应OpenCV函数的不同之处。这篇文章介绍在
本文实例为大家分享了python或C++读取指定文件夹下的所有图片,供大家参考,具体内容如下1.python读取指定文件夹下的所有图片路径和图片文件名impor