My library currently uses OpenCV 2. Now, I am trying to compile the library to use OpenCV 3. It seems that some header files were moved and some constants were renamed. For example, CV_INTER_LINEAR
is not defined in OpenCV 3.
As my library needs to be compiled and run on both OpenCV 2 and 3, Is there any simple way to overcome this incompatibility?
I can change my code, but then it doesn't work in OpenCV 2.
I had the same problem a few months ago, and I have to contraddict Kiran: I got my relatively big project to build both with OpenCv 2 and 3. Being able to build with both 2 and 3 is very important in a project transition phase, especially because OpenCV 3 is not in stable version yet, and yet has amazing optimization features.
Isn't cv::INTER_LINEAR
defined both in OpenCV 2 (header imgproc/imgproc.hpp
) and 3? You'll have to replace some constant names, add a couple of inclusions, but that won't be too hard.
Regarding header inclusions, there's a way to find an including path compatible both for Opencv2 and 3.
From the link Kiran posted:
In OpenCV 3 we write
#include "opencv2/core.hpp"
instead of
#include "opencv2/core/core.hpp"
The old method should also work.
Update
In the new Release Candidate, it's reported they have improved compatibility between OpenCV 2.4 and 3.x
Improved compatibility with OpenCV 2.4:
- 2.4.11 now includes "opencv2/core.hpp" and other such headers in addition to standard "opencv2/core/core.hpp".
- smart pointers (Ptr<>)
can now be created in both 2.4 and 3.0 style (new ClassName(params) vs
makePtr(params))
- trained and stored stat models from
opencv_ml 2.4 can now be loaded and used by opencv_ml 3.0 as-is.
- the
2.4=>3.0 transition guide has been sketched: http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html
EDIT:- Read Antonio's answer. You can include compatibility header types_c.h
(1) and use OpenCV 2 constant names which works in OpenCV 3 also. This is since, even though constant names differ (e.g. INTER_LINEAR
vs CV_INTER_LINEAR
), constant values remain the same(2). So functions accepting integer const values should behave no different in OpenCV 2 and 3. Same is the case for other constants also. This way you just have to change header paths between OpenCV 2 and 3.
OLD ANSWER:- This is not possible. OpenCV 3 is not completely backward compatible with OpenCV 2. You will have to change enums, header paths etc between OpenCV 2 and 3. See Changelog(3) for sections about backward compatibility.
You could try things like copying headers around, giving numbers instead of constant names, using #ifdefs
all over your code - but sooner or later that is going to fail. I suggest maintaining separate copies and have peace of mind. I would rather ignore OpenCV 2 altogether unless I am currently in production.
The OpenCV guys have put up a transition guide online : http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html
In essence I think the following have changed:-
- ml module-> The existence of StatModel in OpenCV 3 and that being the root for all classifiers.
- features2d and xfeatures2d -> SIFT and a couple of others have moved from being in the core repository to the opencv_contrib repo
- And many others I am not aware of
As for code working with Opencv3 and opencv2, I usually introduce a prepocessor directive which holds a flag:-
#define HAS_OPENCV3 1
#ifdef HAS_OPENCV3
#include <opencv2/core.hpp> //Any OPENCV3 code
#else
#include <opencv2/core/core.hpp> //Any Opencv2 code
#endif
Since the relative portions are eliminated before compilation, it will also compile with only the OpenCV2 or OpenCV3 libraries. But, introduces a lot of redundancy (which can be avoided by some clever coding).