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

前端 未结 3 2022
别那么骄傲
别那么骄傲 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: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

提交回复
热议问题