How opencv 4.x API is different from previous version?

前端 未结 3 2031
别那么骄傲
别那么骄傲 2021-02-07 06:08

I noted that opencv 4 is released and one difference is that API changed to be c++11 compliant.

What this really means?

How should I change my codes to be compat

相关标签:
3条回答
  • 2021-02-07 06:32

    I think the most vital impact is you need to use a c++11 compiler.

    Also, it may not change the interface but allow them to make use of the updated Language changes, such as smart pointers, etc.

    0 讨论(0)
  • 2021-02-07 06:40

    I think the most different is, the OpenCV 4.0 use more C++11 features. Now cv::String == std::string and cv::Ptr is a thin wrapper on top of std::shared_ptr.

    The Opencv 4.0 remove folder include/opencv and only keep include/opencv2. A lot of C API from OpenCV 1.x has been removed. The affected modules are objdetect, photo, video, videoio, imgcodecs, calib3d. The old macro defination or unnamed enum is not suggested, use named enum insted.

    //! include/opencv2/imgcodes.hpp
    namespace cv
    {
    
    //! @addtogroup imgcodecs
    //! @{
    
    //! Imread flags
    enum ImreadModes {
           IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
           IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
           IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
           IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
           IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
           IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
           IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
           IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
           IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
           IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
           IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
           IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
           IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
         };
    
        // ...
    }
    

    Such as, when read image, it should be something like this:

    cv::Mat img = cv::imread("test.png", cv::IMREAD_COLOR);
    

    Except the new featues, the most C++ APIs keep the same. While the biggest different I found is cv2.findContours (in Python OpenCV):

    In OpenCV 3.4:

    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
    

    In OpenCV 4.0:

    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
    

    An alternative to work with 2.x 、3.x、4.x is:

    cnts, hiers = cv2.findContours(...)[-2:]
    

    Some links:

    1. OpenCV Release
    2. OpenCV ChangeLog
    3. OpenCV Introduction
    4. OpenCV Documentation
    5. How to use `cv2.findContours` in different OpenCV versions?
    6. OpenCV on Stackoverflow

    0 讨论(0)
  • 2021-02-07 06:50

    According to OpenCV 4.0.0, you don't have to make any significant modifications (most likely any at all) to your source codes, unless you are using some C API which has been removed.

    As already stated

    OpenCV is now C++11 library and requires C++11-compliant compiler

    To use c++11, clang version 3.3 and higher is required with flag -std=c++11. Same for g++ 4.3 and higher.

    It allows them to use std::string instead of cv::String, and other c++11 features. But don't worry, cv::String will still work, but is now allias for std::string. Similliar for smart pointers, etc.

    0 讨论(0)
提交回复
热议问题