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:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末寿冕,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子海雪,更是在濱河造成了極大的恐慌合愈,老刑警劉巖出刷,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件根穷,死亡現(xiàn)場離奇詭異杂曲,居然都是意外死亡冗美,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門掸屡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來封寞,“玉大人,你說我怎么就攤上這事仅财”肪浚” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵满着,是天一觀的道長谦炒。 經(jīng)常有香客問我,道長风喇,這世上最難降的妖魔是什么宁改? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮魂莫,結(jié)果婚禮上还蹲,老公的妹妹穿的比我還像新娘。我一直安慰自己耙考,他們只是感情好谜喊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著倦始,像睡著了一般斗遏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鞋邑,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天诵次,我揣著相機(jī)與錄音,去河邊找鬼枚碗。 笑死逾一,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肮雨。 我是一名探鬼主播遵堵,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼怨规!你這毒婦竟也來了陌宿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤椅亚,失蹤者是張志新(化名)和其女友劉穎限番,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呀舔,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了媚赖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片霜瘪。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖惧磺,靈堂內(nèi)的尸體忽然破棺而出颖对,到底是詐尸還是另有隱情,我是刑警寧澤磨隘,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布缤底,位于F島的核電站,受9級特大地震影響番捂,放射性物質(zhì)發(fā)生泄漏个唧。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一设预、第九天 我趴在偏房一處隱蔽的房頂上張望徙歼。 院中可真熱鬧,春花似錦鳖枕、人聲如沸魄梯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酿秸。三九已至,卻和暖如春魏烫,著一層夾襖步出監(jiān)牢的瞬間辣苏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工则奥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留考润,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓读处,卻偏偏與公主長得像糊治,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子罚舱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容