opencv 4.x API与先前版本有何不同?

man*_*ans 6 c++ opencv c++11

我注意到opencv 4已发布,一个区别是API更改为与c ++ 11兼容。

这到底是什么意思?

如何更改我的代码以与此版本兼容?

koc*_*ica 7

根据OpenCV 4.0.0,您不必对源代码进行任何重大修改(很可能根本不需要修改),除非您使用的是某些已删除的 C API。

如前所述

OpenCV 现在是 C++11 库并且需要符合 C++11 的编译器

要使用c++11,需要带有标志的 clang 3.3 及更高版本-std=c++11。g++ 4.3 及更高版本相同。

它允许他们使用std::string代替cv::String, 和其他 c++11 功能。不过别担心,它cv::String仍然可以工作,但现在是std::string. 类似于智能指针等。


Kin*_*t 金 7

我认为最不同的是,OpenCV 4.0使用了更多的C ++ 11功能。现在cv::String == std::string,它cv::Ptr是顶部的薄包装std::shared_ptr

Opencv 4.0删除文件夹include/opencv,仅保留include/opencv2。OpenCV 1.x中的许多C API已被删除。受影响的模块是objdetect, photo, video, videoio, imgcodecs, calib3d。不建议使用旧的宏定义或未命名的枚举,请使用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.
     };

    // ...
}
Run Code Online (Sandbox Code Playgroud)

例如,当读取图像时,应该是这样的:

cv::Mat img = cv::imread("test.png", cv::IMREAD_COLOR);
Run Code Online (Sandbox Code Playgroud)

除了新功能之外,大多数C ++ API都保持不变。虽然我发现最大的不同是cv2.findContours(在中Python OpenCV):

在OpenCV 3.4中:

findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在OpenCV 4.0中:

findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

使用2.x?3.x?4.x的替代方法是:

cnts, hiers = cv2.findContours(...)[-2:]
Run Code Online (Sandbox Code Playgroud)

一些链接:

  1. OpenCV版本
  2. OpenCV变更日志
  3. OpenCV简介
  4. OpenCV文档
  5. 如何在不同的OpenCV版本中使用`cv2.findContours`?
  6. 在Stackoverflow上使用OpenCV

  • 也许他们只是使用历史写作?实际上我认为 `enum class` 或 `enum struct` 可能更好,因为它们是有范围的。但是由于代码库如此之大,因此存在许多使用无作用域的代码。如果将 `enum` 更改为 `enum class`,则使用 `cv::IMREAD_COLOR` 的现有代码将编译失败,除非修改为 `cv::ImreadModes::IMREAD_COLOR`。并且在将 `C++ OpenCV API` 包装到 `Python|Java|C#` 中时,仍然存在许多与 `enum` 更改相关的错误。所以他们只是保持?我不太确定。 (2认同)