OpenCV 3.0 was released recently, and you might be thinking of upgrading your code base. OpenCV 2 code will most likely not compile with OpenCV 3 because the new version is not backwardly compatible. So you need a mechanism to make sure your code is compatible with both OpenCV 3 and OpenCV 2. This post explains how to detect the version of OpenCV inside your code. Example C++ and Python code is shown below.
How to Detect OpenCV Version in Python
Everything is easy in Python. cv2.__version__ gives you the version string. You can extract major and minor version from it as shown in the example below.
import cv2
# Print version string
print "OpenCV version : {0}".format(cv2.__version__)
# Extract major, minor, and subminor version numbers
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
print "Major version : {0}".format(major_ver)
print "Minor version : {0}".format(minor_ver)
print "Submior version : {0}".format(subminor_ver)
if int(major_ver) < 3 :
'''
Old OpenCV 2 code goes here
'''
else :
'''
New OpenCV 3 code goes here
'''
How to Detect OpenCV Version in C++
In C++ several macros are defined to easily detect the version — CV_VERSION, CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION. See the sample code below as an example.
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
cout << "OpenCV version : " << CV_VERSION << endl;
cout << "Major version : " << CV_MAJOR_VERSION << endl;
cout << "Minor version : " << CV_MINOR_VERSION << endl;
cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;
if ( CV_MAJOR_VERSION < 3)
{
// Old OpenCV 2 code goes here.
} else
{
// New OpenCV 3 code goes here.
}
}
You can see another example at my post about Blob Detector.
I use this little program to test the version of OpenCV.
When I try this: g++ -I/home/martin/lib/opencv-2.4.12/include/opencv2 -L/home/martin/lib/opencv-2.4.12/lib/ -lopencv_core main.cpp
Then I get his when I run :
OpenCV version: 2.4.11
This is the OpenCV version I installed in /usr/local/lib and /usr/local/include. Why don’t take the compiler the version 2.4.12 I described in command above?
That is strange. What do you get when you do the following
ls /home/martin/lib/opencv-2.4.12/lib/
libopencv_calib3d.so libopencv_ml.so.2.4.12
libopencv_calib3d.so.2.4 libopencv_nonfree.so
libopencv_calib3d.so.2.4.12 libopencv_nonfree.so.2.4
libopencv_contrib.so libopencv_nonfree.so.2.4.12
libopencv_contrib.so.2.4 libopencv_objdetect.so
libopencv_contrib.so.2.4.12 libopencv_objdetect.so.2.4
libopencv_core.so libopencv_objdetect.so.2.4.12
libopencv_core.so.2.4 libopencv_ocl.so
libopencv_core.so.2.4.12 libopencv_ocl.so.2.4
libopencv_features2d.so libopencv_ocl.so.2.4.12
libopencv_features2d.so.2.4 libopencv_photo.so
libopencv_features2d.so.2.4.12 libopencv_photo.so.2.4
libopencv_flann.so libopencv_photo.so.2.4.12
libopencv_flann.so.2.4 libopencv_stitching.so
libopencv_flann.so.2.4.12 libopencv_stitching.so.2.4
libopencv_gpu.so libopencv_stitching.so.2.4.12
libopencv_gpu.so.2.4 libopencv_superres.so
libopencv_gpu.so.2.4.12 libopencv_superres.so.2.4
libopencv_highgui.so libopencv_superres.so.2.4.12
libopencv_highgui.so.2.4 libopencv_ts.a
libopencv_highgui.so.2.4.12 libopencv_video.so
libopencv_imgproc.so libopencv_video.so.2.4
libopencv_imgproc.so.2.4 libopencv_video.so.2.4.12
libopencv_imgproc.so.2.4.12 libopencv_videostab.so
libopencv_legacy.so libopencv_videostab.so.2.4
libopencv_legacy.so.2.4 libopencv_videostab.so.2.4.12
libopencv_legacy.so.2.4.12 pkgconfig
libopencv_ml.so python2.7
libopencv_ml.so.2.4
Actually, you have an old source code where they forgot to change the version number. Check this out.
https://github.com/Itseez/opencv/commit/1f5fd865aaf1be73498a8269a8dbf23318399da8
You gotta download the latest 2.4.12
I download the latest version from http://opencv.org/downloads.html
Ok if you compiled it from the source, check this
cat modules/core/include/opencv2/core/version.hpp | grep "#define CV_VERSION_MINOR"
If is says, 12 I am really confused :). But if it says 11, you know the problem.
I am very new with OpenCV, but from which position you try the command above? When I try this at the position of my testversion source the location is not recognized.
When I use this command in the folder of 2.4.12 it says 12 (cat /home/martin/lib/opencv-2.4.12/include/opencv2/core/version.hpp grep “#define CV_SERION_MINOR”)
I installed 2.4.12 in another location than /usr/local/ because I have an overview of the different versions. It seems when I compile the source with g++ it takes always the version in /usr/local/lib.