C++ 是一種編譯型(compiled)語言,設計重點是性能、效率和使用靈活性,偏向於系統編程、嵌入式、資源受限的軟件和系統。
Python是一種解釋型(interpreted)語言,同樣也支持不同的編程范式。Python 內置了常用數據結構(str, tuple, list, dict),簡潔的語法、豐富的內置庫(os,sys,urllib,...)和三方庫(numpy, tf, torch ...),功能強大。最為重要的是和能夠和多種服務(flask…)和tensorflow、pytorch等無縫聯合,從而方便將你的算法開放出去。
一方面,我們需要編譯型語言(C++)性能;一方面,也需要解釋型語言(Python)的靈活。這時,pybind11 可以用作 C++ 和 Python 之間溝通的橋梁。
Pybind11 是一個輕量級只包含頭文件的庫,用於 Python 和 C++ 之間接口轉換,可以為現有的 C++ 代碼創建 Python 接口綁定。Pybind11 通過 C++ 編譯時的自省來推斷類型信息,來最大程度地減少傳統拓展 Python 模塊時繁雜的樣板代碼, 已經實現了 STL 數據結構、智能指針、類、函數重載、實例方法等到Python的轉換,其中函數可以接收和返回自定義數據類型的值、指針或引用。
由於在Windows上和在Linux上使用會有較大不同,所以我這里將分為兩個部分來說明問題,本文為上篇,具體說明Windows+VS實現
1、vs的最簡單調用
新創建項目,做以下修改:


# include <iostream >
# include <pybind11 /pybind11.h >
namespace py = pybind11;
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example module";
// Add bindings here
m.def( "foo", []() {
return "Hello, World!";
});
}
PYBIND11_MODULE()
macro creates a function that will be called when an
import
statement is issued from within Python. The module name (
example
) is given as the first macro argument (it should not be in quotes). The second argument (
m
) defines a variable of type
py::module
which is the main interface for creating bindings. The method
module::def()
generates binding code that exposes the
add()
function to Python.


1.PYD是一種PYTHON動態模塊。
2.實質上還是dll文件,只是改了后綴為PYD。
這里特別需要注意,就是.pyd文件名和GOPyWarper這個函數名字一定要一樣,否則報
錯誤。
2、vs添加OpenCV的調用
配置中,需要添加OpeCV部分。分別是附加包含目錄和附加依賴項。


//
# include "pch.h"
# include <iostream >
# include <opencv2 /core.hpp >
# include <opencv2 /imgcodecs.hpp >
# include <opencv2 /imgproc.hpp >
# include <opencv2 /highgui.hpp >
# include <pybind11 /pybind11.h >
using namespace cv;
using namespace std;
namespace py = pybind11;
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example module";
// Add bindings here
m.def( "foo", [](string strPath) {
Mat src = imread(strPath);
Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);
imshow( "gray", gray);
waitKey( 0); //必須設置,否則卡死
return "Hello, OpenCV!";
});
}
var1 = GOPyWarper.test_rgb_to_gray(src)
cv2.imshow( 'gray',var1)




py::list out;



//輸出結果
py : :list out;
……
pybind11—HOG特征提取以及python接口封裝
pybind11—目標跟蹤demo(深度學習人臉檢測跟蹤)
pybind11—目標跟蹤demo(KCF,python version)
pybind11—opencv圖像處理(numpy數據交換)
pybind11—C++ STL
pybind11—函數,返回值,數據轉換
pybind11—類(繼承,重載,枚舉)
pybind11使用
pybind11—類,結構體
191123 使用 Pybind11 和 OpenCV 創建 Python 庫