[Mac]MacOS Sierra Using CMake to Compile OpenCV 3.2 With Contrib (for C++)

[Preface]

OpenCV, stands for Open Source Computer Vision Library, which is a super powerful computer vision library. Start from version 1, 2 to the current 3, all the functions, features are continuous progress and update. But since it is a third-party library, the installation will be a little bit troublesome. 
You can run OpenCV on the most of the OS, such as Windows, MacOS, Linux, Android, etc. This article will use MacOS Sierra as an example to compile OpenCV.

Other points is that from OpenCV 3, the entire library structure has been changed to an object-oriented approach. And it seems that for the popularization and lightweight, the pre-build library from official website is the basic version, that means if you need more advanced algorithms or functions, you need to compile with contrib(extra module). Not only can use extra function, but the self-compiled library will be optimized according to your environment. 

In short, if you need to do the research or need maximum efficiency, I will recommend to compile it again. This article will build a OpenCV C++ library. If you need to use other languages, like Python, check these website out.

[Requirement]

1.Mac With MacOS Sierra
2.OpenCV 3 Source Code 
3.OpenCV Contrib 
4.CMake (I install a GUI version)
5.Xcode (Install it before compile)
6.Xcode Command Line Tools (same as Xcode)

▼OpenCV 3 source code,  Contrib, and CMake




2017/11/07 Update

After testing, Xcode and Command Line Tools are required. And they need to be execute at least onetime then the CMake will be able to get the information about compiler.


[Step by Step]

  • Install CMake

▼Simply drag and double click

  • Create CMake project file
Unzip OpenCV Source Code and the folder should look like this.

▼OpenCV Source Code

Then open Cmake.
▼ CMake

First, locate the source code, which will be the OpenCV source code folder.

Second, locate to the folder where you want to put the CMake project files.

And then, click Configure button on the left bottom.

After that, CMake will ask you how will you want to use for the compiling, I chose Unix MakeFile. The first time use XCode to compile, but always got some error during the compiling.

After the first time configure, the list should have many red line. Click configure again to make all the lines become white, which is correct configured.

After that, find the option OPENCV_EXTRA_MODULES_PATH on the list. Locate to the modules folder in the Contrib folder.

▼ The Modules folder in the Contrib folder
Then click Configure a few times until all the red lines gone. The list will be the parameters of build OpenCV. You can edit them if you need to.

▼It should look like this if all the thing are correct

Chick Generate the make file of OpenCV. If some error happened, the log will be printed below.

  • Compile OpenCV

Launch Terminal and go to the folder where you put the CMake project files (the second line of the CMake).

And execute the command:

$ make -j4

The j4 means it will use 4 threads to do the compile, which will be faster. You can edit to by yourself. This part will take a ling time. Get a coffee!


▼It should look like this when it is done.
When it reach 100% and no error, then that is correct! And run the command:
$ sudo make install

▼Also let it run until 100%

Now, the OpenCV environment is done! Next, let see how to create a Xcode project.

  • Create a C++ Project using Xcode

This part is not so complicated, so I will use photo to describe it. First, create a C++ project, click Build Setting on the top. Find Search Path, and look for Header Search Path and Library Search Path.

▼Click + and add two paths
/usr/local/include    //Header
/usr/local/lib    //Library




Then, create a folder under the project.

▼Click Add Files to "" ...








▼Click " / ", and will let you to type a path


▼Locate to the path:

/usr/local/lib</ code>
Select and add all the files which the name starts with OpenCV.


Done! And you can start to coding with OpenCV!

Comments

Popular posts from this blog

[Mac] MacBook Pro 2011 Late SSD Replacement

[Raspberry Pi]Build OpenCV 4 on Raspberry Pi With Multiple Languages(C++&Python2,3) (2019/9/20 Updated)

[Windows 10] Install, Beautify WSL and My Personal Review