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

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 sys
import numpy as np
from PIL import Image

def 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})




















`
上一篇:Django的创建以及‘DIRS‘: [BASE_DIR / ‘templates‘]问题的解决


下一篇:Linux locate 命令