So after about 7 years I recently started playing around with OpenCV again. It really has come a long way especially on how it distributes binaries and libraries. I still remember the old days when building on Linux was hard but possible whereas Windows was short of impossible. Well it seems that has all changed ! After struggling to build this new version of OpenCV for about 2 days I decided to share it here for my own memo and hopefully it can also help others who need it. This is heavily based on this tutorial.
Let's dive in. First things first. We need to make sure we have a similar build environment.
- Windows 10
- Visual studio express 2015
- Opencv 3.1
- Dependencies : Just choose what you need.
Step 1
Download OpenCV executable here. You will find it in your Downloads folder. Run it and you will get an opencv folder. Feel free to move it around to any path you like. Let's assume it is in {opencv_dir}. In my case, as you can see from the screenshot, I moved it to ~/Desktop/Workspace/.
Step 2
Download CMake here. I used the latest stable version 3.6.2. Extract the zip and launch the GUI (run cmake-gui.exe). As you can see from the screenshot, we have to specify the source code folder and the build folder. You can choose /sources and /build like me but you can also specify another folder for your build, for example /mybuild.
When launching the first time, you can manually specify compilers but the default settings work just fine.
Potential pitfall : Just installing Visual Studio Express 2015 does not install the VC++ Build tools. If you see an error that says C_COMPILER not specified, create a new C++ Project with Visual Studio Express 2015. You will be prompted to download and install the Build Tools.
Update : You may also see another error when you don't have a compiler installed. Go to the {opencv install folder}/build/CMakeFiles/CMakeError file and check for the following error : Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Update : Visual Studio 2017 has a different UI to setup C++ compilers. When you go to Files->New Project you will be presented with the following screen.
I apologize that my version of VS2017 is Japanese, but you have to click on the "Launch Visual Studio Installer" link. This will launch another screen to customize what additional plugins or libraries you may need. Once you have the C++ compiler press Configure.
You may also want to click the middle tab "Other Components" and check the Windows 10 SDKs.
If everything works fine you will see the screenshot below. You can customize the OpenCV build but default is fine. For my setup, I unchecked the BUILD_SHARED_LIBS option under BUILD because I wanted to link to static libraries so I can move the executable to another computer without an OpenCV installation.
!Important! If you want to use opencv-contrib too, you need to download it at this point and set the path to <opencv-contrib root folder>/modules to OPENCV_EXTRA_MODULES_PATH.
After pressing Generate, go to {opencv_dir}/opencv/ and check that you have two folders. build and sources.
Step 3
From {opencv_dir}/opencv/build, launch OpenCV.sln. Go to the Solution Explorer panel and find CMakeTargets->ALL_BUILD. Right click and choose "Build". Make sure that the build configuration is set to "Debug". Refer to the image below
.
After taking about 20 - 30 minutes change the build configuration to "Release" and right click "ALL_BUILD", and choose "Build" again. This will build the libraries for "Release"
Next, still in the Solution Explorerm go to "INSTALL", right click and choose "Build". Repeat this process for both "Debug" and "Release" configurations.
At this point you will see an install folder in {opencv_dir}/opencv/build/ and you will find all static libraries for both Debug and Release configurations in C:\Users\Sonny\Desktop\Workspace\opencv\build\install\x86\vc14\staticlib
Step 4
Launch Visual Studio Express 2015 and create a new Win32 Application. Then just copy and paste any OpenCV sample code and build. The build will fail at this point but we will get there in a second. For the sample code, the tutorial page I followed here has a good sample.
First, right click on the project and choose "Properties". You will see the image below. In "Additional Include Directories" specify the folder where the header files are. This value is {opencv_dir}/opencv/build/include.
Then, go to Linker->General and here you need to modify the "Additional Library Directories". Add the path to the static libraries from Step 3 The value should be something like {opencv_dir}\opencv\build\install\x86\vc14\staticlib.
After that, go to Linker->Input and add the following to "Additional Dependencies".
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib
glu32.lib
opengl32.lib
opencv_core310d.lib
opencv_calib3d310d.lib
opencv_features2d310d.lib
opencv_flann310d.lib
opencv_highgui310d.lib
opencv_imgproc310d.lib
opencv_imgcodecs310d.lib
opencv_ml310d.lib
opencv_objdetect310d.lib
opencv_video310d.lib
opencv_photo310d.lib
opencv_shape310d.lib
opencv_stitching310d.lib
opencv_superres310d.lib
opencv_ts310d.lib
opencv_videoio310d.lib
opencv_videostab310d.lib
zlibd.lib
IlmImfd.lib
libjasperd.lib
libjpegd.lib
libpngd.lib
libtiffd.lib
libwebpd.lib
ippicvmt.lib
vfw32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib
glu32.lib
opengl32.lib
opencv_core310d.lib
opencv_calib3d310d.lib
opencv_features2d310d.lib
opencv_flann310d.lib
opencv_highgui310d.lib
opencv_imgproc310d.lib
opencv_imgcodecs310d.lib
opencv_ml310d.lib
opencv_objdetect310d.lib
opencv_video310d.lib
opencv_photo310d.lib
opencv_shape310d.lib
opencv_stitching310d.lib
opencv_superres310d.lib
opencv_ts310d.lib
opencv_videoio310d.lib
opencv_videostab310d.lib
zlibd.lib
IlmImfd.lib
libjasperd.lib
libjpegd.lib
libpngd.lib
libtiffd.lib
libwebpd.lib
ippicvmt.lib
vfw32.lib
That's quite a long list !
Finally, add {opencv_dir}\opencv\build\install\x86\vc14\staticlib to PATH and restart your PC.
Step 5
Build your solution and hope it works !!
I hope this helps someone like me who needs to setup OpenCV quickly.
This comment has been removed by a blog administrator.
ReplyDeleteOr you simply download and install Vcpkg and add the .exe path to your system path after open your command line and type "vcpkg install opencv: x64-windows" depending on the version of your system and your operating system, then "vcpkg install integrate" otherwise, try "vcpkg integrate install") after this step, you can create any project with Visual Studio and the library is automatically added to your project.
ReplyDeleteThanks! Vcpkg looks like an awesome tool! Will definitely try this for my next installs.
DeleteOn hindsight, with a simple CMake script that is available online we can add opencv libraries to a Visual Studio project without specifying the individual libraries at the linker settings.