官网下载boost:https://www.boost.org/
- 打开VS2015 Developer Command Promp(VS2015开发人员命令提示)t命令行
- 定位到Boost解压后的文件夹:cd /d J:\Library\boost\boost_1_75_0
- 在命令行执行booststrap.bat,将在文件夹下生成 b2.exe和project-config.jam等文件
- 新建user-config.jam文件
# Configure specific Python version.---user for boost.python---2018.10.12---
# using python : 3.6.1
: D:/Anaconda3/envs/lanenet/python.exe
: D:/Anaconda3/envs/lanenet/include #directory that contains pyconfig.h
: D:/Anaconda3/envs/lanenet/libs #directory that contains python361.lib
: <toolset>msvc ;
- b2 toolset=msvc-14.0 --with-python threading=multi link=static address-model=64 variant=debug --user-config=user-config.jam
- b2 toolset=msvc-14.0 --with-python threading=multi link=static address-model=64 variant=release --user-config=user-config.jam
- 得到如下(其中将xxx-gd-xxx的复制一份并把前面lib去掉)
- vs项目配置参数
- boost_debug.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>J:\Library\boost\boost_1_75_0;D:\Anaconda3\envs\lanenet\include;$(IncludePath)</IncludePath>
<LibraryPath>J:\Library\boost\boost_1_75_0\stage\lib;D:\Anaconda3\envs\lanenet\libs;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>python3.lib;python36.lib;libboost_numpy36-vc140-mt-gd-x64-1_75.lib;libboost_python36-vc140-mt-gd-x64-1_75.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ClCompile />
<ClCompile />
</ItemDefinitionGroup>
<ItemGroup />
</Project>
- boost_release.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>J:\Library\boost\boost_1_75_0;D:\Anaconda3\envs\lanenet\include;$(IncludePath)</IncludePath>
<LibraryPath>J:\Library\boost\boost_1_75_0\stage\lib;D:\Anaconda3\envs\lanenet\libs;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>python3.lib;python36.lib;libboost_numpy36-vc140-mt-x64-1_75.lib;libboost_python36-vc140-mt-x64-1_75.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ClCompile />
<ClCompile />
<ClCompile>
<PreprocessorDefinitions>BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
-
python调用C++
//#include<iostream> //#include<stdio.h> // //#include<boost\version.hpp> //包含boost头文件 //#include<boost\config.hpp> // //int main() //{ // using namespace std; // cout << BOOST_VERSION << endl; // cout << BOOST_LIB_VERSION << endl; // cout << BOOST_PLATFORM << endl; // cout << BOOST_COMPILER << endl; // cout << BOOST_STDLIB << endl; // // system("pause"); // return 0; //} //#define BOOST_PYTHON_STATIC_LIB #include <boost/python.hpp> #include <iostream> #include <stdio.h> using namespace boost::python; char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(boostpy03) // Python 模块开始 { using namespace boost::python; // 打开命名空间 def("greet", greet); }
生成boostpy03.dll
-
修改为boostpy03.pyd
-
进入python环境,运行python
import boostpy03 print(boostpy03.greet())
输出:hello, world
-
成功
-
C++调用python
借用这边博客https://blog.csdn.net/dielaoji0733/article/details/90678016
- cpp文件
#include <iostream>
#include <python3.5/Python.h>
#include <numpy/arrayobject.h>
#include <opencv2/opencv.hpp>
int main(int argc, char* argv[])
{
//初始化python
Py_Initialize();
import_array(); //numpy初始化,会报迷之警告,大家忽略它吧
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')"); //将当前路径加入,否者会出现找不到模块,这两句话非常重要
PyObject *py_Module = PyImport_ImportModule("TestModule"); //引入模块
PyObject *py_main = PyObject_GetAttrString(py_Module,"main"); //获取main函数
npy_intp Dims[3] = {480, 640, 3}; //给定维度信息
cv::VideoCapture cap(1);
cv::Mat frame;
while(1)
{
cap>>frame;
/*生成包含这个多维数组的PyObject对象,使用PyArray_SimpleNewFromData函数,
* 第一个参数2表示维度,第二个为维度数组Dims,第三个参数指出数组的类型,第四个参数为数组*/
/*关键:直接将矩阵的数据指针传入即可 numpy操作即可转换程对Mat操作。经测试任意维度的任意类型数据均适用*/
PyObject *PyArray = PyArray_SimpleNewFromData(3, Dims, NPY_UBYTE, frame.data);
PyObject *ArgArray = PyTuple_New(1);
PyTuple_SetItem(ArgArray, 0, PyArray); //同样定义大小与Python函数参数个数一致的PyTuple对象
PyArrayObject *py_return = (PyArrayObject *)PyEval_CallObject(py_main, ArgArray);
/*关键: 反馈回来的数据直接将矩阵数据指针指向ArrayObject的数据即可*/
cv::Mat img(cv::Size(640,480),CV_8UC3);
img.data = (u_char*)py_return->data; //直接将图像的数据指针指向numpy返回的数据
cv::imshow("c++",img);
cv::waitKey(1);
}
Py_Finalize(); //释放python
return 0;
}
- py文件
import cv2
def main(img):
cv2.imshow('py', img)
cv2.waitKey(1)
return img
if __name__ == "__main__":
cap = cv2.VideoCapture(1)
while True:
_, img = cap.read()
main(img)
出现的问题 PyObject *py_Module = PyImport_ImportModule("TestModule"); 返回值为NULL
指向你这个python的环境,查询sys.path所有内容,把相关的地址都放进去就可以了