微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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




















`

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐