I noted that opencv 4 is released and one difference is that API changed to be c++11 compliant.
What this really means?
How should I change my codes to be compatible with this version?
If you do not have to depend on earlier versions, and want to use OpenCV with Python, choose the latest stable version. Today it is OpenCV 2.3. 1. The major benefit of OpenCV ≥ 2.3 for Python users: a new cv2 module in addition to the old (backwards compatible) cv module.
Checking your OpenCV version: a real-world example X and OpenCV 3 to detect the contours (i.e. outlines) of the Tetris blocks. As you can see, all we need to do is make a call to is_cv2 , is_cv4 , and is_cv3 and then wrap our version specific code inside the if statement blocks — that's it!
The OpenCV – 4.5. 1 version is released in 2020 with advanced features of the library. The OpenCV 4. x is based on C++ 11 library and the OpenCV – 1.
I think the most different is, the OpenCV 4.0
use more C++11 features. Now cv::String == std::string
and cv::Ptr
is a thin wrapper on top of std::shared_ptr
.
The Opencv 4.0 remove folder include/opencv
and only keep include/opencv2
. A lot of C API from OpenCV 1.x has been removed. The affected modules are objdetect, photo, video, videoio, imgcodecs, calib3d
. The old macro defination or unnamed enum is not suggested, use named enum 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.
};
// ...
}
Such as, when read image, it should be something like this:
cv::Mat img = cv::imread("test.png", cv::IMREAD_COLOR);
Except the new featues, the most C++ APIs keep the same. While the biggest different I found is cv2.findContours
(in Python OpenCV
):
In OpenCV 3.4:
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
In OpenCV 4.0:
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
An alternative to work with 2.x 、3.x、4.x is:
cnts, hiers = cv2.findContours(...)[-2:]
Some links:
According to OpenCV 4.0.0, you don't have to make any significant modifications (most likely any at all) to your source codes, unless you are using some C API which has been removed.
As already stated
OpenCV is now C++11 library and requires C++11-compliant compiler
To use c++11
, clang version 3.3 and higher is required with flag -std=c++11
. Same for g++ 4.3 and higher.
It allows them to use std::string
instead of cv::String
, and other c++11 features. But don't worry, cv::String
will still work, but is now allias for std::string
. Similliar for smart pointers, etc.
I think the most vital impact is you need to use a c++11 compiler.
Also, it may not change the interface but allow them to make use of the updated Language changes, such as smart pointers, etc.
The above comment "According to OpenCV 4.0.0, you don't have to make any significant modifications (most likely any at all) to your source codes, unless you are using some C API which has been removed." appears to be far from true.
I and my partner developed a package for a collaborator. We used v 3.4 (C++140 ; the collaborator has installed v 4.5. Now we are finding our code can't be compiled on their system because of numerous symbol name changes. Examples: CV_THRESH_TOZERO, CV_GRAYTORGB.
We do know how to solve the problem (short of rewriting our software) which involves a pile of #ifdefs. However this is not the usual meaning of "backward compatible". And it should not have been necessary: the opencv people should have kept the old definitions as well as the new ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With