Maintaining code compatibility between OpenCV 2 and OpenCV 3

前端 未结 3 785
不知归路
不知归路 2021-02-15 16:14

My library currently uses OpenCV 2. Now, I am trying to compile the library to use OpenCV 3. It seems that some header files were moved and some constants were renamed. For exam

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-15 16:50

    The OpenCV guys have put up a transition guide online : http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html

    In essence I think the following have changed:-

    • ml module-> The existence of StatModel in OpenCV 3 and that being the root for all classifiers.
    • features2d and xfeatures2d -> SIFT and a couple of others have moved from being in the core repository to the opencv_contrib repo
    • And many others I am not aware of

    As for code working with Opencv3 and opencv2, I usually introduce a prepocessor directive which holds a flag:-

    #define HAS_OPENCV3 1
    #ifdef HAS_OPENCV3
    #include  //Any OPENCV3 code
    #else
    #include  //Any Opencv2 code
    #endif
    

    Since the relative portions are eliminated before compilation, it will also compile with only the OpenCV2 or OpenCV3 libraries. But, introduces a lot of redundancy (which can be avoided by some clever coding).

提交回复
热议问题