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

新手上路,请多包涵

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

这究竟意味着什么?

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

原文由 mans 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 406
2 个回答

我认为最不同的是, 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

在此处输入图像描述

在 OpenCV 4.0 中:

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

在此处输入图像描述

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

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


一些链接:

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

原文由 Stackoverflow 发布,翻译遵循 CC BY-SA 4.0 许可协议

根据 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 的别名。与智能指针等类似。

原文由 kocica 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题