python调用c++代码例子,实现图像,class通信
例子运行环境:ubuntu14.04,cmake2.8,boost,opencv
c++ 代码:main.cpp
1 #include <stdio.h> 2 #include <string> 3 #include <opencv2/opencv.hpp> 4 5 #include "boost/python.hpp" 6 namespace bp = boost::python; 7 8 typedef struct BBox { 9 int x; 10 float confidence; 11 } _BBox; 12 13 class communication{ 14 public: 15 communication(bp::str str) { 16 imagename = std::string(((const char *) bp::extract<const char *>(str))); 17 std::cout << "communication init." << std::endl; 18 std::cout << "image name is " << imagename << std::endl; 19 } 20 21 bp::list test(int rows,int cols,bp::str img_data) { 22 unsigned char *data = (unsigned char *) ((const char *) bp::extract<const char *>(img_data)); 23 cv::Mat m= cv::Mat(rows, cols, CV_8UC3,data); 24 cv::cvtColor(m, m, cv::COLOR_BGR2GRAY); 25 cv::imshow(imagename, m); 26 cv::waitKey(1); 27 28 bp::list img_list = bp::list(); 29 for( size_t nrow = 0; nrow < m.rows; nrow++) { 30 unsigned char* data1 = m.ptr<unsigned char>(nrow); 31 for(size_t ncol = 0; ncol < m.cols; ncol++) 32 img_list.append<unsigned char>(data1[ncol]); 33 } 34 return img_list; 35 } 36 bp::list ret_box_list() { 37 bp::list ret_list = bp::list(); 38 _BBox a,b; 39 a.x = 1; 40 a.confidence = 1.3; 41 b.x = 2; 42 b.confidence = 2.7; 43 ret_list.append<BBox>(a); 44 ret_list.append<BBox>(b); 45 return ret_list; 46 } 47 48 49 static void set_something(int id) { 50 std::cout << "set_something done." << std::endl; 51 } 52 53 std::string imagename; 54 }; 55 56 BOOST_PYTHON_MODULE (interact) { 57 bp::class_<communication>("communication", bp::init<bp::str>()) 58 .def("test", &communication::test) 59 .def("ret_box_list", &communication::ret_box_list) 60 .def("set_something", &communication::set_something) 61 .staticmethod("set_something"); 62 63 bp::class_<BBox>("BBox") 64 .def_readonly("x", &BBox::x) 65 .def_readonly("confidence", &BBox::confidence); 66 }
cmake 代码:CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(interact_cpp_py) #aux_source_directory (${PROJECT_SOURCE_DIR} SRC_LIST) #add_executable (${PROJECT_NAME} ${SRC_LIST}) ## Compiler settings. generate .so needed by python #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--export-dynamic -Wall -Wno-sign-compare -fPIC") #BOOST FIND_PACKAGE(Boost COMPONENTS python) FIND_PACKAGE(PythonInterp) FIND_PACKAGE(PythonLibs) include_directories(SYSTEM ${Boost_INCLUDE_DIR}) include_directories(SYSTEM ${PYTHON_INCLUDE_DIR}) link_libraries(${BOOST_LIBRARIES} ${PYTHON_LIBRARIES}) #target_link_libraries(${PROJECT_NAME} ${LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) ##OPENCV find_package( OpenCV REQUIRED ) #target_link_libraries( ${PROJECT_NAME} # ${OpenCV_LIBS} #) ## build .so set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) add_library(interact SHARED main.cpp) set_target_properties(interact PROPERTIES PREFIX "") target_link_libraries(interact ${LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${OpenCV_LIBS})
python代用c++代码:test.py
1 import numpy as np 2 import interact, cv2 3 4 class call_cpp(object): 5 def __init__(self, name): 6 self.itcom = interact.communication(name) 7 8 def run(self, img) : 9 image_char = img.astype(np.uint8).tostring() 10 ret_list = self.itcom.test(img.shape[0], img.shape[1], image_char) 11 gray = np.zeros([img.shape[0], img.shape[1]], np.ubyte) 12 c = 0 13 for i in range(img.shape[0]): 14 for j in range(img.shape[1]): 15 gray[i][j] = ret_list[c] 16 c += 1 17 return gray 18 19 def run_class(self): 20 return self.itcom.ret_box_list() 21 22 if __name__ == '__main__': 23 image = cv2.imread('deng.jpg') 24 itr = call_cpp("itr") 25 a = itr.run_class() 26 print a[0].x, a[0].confidence, a[1].x, a[1].confidence 27 image1 = itr.run(image) 28 cv2.imshow("python", image1) 29 cv2.waitKey(0)