Configure OpenCV and Boost

Original Link

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:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容