c++ 調(diào)用python傳輸圖片實(shí)例
如下所示:
#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)
其實(shí)還可以用imencode來(lái)解決:本文尚未完善
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++ 調(diào)用python傳輸圖片實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言多線程服務(wù)器的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了C語(yǔ)言多線程服務(wù)器的實(shí)現(xiàn)實(shí)例,文章用實(shí)例講解的很清楚,有對(duì)這方面不太懂的同學(xué)可以參考下2021-02-02
vscode 安裝go第三方擴(kuò)展包填坑記錄的詳細(xì)教程
這篇文章主要介紹了vscode 安裝go第三方擴(kuò)展包填坑記錄,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
C++入門教程之內(nèi)聯(lián)函數(shù)與extern?"C"詳解
C++中的內(nèi)聯(lián)函數(shù)與靜態(tài)函數(shù)靜態(tài)函數(shù)靜態(tài)函數(shù)的定義靜態(tài)函數(shù)又稱為內(nèi)部函數(shù),下面這篇文章主要給大家介紹了關(guān)于C++入門教程之內(nèi)聯(lián)函數(shù)與extern?"C"的相關(guān)資料,需要的朋友可以參考下2023-01-01
C++實(shí)現(xiàn)LeetCode(200.島嶼的數(shù)量)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(200.島嶼的數(shù)量),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言中壓縮字符串的簡(jiǎn)單算法小結(jié)
這篇文章主要介紹了C語(yǔ)言中可用于實(shí)現(xiàn)字符串壓縮的簡(jiǎn)單算法小結(jié),列舉了包括哈夫曼算法等三個(gè)核心的程序?qū)崿F(xiàn)算法,需要的朋友可以參考下2016-03-03
C語(yǔ)言中你不知道的隱式類型轉(zhuǎn)換規(guī)則詳解
在C語(yǔ)言中,類型轉(zhuǎn)換的方式一般可分為隱式類型轉(zhuǎn)換和顯示類型轉(zhuǎn)換(也稱為強(qiáng)制類型轉(zhuǎn)換),其中隱式類型轉(zhuǎn)換由編譯器自動(dòng)進(jìn)行,不需要程序員干預(yù),本文給大家詳細(xì)介紹了C語(yǔ)言中隱式類型轉(zhuǎn)換規(guī)則,需要的朋友可以參考下2024-01-01
C++模擬實(shí)現(xiàn)STL容器vector的示例代碼
這篇文章主要為大家詳細(xì)介紹了C++如何模擬實(shí)現(xiàn)STL容器vector的相關(guān)資料,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定幫助,需要的可以參考一下2022-11-11

