我注意到 opencv 4 已发布,其中一个区别是 API 更改为与 c++11 兼容。
这究竟意味着什么?
我应该如何更改我的代码以与此版本兼容?
最佳答案
我认为最不同的是,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
。不建议使用旧的宏定义或未命名的枚举,而是使用命名的枚举。
//! 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:]
一些链接:
- OpenCV Release
- OpenCV ChangeLog
- OpenCV Introduction
- OpenCV Documentation
- How to use `cv2.findContours` in different OpenCV versions?
- OpenCV on Stackoverflow
关于c++ - opencv 4.x API 与之前的版本有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906178/