c++ - opencv 4.x API 与之前的版本有何不同?

标签 c++ opencv c++11

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

这究竟意味着什么?

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

最佳答案

我认为最不同的是,OpenCV 4.0 使用了更多的 C++11 特性。现在 cv::String == std::stringcv::Ptrstd::shared_ptr 之上的薄包装。

Opencv 4.0 删除文件夹include/opencv,只保留include/opencv2。 OpenCV 1.x 中的许多 C API 已被删除。受影响的模块是 objdetect、photo、video、videoio、imgcodecs、calib3d。不建议使用旧的宏定义或未命名的枚举,而是使用命名的枚举。

//! 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.
     };

    // ...
}

比如读取图片时应该是这样的:

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

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

在 OpenCV 3.4 中:

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

enter image description here

在 OpenCV 4.0 中:

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

enter image description here

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

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

一些链接:

  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

关于c++ - opencv 4.x API 与之前的版本有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906178/

相关文章:

c++ - 在一行中将 Template<class T> 应用于多个类型别名

c# - OpenSSL无法验证ECDSA签名C++、C#验证正确

visual-studio - 为什么 Visual Studio 2010 无法找到/打开 PDB 文件?

python - 适用于 Python 3.5.1 的 OpenCV

c++ - 在范围适配器的迭代器类中实现 operator->

c++ - 从迭代器转换为变量的类型。 (C++11 模板)

c++ - 转换运算符 template<typename T> T&&() 是否有意转换为右值?

c++ - 从 Worker 暂停/恢复 Qthread

c++ - 字符串下标超出范围(C++)

xcode - OpenCV-文件是为不受支持的文件格式构建的,该文件格式不是要链接的体系结构(i386)