Skip to main content

OpenCV Quickstart on Windows

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.
  1. Windows 10
  2. Visual studio express 2015
  3. Opencv 3.1
  4. Dependencies : Just choose what you need.
opencv_-config_properties

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.
vs-newproj.PNG
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.
vsinstaller.PNG
You may also want to click the middle tab "Other Components" and check the Windows 10 SDKs.
additionalSDK.PNG
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.
cmake-gui
CMake configuration for OpenCV build

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
opencv_sln_debug.
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.
property-pages
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
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.

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Or 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.

    ReplyDelete
    Replies
    1. Thanks! Vcpkg looks like an awesome tool! Will definitely try this for my next installs.
      On 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.

      Delete

Post a Comment

Popular posts from this blog

Using FCM with the new HTTP v1 API and NodeJS

When trying to send FCM notifications I found out that Google has changed their API specifications. The legacy API still works but if you want to use the latest v1 API you need to make several changes. The list of changes is listed on their site so I won't be repeating them again but I'll just mention some of the things that caused some trial and error on my project. The official guide from Google is here : Official Migration Guide to v1 . The request must have a Body with the JSON containing the message data. Most importantly it needs to have "message" field which must contain the target of the notification. Usually this is a Topic, or Device IDs. Since my previous project was using GAS, my request had a field called "payload" instead of "body". Using the request from my previous project, my request in Node JS was as follows: request ({ url: 'https://fcm.googleapis.com/v1/projects/safe-door-278108/messages:send' , method: ...

Building a native plugin for Intel Realsense D415 for Unity

Based on a previous post , I decided to write a plugin for the Intel Realsense SDK methods so we can use these methods from within Unity. FYI Intel also has their own Unity wrapper in their Github repository , but for our projects, I needed to perform image processing with OpenCV and passing the results to Unity instead of just the raw image/depth data. There is a plugin called OpenCVForUnity to use OpenCV functions from Unity but previous experiments indicate the image processing inside Unity can take a long time. I hope this post can help someone else who wants to use Intel's cameras or any other devices natively in Unity. Test Environment Windows 10 64bit Unity 2017.2.0f3 x64 bit Realsense SDK from Intel CMake 3.0 or higher Steps Checkout the native plugin code here . Don't worry about the other projects in the same repository. The relevant code is in the link above. Checkout the Unity sample project here . However, instead of master you need to go to the br...

Microsoft Azure Face API and Unity

During one of my projects, I came across Microsoft's face recognition API (Azure Face API) and it looked good enough to recognize people's faces and detect if a person is a newcomer or a repeating customer to our store. As our installations mainly use the game engine Unity, I wanted to be able to use the Face API from Unity. Face API does not have an SDK for Unity but their requests are just HTTP requests so the Networking classes in Unity can be wrapped into methods to make it easy to call these APIs. First of all, to those who just want to see the code, here it is . My tests focus on the identification of a face in an input image. The full tutorial I followed can be found here . The Main scene goes through the steps in the tutorial such as creating a PersonGroup and adding Persons to the group if it is not created yet. Just make sure you: Change the API key. I used a free trial key which is no longer valid. Use whatever images you want. I don't mind you us...