1. 程式人生 > >wrap opencv c++ for python

wrap opencv c++ for python

how opencv do ?

opencv_hdrs: 標頭檔案集合

set(opencv_hdrs
    "${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/core.hpp"
    "${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp"
    "${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc/imgproc.hpp"
    "${OPENCV_MODULE_opencv_video_LOCATION}
/include/opencv2/video/background_segm.hpp"
"${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp" "${OPENCV_MODULE_opencv_photo_LOCATION}/include/opencv2/photo/photo.hpp" "${OPENCV_MODULE_opencv_highgui_LOCATION}/include/opencv2/highgui/highgui.hpp" "${OPENCV_MODULE_opencv_ml_LOCATION}
/include/opencv2/ml/ml.hpp"
"${OPENCV_MODULE_opencv_features2d_LOCATION}/include/opencv2/features2d/features2d.hpp" "${OPENCV_MODULE_opencv_calib3d_LOCATION}/include/opencv2/calib3d/calib3d.hpp" "${OPENCV_MODULE_opencv_objdetect_LOCATION}/include/opencv2/objdetect/objdetect.hpp" "${OPENCV_MODULE_opencv_contrib_LOCATION}
/include/opencv2/contrib/contrib.hpp"
)

gen.py 標頭檔案分析,

add_custom_command(
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated0.i
   COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py" "${CMAKE_CURRENT_SOURCE_DIR}/src2"
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/api
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/defs
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py)

gen2.py 標頭檔案分析

add_custom_command(
   OUTPUT ${cv2_generated_hdrs}
   COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs}
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/hdr_parser.py
   DEPENDS ${opencv_hdrs})

library

add_library(${the_module} SHARED src2/cv2.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated0.i ${cv2_generated_hdrs} src2/cv2.cv.hpp)

cv2.cpp 為原始檔,generated0.i 為gen.py轉換src2目錄下的檔案得到,${cv2_generated_hdrs}為gen2.py轉換${opencv_hdrs}得到。opencv中採用的轉換方式是自己的一套,而且只有針對C++的部分特性,因此對於一些例外,需要採用手動生成python封裝。如下圖,在misc/python 中的檔案都是為了手動轉換這些例外。因此,opencv自帶的一套很難被重用。
這裡寫圖片描述