根据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. 类似于智能指针等。
我认为最不同的是,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)
一些链接: