Hey Friends,
I am starting my own blog series on OpenCV Development Tutorials and this being my first post. As many of you may have wondered, the most difficult and frustrating part of working on any programming language is not to learn how to code but how to correctly setup your environment. Over the years of my programming experience, i have always hated scenario’s where i am troubleshooting or diagnosing environment build issues from setting up the environment variables to correctly building the libraries from source. My intention using blog as a medium of communication is to *NOT WASTE* anymore precious time of yours and get you up and running smoothly.
#Update 26 November 2017
If you would like to save all your time and the hassle of going through all of this, you can follow the below blog post to download the OpenCV 3 Headers and Libraries and using them without any compilation or issues.
First thing first, it is very important to note that for OpenCV there are two options available as a part of build process.
a) Using the Pre-Built Libraries
b) Compiling from the Source
While, the former used to be my favorite approach uptill version 2.10, i later realized that latter should be your approach because 1) Pre-built libraries won’t work straight out visual studio 2015 and 2) it gives you more clarity with your build environment and also makes you feel more comfortable with the technology when you start things from scratch.
Without any further delay, let’s start the process. In this blog post, we’ll focus on #b.
I. Pre-Requisites
- Visual Studio 2015 (Community or Professional or Express)
- OpenCV 3.3 Libraries (Download Here)
It has two folders:
Build – this contains the includes; also the prebuilt libs and dlls that work with a particular version of Visual Studio.
Sources – source code for OpenCV; also contains sample code and data
- CMake (Download Here)
II. Installation
- Validate Visual Studio 2015
First, make sure you have a working Visual Studio 2015 setup. In order to test everything works out, you can try to run below sample program and confirm that the compiler doesn’t have any issues. This would avoid long time struggle of identifying if it’s the issue with OpenCV or Visual Studio.
- From top menu of Visual Studio, File –> New –> Project. You get a pop up window, New Project.
- Under Visual C++ , Select Win32 -> Win32 Console Application.
- Click Next and Select “Empty Project” and Finish
- Right click the “Source Files” and Select “ADD” and click “New Item”
- Select “C++ File” and give it a name “test”
- Paste the below code in the file and hit F5 or Ctrl + F5
1 2 3 4 5 6 7 8 | #include "stdafx.h" #include "iostream" using namespace std; int main(){ cout "Hello World\n"; return 0; } |
If you are able to successfully run the code, then everything is good up till here. In rare scenarios and where Windows Software Development Kit for Windows 10 is not installed, the code would fail complaining that it didn’t find the C++ Compiler, go ahead and install the required dependencies and you should be good.
2. Extract the OpenCV binaries into a folder under c:\opencv_git
3. Install CMake and open the CMake-GUI from the Start Menu and configure it in the following way:
- Source Code will be your extracted directory = C:/opencv_git
- Build the binaries= C:/opencv_git/mybuild (create a new folder)
- Hit the configure button.
- Specify the Generator for the project, the default “Visual Studio 14 2015 Win64” in our case (remember to use Win64 for x64 built of binaries)
- Use default native compilers, Click Finish button.
- At this point you will see log being generated in the bottom box of the CMake tool. If all goes well, you should see a configuration success message.
- You should see a long list of items with red-pink background.
- Accept the default selections without any modifications.
- Finally click on the Generate button and you should see contents in your build directory once, it finishes.
Ok, so we are half way through, stay put, 80% job is done, and we just need to do one more step to get this up and running.
4. Compile CMake generated binaries using Visual Studio Compiler
- Open the Build Folder where we created the Half-baked OpenCV binaries and you should see a file named as “OpenCV.sln”. This is visual studio 2015 file, so double click on it and you should see visual studio 2015 popping up for you.
- The goal here is to get both Debug and Release binaries and libraries built. First, we’ll use the Debug configuration, so make sure Debug is selected from the configuration manager.
- Right Click the “ALL_BUILD” selection in Solution Explorer and click “BUILD”. It is going to take some time, and here is chance for you to grab that coffee or drink that you have been waiting for. This will build the appropriate xxxxx330d.lib, xxxxx330d.exp and xxxxx330d.pdb files.
- Once, this is done, swap configurations (Debug <–> Release) so Release is selected, then Right Click on ALL_BUILD again and select Build. Time for the washroom break after that drink that you had previously. This will build all xxxxx330.lib, xxxxx330.exp and xxxxx330.dll files.
- After both sets of builds are complete, Expand the “CMakeTargets” folder within Solution Explorer and Right click on “INSTALL” and then Build once more. This will join both Debug and Release libraries into a single “lib” and “bin” folder. This would save tons of extra work for your reverse build environments.
- Hurray, this completed the Compilation process and you should see compiled binaries and libraries under C:\opencv_git\mybuild\install\x86\vc14\bin and C:\opencv_git\mybuild\install\x86\vc14\lib respectively.
5. Validate OpenCV Windows 10 build Environment
- Now, that we have built the OpenCV binaries and libraries, it’s time to get into action and run a demo program for which we did all the hardwork. Let’s get that to work.
Note: For any OpenCV project, you have to tell the C/C++ compiler where the OpenCV header files are, and you have to tell the Linker system where the compiled OpenCV library files are located. This should be your first step while working on any OpenCV projects, otherwise you will get annoying “File Not Found” error messages.
- If you haven’t closed your Visual Studio instance, create a New Empty Project and add a new Source.cpp file to Sources.
- Select the configuration mode as “Release”.
- In the Solution Explorer, right click the project title and open the Properties.
- On the Configuration Properties, C/C++, General settings for Additional Include Directories, enter “C:\opencv_git\mybuild\install\include\”
- On the Configuration Properties, Linker, General settings for Additional Library Directories, enter “C:\opencv_git\mybuild\install\x86\vc14\lib”
- On the Configuration Properties, Linker, Input, Additional Dependencies, add the following libraries:
opencv_core330.lib
opencv_highgui330.lib
opencv_imgproc330.lib
opencv_imgcodecs330.lib
opencv_video330.lib
opencv_videoio330.lib - NOTE: There are SEPARATE Property entries, depending on if you are in Debug or Release mode.
- And finally, you have to add the .dll library locations to the Environment Variables / System Variables PATH. In this case I’ve added both C:\opencv_git\mybuild\install\x86\vc14\bin and C:\opencv_git\mybuild\install\x86\vc14\lib to Path entry.
Note: Visual Studio 2015 contains VC14 compiler which is used with OpenCV thus we are going to use vc14 folder from OpenCV installation.
Here is the test code, that i wrote which will simply open up your webcam and display live feed.
#include "opencv2/opencv.hpp"
#include "opencv2/opencv.hpp"
#include "opencv\highgui.h"
using namespace cv;
int main()
{
Mat image; //Create Matrix to store image
VideoCapture cap; //initialize capture
cap.open(0);
namedWindow("window", 1); //create window to show image
while (1)
{
cap >> image; //copy webcam stream to image
imshow("window", image); //print image to screen
waitKey(30); //delay 30ms
}
return 0;
}
Hi, the problem is here : when I want to reconfigure the cmake
CMake Error: The source directory "C:/OpenCV/opencv" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
Regards