python调用c++代码(pybind)

下载pybind

pip install pybind11
#git clone git@github.com:pybind/pybind11.git
#cd pybind11
#mkdir build
#cd build
#cmake ..
#cmake --build . --config Release --target check

创建cpp文件

//以main.cpp为例
#include<iostream>
#include<pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include<opencv2/opencv.hpp>

int add(int i, int j) {
    return i + j;
}

//C++ Mat ->numpy
pybind11::array_t<unsigned char> cv_mat_uint8_1c_to_numpy(cv::Mat& input) {
    pybind11::array_t<unsigned char> dst = pybind11::array_t<unsigned char>({ input.rows,input.cols }, input.data);
    return dst;
}
//C++ Mat ->numpy
pybind11::array_t<unsigned char> cv_mat_uint8_3c_to_numpy(cv::Mat& input) {
    pybind11::array_t<unsigned char> dst = pybind11::array_t<unsigned char>({ input.rows,input.cols,3}, input.data);
    return dst;
}


std::tuple<int,pybind11::array_t<uint8_t>> ReadImage()
{
    //读取并返回一张图片
    pybind11::array_t<uint8_t> output_array;
    cv::Mat img;
    img = cv::imread("/home/HwHiAiUser/zyx/bus.jpg");
    if(img.empty())
        return std::tuple<int,pybind11::array_t<uint8_t>>(0,output_array);
    output_array = cv_mat_uint8_3c_to_numpy(img);
    return std::tuple<int,pybind11::array_t<uint8_t>>(1,output_array);
}

PYBIND11_MODULE(pycv, m) {
    m.doc() = "pybind11 pycv plugin"; // optional module docstring
    m.def("add", &add, "A function which adds two numbers");
    m.def("GetResImage", &ReadImage,"读取一张图片");
}

创建CMakeLists.txt文件

cmake_minimum_required(VERSION 3.4)
project(example)


# ---设置pybind11---
# set(PYTHON_EXECUTABLE "/usr/bin/python3")
# set(PYTHON_INCLUDE_DIRECTORY "/usr/include/python3.8")
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

find_package(pybind11 REQUIRED PATHS /usr/local/lib/python3.8/dist-packages/pybind11/share/cmake/pybind11 NO_DEFAULT_PATH)
# find_package(pybind11 REQUIRED PATHS /home/sskj/.local/lib/python3.8/site-packages/pybind11/share/cmake/pybind11 NO_DEFAULT_PATH)

include_directories(${pybind11_INCLUDE_DIRS})
# add_subdirectory(/root/zzz/pybind11)   # 使用git克隆的pybind11源码路径

pybind11_add_module(pycv main.cpp)   # 添加cpp文件

# ---设置opencv库---
set(OpenCV_DIR /usr/local/lib/cmake/opencv4) # xxxx目录包含OpenCVConfig.cmake
find_package(OpenCV REQUIRED)	# 找到opencv库
include_directories(${OpenCV_INCLUDE_DIRS})

message("--------------${OpenCV_DIR}---------------------")
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
message("libs:--->${OpenCV_LIBRARIES}")
message("----------------opencv end----------------------")


# ------
target_link_libraries(pycv PUBLIC ${OpenCV_LIBRARIES})
# add_executable(${PROJECT_NAME} *.cpp)	# *.cpp指要编译的那些源文件
# target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})

编译

mkdir build
cd build
cmake ..
make -j4

# 编译后生成.so文件为调用库
# .
# ├── CMakeCache.txt
# ├── CMakeFiles
# ├── cmake_install.cmake
# ├── pycv.cpython-38-x86_64-linux-gnu.so
# ├── extern
# └── Makefile

python调用

import pycv
import cv2
print(pycv.add(1,2))
ret,img = pycv.GetResImage()
print(f'{ret},{img.shape}')
cv2.imshow("img",img)
cv2.waitKey(0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值