C++读取图片发送给python显示出来

C++读取图片发送给python显示出来,第1张

概述C++代码:#include<iostream>#include<stdlib.h>#include<python2.7/Python.h>#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<numpy/arrayobject.h>#include<opencv2/imgproc.hpp>#

C++代码 :

#include <iostream>#include <stdlib.h>#include <python2.7/Python.h>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <numpy/arrayobject.h>#include <opencv2/imgproc.hpp>#include <memory>using namespace std;using namespace cv;int main(){    Py_Initialize();    std::cout << "importing Deeplab V3 ..." << std::endl;    PyRun_SimpleString("import sys,os");    PyRun_SimpleString("sys.path.append('../python/')");    PyObject *py_module =NulL,*pFunc=NulL;    // 导入deeplab.py  module    // 2021.03.05 发现 matplotlib.pyplot 导入会出错    py_module = Pyimport_importModule("hello");    if (!py_module)    {        cout << "Python get module Failed." << endl;        return 0;    }    else        cout << "Python get module succeed!" << endl;    //   C++ 代码 读取图片传送给 python  Mat img = imread("./test.png",CV_LOAD_IMAGE_color);    int m, n;    n = img.cols *3;    m = img.rows;    unsigned char *data = new unsigned char[m*n];    int p = 0;    for (int i = 0; i < m; ++i)    {        for (int j = 0; j < n ; ++j)        {            data[p] = img.at<unsigned char>(i,j);            p++;        }    }    import_array();                                //必须有传数组时需要    pFunc = PyObject_GetAttrString(py_module,"forward");    if(pFunc == nullptr)    {        cout<<"Error: python pFunc_forward is null!"<<endl;        return -1;    }    npy_intp Dims[3] = {img.rows,img.cols,3};    PyObject *pyarray = PyArray_SimpleNewFromData(3,Dims,NPY_UBYTE,(voID*)data);    PyObject *argarray = PyTuple_New(1);    PyTuple_SetItem(argarray,0,pyarray);    PyObject *pRet = PyObject_CallObject(pFunc,argarray);    if(pRet == nullptr)    {        cout<<"Error: python pFunc_forward pRet is null!"<<endl;        return -1;    }    Py_Finalize();    return 0;}`**python  部分的代码:**import sysimport numpy as npfrom PIL import Imagedef forward(image):    global g_model    c=image[:,:,0]    b=image[:,:,1]    a=image[:,:,2]    a = np.expand_dims(a,axis=2)    b = np.expand_dims(b,axis=2)    c = np.expand_dims(c,axis=2)    image = np.concatenate((a,b,c),axis=2)    im = Image.fromarray(image)    im.save("your_file.png")CmakeList.txt   cmake_minimum_required(VERSION 2.8)set(OpenCV_DIR "/home/miao/otherpackage/opencv3.2/share/OpenCV" )project(seg_on_deeplab)set(CMAKE_CXX_STANDARD 14)find_package(OpenCV required)INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_Dirs} /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/)set(PYTHON_INCLUDE_Dirs ${PYTHON_INCLUDE_Dirs} /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy)message(STATUS ${OpenCV_INCLUDE_Dirs})# 添加python 的头文件find_package(Pythonlibs 2.7 required)find_package(OpenCV required)include_directorIEs(/usr/include/python2.7)include_directorIEs(${PYTHON_INCLUDE_Dirs})# 链接python的 动态库link_directorIEs(/usr/lib/python2.7/config-x86_64-linux-gnu)add_executable(seg_on_deeplab main.cpp)# 链接 python 库target_link_librarIEs(seg_on_deeplab libpython2.7.so ${OpenCV_liBS})`
总结

以上是内存溢出为你收集整理的C++读取图片发送给python显示出来全部内容,希望文章能够帮你解决C++读取图片发送给python显示出来所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1188878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存