This guide will illustrated how to install the OpenCV and Boost libraries as well as get them ready for use with your IDE. This is certainly not the only way to set things up, but it does have the virtue for working for me in the past.
Note that we will also install CMake. What this will do is allow us to more easily create projects that properly link to OpenCV and Boost with the development environment of our choice. Keep in mind: once you create the project in CMake, you do NOT (usually) have to recreate the project to compile it. In other words, CMake creates the project, and then you open the project in your IDE to write code and compile it.
Specifically, this guide covers:
- CMake 3.7.2
- OpenCV 3.2
- Boost 1.63.0
Windows
The following has been tested on Windows 7 (64-bit).
1. Download and install CMake
Go to CMake downloads and download the Windows win64-x64 Installer. NOTE: If you already have CMake 3.4 or lower, you may have to uninstall that first!
Once downloaded, run the installer.
2. Download OpenCV
Go to OpenCV downloads and download Version 3.2: OpenCV for Windows.
3. Unpack OpenCV
There are many ways to handle how to organize your dependencies. I would suggest you make a folder at the top level of one of your hard disks called "Dependencies". For example:
E:/Dependencies
Inside of this folder, put the self-extracting executable you just downloaded, and run that executable. When you are done, there should be a folder called "opencv":
E:/Dependencies/opencv
There is a "build" directory under this "opencv" folder:
E:/Dependencies/opencv/build
From here on out, we will refer to OPENCV_DIR as the path to this build directory.
OpenCV should be ready to go at this point if you are using Visual Studio 2015 (there are pre-compiled binaries). However, if you are using a different IDE, you may have to recompile the library.
4. Download Boost
Go to the Boost website, and look under "Current Release". Click on "Version 1.63.0", and on the next page download boost_1_63_0.zip.
5. Unpack Boost
Similar to what you did for OpenCV, put the Boost zip file into your Dependencies folder and "Extract to here". When you are done, there should be a folder called "boost_1_63_0":
E:/Dependencies/boost_1_63_0
From here on out, we will refer to BOOST_DIR as the path to this directory.
6. Compile Boost
Because we will be using the filesystem modules, we will have to compile Boost from source. Fortunately, it isn't as much hair-pulling as it usually is.
If you are using Visual Studio 2015, open the Start Menu and go to "Visual Studio 2015" -> "Visual Studio Tools" -> "Windows Desktop Command Prompts" -> "VS2015 x64 Native Tools Command Prompt".
Change directories to BOOST_DIR. For example:
cd E:/Dependencies/boost_1_63_0
E:
(Remember: if you installed Boost to a drive other than C (for example, E), you will have to type the other drive letter and a colon after issuing the cd command (as shown above).
Complete details can be found here, but the short version is:
bootstrap
.\b2 --toolset=msvc-14.0 address-model=64 link=static --build-type=complete
Note that the above parameters are specifically for Visual Studio 2015 (64-bit).
Go make yourself a cup of coffee, or tea, or whatever caffeinated beverage works for you. This will take a bit of time.
After this, your libraries should be ready to go!
Linux
The following has been tested on Ubuntu Desktop (64-bit).
It should be noted that, if you can get OpenCV 3.2 and Boost 1.63.0 from your package manager, you are free to do so. However, the guide that follows will assume you have to compile and install them manually.
1. Install CMake
Use your package manager to install CMake; it should be version 2.8 or above.
2. Download OpenCV
Go to OpenCV downloads and download Version 3.2: OpenCV for Linux/Mac.
3. Unpack OpenCV source
Unpack the source to any folder you see fit. I opted to put mine in a folder called "Dependencies" in my HOME directory:
~/Dependencies
Inside that folder, put the .zip file you just downloaded, and unzip it:
cd ~/Dependencies
unzip opencv-3.2.0.zip
When you are done, there should be a folder called "opencv-3.2.0". This is the SOURCE folder:
~/Dependencies/opencv-3.2.0
From here on out, we will refer to OPENCV_SRC as the path to this directory.
4. Compile OpenCV
Alas, we must compile OpenCV before we can use it. The following instructions are borrowed in part from these instructions.
There are several dependencies you will need. The instructions mentioned above suggest the following commands:
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
However, you won't be able to do this if you do not have administrator privileges on the machine you are working on. Note also that the third list of packages is optional.
Open a commandline window and go to OPENCV_SRC:
cd ~/Dependencies/opencv-3.0.0
Create a temporary folder called "build" and cd to it:
mkdir build;
cd build
Issue the following command:
cmake -D CMAKE_BUILD_TYPE=RELEASE ..
(Note that those two dots on the end are necessary.)
Also note that there are other options/modules we could turn on at this point, but for now we're going with a basic setup.
Then, issue these commands:
make
sudo make install
Again, the latter command implies you have administrator privileges.
Either way, this will take a fairly long while, so go make yourself a cup of coffee, tea, or whatever beverage you choose.
5. Download Boost
Go to the Boost website, and look under "Current Release". Click on "Version 1.63.0", and on the next page download boost_1_63_0.zip.
6. Unpack Boost
Similar to what you did for OpenCV, put the Boost zip file into your Dependencies folder and extract it to there:
cd ~/Dependencies
unzip boost_1_63_0.zip
When you are done, there should be a folder called "boost_1_63_0":
~/Dependencies/boost_1_63_0
From here on out, we will refer to BOOST_DIR as the path to this directory.
7. Compile Boost
Because we will be using the filesystem modules, we will have to compile Boost from source. Fortunately, it isn't as much hair-pulling as it usually is.
Change directories to BOOST_DIR:
cd ~/Dependencies/boost_1_63_0
Complete details can be found here, but the short version is:
./bootstrap.sh
./b2
sudo ./b2 install
Go make yourself a cup of coffee, or tea, or whatever caffeinated beverage works for you. This will take a bit of time.
After this, your libraries should be ready to go!
Creating a Project with CMake
1. Prepare files
Let's assume you want to create a project in a folder called "TestProject". Inside TestProject, create a new text file named "CMakeLists.txt" (this will ALWAYS have the same name). In this file, insert the following text:
cmake_minimum_required(VERSION 2.8)
#set(BOOST_ROOT "E:/Dependencies/boost_1_64_0")
#set(OpenCV_DIR "E:/Dependencies/opencv/build")
set(Boost_USE_STATIC_LIBS ON)
set(OpenCV_STATIC ON)
project(TestProject)
find_package(OpenCV REQUIRED)
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
file(GLOB MY_SOURCE_FILES
"*.h"
"*.hpp"
"*.cpp"
)
add_executable(TestProject ${MY_SOURCE_FILES})
target_link_libraries(TestProject ${OpenCV_LIBS} ${Boost_SYSTEM_LIBRARIES} ${Boost_FILESYSTEM_LIBRARIES})
This CMakeLists.txt file is also available here.
If you are using Windows, uncomment the "set" lines near the top (and replace with the appropriate paths).
As a test, download the source file TestMain.cpp.
Also copy in a PNG image into the build directory of your project.
2. Run CMake and Compile Using Command Line Under Unix/Linux
Create a folder "build" and cd to it:
mkdir build
cd build
Now, run the following:
ccmake ..
(Note the double dot is required.)
Once that's done, you should have the project files you need.
To compile, run:
make
3. Run CMake GUI and Compile Using IDE
Open the CMake GUI. Change the "Where is the source code" path to where your project code and CMakeLists.txt file is; we'll refer to this as "SOURCE_DIR". Change the "Where to build the binaries" path to SOURCE_DIR/build. Hit the "Configure" button.Say "Yes" if it asks you to create the folder.
On the window that follows, select the compiler/development environment you wish to use. For Visual Studio 2015, you want "Visual Studio 14 2015 Win64"."Use default native compilers", and click "Finish".
If there are no errors, hit "Generate" to create the project.If it completes successfully, you should now have a solution file (or project file) in the build folder which you can use to build your projects.
If you are using Visual Studio, you will have to set the PATH variable to include the dll's for OpenCV.Open your solution file, right-click on the project ("TestProject"), click "Properties", go to "Debugging", and under the Environment field, enter the following:
PATH=$PATH;E:/Dependencies/opencv/build/x64/vc14/bin
Replace "E:/Dependencies/opencv" with wherever OpenCV is installed.
4. Running the program
When you compile and run, you should see a console window, and any PNG files in the current directory will be loaded and displayed one at a time. Press any key to close each window:
You should also see some files and folders printed out: