Repository navigation
Universal OpenCV Project
Tomoaki Teshima edited this page Oct 30, 2017
·
1 revision
How do you build OpenCV project ? using makefile or Visual Studio ? To use OpenCV, you need to setup some configurations, such as include path, library path and the library name. Now how do you do them ?
The following document explains how to set up OpenCV on Visual Studio using CMake. It's supposed to work universal among any version of OpenCV.
Basically, "Let CMake handle it".
- NEVER WRITE THE FULL PATH OF OPENCV IN YOUR SOURCE CODE. It's bold sentence, and it's bold for good reason.
- NEVER WRITE THE FULL PATH OF OPENCV IN YOUR SOURCE CODE. I wrote it twice, and I wrote it for good reason.
- Here's an example of hard coded path
#if _DEBUG
#pragma comment(lib, "opencv_core330.lib")
#pragma comment(lib, "opencv_highgui330.lib")
#pragma comment(lib, "opencv_imgproc330.lib")
#else
#pragma comment(lib, "opencv_core330d.lib")
#pragma comment(lib, "opencv_highgui330d.lib")
#pragma comment(lib, "opencv_imgproc330d.lib")
#endif- UGLY!! Really UGLY!!
- If you can guarantee ALL of the following points, you have a chance that you have a right to write a full path in your project
- ONLY your self will read or build the project
- You will NEVER change the version of OpenCV
- You will NEVER build the source code on other machine/hard drive. Hence, your computer/hard drive has to live FOR EVER.
- Now I'm quite sure that the no one fullfills the third requirement.
- There's no good point to hard code the path. Let CMake handle it.
- Pros and Cons
- Pros
- Easy to write
- Easy to build
- Cons
- Anyone who wants to use that source code needs to set up EXACTLY SAME path
- Changing version is headache. You need to re-write all the number embedded in the source code
- Some of the lib file only exist in 2.4 series (like gpu, opencl) or only in 3.0 series (like dnn, cudaimgproc, imgcodecs)
- This would be nightmare if you want to switch from/to 3.0 to/from 2.4.
- There is no chance to hard code the configuration. "Let CMake handle it".
- I believe that this approach is common with Visual Studio.
- This property sheet is a smart approach since it divides the code and the configuraiton in a different space.
- Now you have an freedom to install your OpenCV in arbitary location, and point that path using property sheet. Cool!
- Still, my point is that pointing the OpenCV is still headaching
- The approach of the property sheet doesn't solve any of the previous situation
- For example, if you wish to use another version of OpenCV, you have to change all the path in the propety sheet.
- If your hard drive crahses, you need to install OpenCV again, but it has to be at the same location. Isn't it pain to install OpenCV to same location every time ?
- It's better since all the configuration are written in one place, but still, are you going to write down every lib file that you use ?
- Let CMake handle it.
- Same as the library files, you need to specify the header files of OpenCV
- Based on exactly the same reason, you shouldn't use property sheet nor write the full path in your source code.
- Let cmake handle the path, and you concentrate on your algorithm.
- To make the header file more universal against OpenCV, you should only include
#include <opencv2/opencv.hpp>- As I menteioned previously, in OpenCV 3 series, the imwrite/imread has moved to
imgcodecsmodule, when the same functions still remain inhighguimodule in OpenCV 2.4 series. - To handle the difference, you need to write something like this.
#include <core.hpp>
#include <highgui.hpp>
#if CV_VERSION_MAJOR == 3
#include <imgcodecs.hpp>
#endif- Now this #if approach is UGLY. It's just a waist of lines.
- Just write one line instead of it
#include <opencv2/opencv.hpp>- Now as a con for this appraoch, it may take a longer time for compilation.
- Including single
opencv2/opencv.hppends up in including all available module header files. That may take extra time for compilation, but how much long ? - I havn't measured it, and neither do I imagine to measure the difference. I'm very confident that taking care of the compilation time is very small problem compared to taking care of multiple version of OpenCV
- Just write one line. IT'S WORTH DOING IT.
- Let CMake handle it.
-
NEVER rely on
worldmodule - When OpenCV 3.2 was released, my friend contacted me complaining that the combination of
opencv_contribandopencv_worldends up in an empty library file - Now, I hardly see any good point using
worldmodule- If you are using world module because you don't want to write down all the lib files you use, let CMake handle it.
- If you are using world module because you don't want to write down all the header files you use, let CMake handle it.
- If you are using world module because you don't want to write down all the library/header paths you use, let CMake handle it.
- World module gives you nothing but a possibility of trouble. Just forget to use it and let CMake handle it.
- I'll say it as many as possible
- Let CMake handle it
-
So, using cmake will make your life much more easier
-
Especially if you have to read/write a code from your colleague which uses a different version of OpenCV, you might want to have a war with your colleague at the end.
-
Don't worry just follow the following procedure to avoid the war.
- (optional) build OpenCV using cmake and install it to any location
- set environment variable "OPENCV_DIR" and point your INSTALL_DIR
- use CMake to build your OpenCV project
- use following two lines for OpenCV configurations
Find_Package(OpenCV REQUIRED)target_link_libraries(myApp ${OpenCV_LIBS})
-
Let's dig in to each procedure
- This step is optional, but it comes at the beginning
- It might work by getting the pre-build OpenCV from the official site, but I really recommend you to build OpenCV from the source code.
- Just remember the path of
INSTALL_DIRat cmake stage.
- This is the key point.
- If you set
OPENCV_DIRvariable on your system, cmake will refer it. - For instance, my computer holds following path in
OPENCV_DIR
C:\opencv\build\install
- This trick will work for known Linux distributions and probably for Mac.
- At last, the cmake comes
- For a simple program, your CMakeLists.txt should look something like following
cmake_minimum_required(VERSION 2.8)
project(universalOpenCV)
add_executable(program main.cpp)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(program ${OpenCV_LIBS})- Very nice and clean, isn't it ?
- The include path and the library path will be handled nice and clean by cmake
- If you re-install/re-setup your new machine, all you need to do is build OpenCV and specify the location by
OPENCV_DIR - If you with to change the version of OpenCV, all you need to do is build OpenCV and specify the location by
OPENCV_DIR - just build the OpenCV and specify the location by
OPENCV_DIR!
- If you re-install/re-setup your new machine, all you need to do is build OpenCV and specify the location by
- Later OpenCV has more feature. If your application relies on any new feature, it's impossible to switch to another version of OpenCV.
- ex. dnn module (OpenCV 3.3 and later), SURF/SIFT (OpenCV 2.4 and earlier, currently they moved to contrib repository)
- Now if your app relies on specific version of OpenCV, that doesn't mean that you have a right to hard code the paths.
- Again, let the CMake handle it.
- CMake can handle the version of OpenCV, thus you can check if the OpenCV you are going to link is fullfilling your apps request or not.
- Let anything
Have a nice and "universal" OpenCV life !