When developing projects with OpenCV (Open Source Computer Vision Library), identifying the installed version is crucial, as different versions may support varying features and API usage. The following methods can be used to determine the OpenCV version:
1. Check Version Using Python Code
If you are using OpenCV in a Python environment, you can check the installed OpenCV version using the following Python code:
pythonimport cv2 print(cv2.__version__)
This code outputs the version number of the OpenCV library, for example, '4.5.2'.
2. Command Line
For certain installation methods, you can directly query the OpenCV version via the command line:
-
On Linux, if you installed OpenCV via a package manager, you can use the following command to check:
bashpkg-config --modversion opencv4 # For OpenCV 4.xOr, for OpenCV 3.x, you may need to use:
bashpkg-config --modversion opencv -
On Windows, this method is less commonly used because Windows does not have a tool similar to
pkg-config.
3. Check Version in C++
If you are using OpenCV in a C++ environment, you can print the version number by including the OpenCV library header and using predefined macros:
cpp#include <opencv2/opencv.hpp> #include <iostream> int main() { std::cout << "OpenCV version : " << CV_VERSION << std::endl; return 0; }
This code outputs the OpenCV version number.
Practical Example
In a previous project, I needed to use the SIFT feature detection algorithm from OpenCV. This algorithm was available in OpenCV versions up to 3.4.2.16, but due to copyright concerns, it was relocated to the opencv_contrib module in later versions. Therefore, I first used the Python code mentioned above to confirm the installed OpenCV version in our environment to ensure we could directly use the SIFT algorithm without needing to install the opencv_contrib module.
Conclusion
By using these methods, you can conveniently and quickly verify the OpenCV version, enabling you to adjust or utilize specific features as needed. When collaborating in a team or setting up environments, it is crucial to regularly check and standardize the OpenCV version to prevent compatibility issues.