Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV By Example

You're reading from   OpenCV By Example Enhance your understanding of Computer Vision and image processing by developing real-world projects in OpenCV 3

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher Packt
ISBN-13 9781785280948
Length 296 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Vinícius G. Mendonça Vinícius G. Mendonça
Author Profile Icon Vinícius G. Mendonça
Vinícius G. Mendonça
David Millán Escrivá David Millán Escrivá
Author Profile Icon David Millán Escrivá
David Millán Escrivá
Prateek Joshi Prateek Joshi
Author Profile Icon Prateek Joshi
Prateek Joshi
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with OpenCV 2. An Introduction to the Basics of OpenCV FREE CHAPTER 3. Learning the Graphical User Interface and Basic Filtering 4. Delving into Histograms and Filters 5. Automated Optical Inspection, Object Segmentation, and Detection 6. Learning Object Classification 7. Detecting Face Parts and Overlaying Masks 8. Video Surveillance, Background Modeling, and Morphological Operations 9. Learning Object Tracking 10. Developing Segmentation Algorithms for Text Recognition 11. Text Recognition with Tesseract Index

Installing OpenCV

Let's see how to get OpenCV up-and-running on various operating systems.

Windows

To keep things easy, let's install OpenCV using prebuilt libraries. Let's go to http://opencv.org and download the latest version for Windows. The current version is 3.0.0, and you can go to the OpenCV homepage to get the latest link to download the package.

You need to make sure you have admin rights before you proceed. The downloaded file will be an executable file, so just double-click on it to start the installation process. The installer expands the content into a folder. You will be able to choose the installation path and check the installation by inspecting the files.

Once you are done with the previous step, we need to set the OpenCV environment variables and add it to the system path to complete the installation. We will set up an environment variable that will hold the build directory of the OpenCV library. We will be using this in our projects. Open the terminal and type the following command:

C:\> setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc11

Note

We are assuming that you have a 64-bit machine with Visual Studio 2012 installed. If you have Visual Studio 2010, replace vc11 with vc10 in the preceding command. The path specified earlier is where we will have our OpenCV binaries, and you will see two folders inside this path called lib and bin. If you are using Visual Studio 2015, you should be able to compile OpenCV from scratch.

Let's go ahead and add the path to the bin folder of our system's path. The reason we need to do this is because we will be using the OpenCV library in the form of Dynamic Link Libraries (DLLs). Basically, all the OpenCV algorithms are stored here, and our operating system will only load them during runtime. In order to do this, our operating system needs to know where they are located. The system's PATH variable will contain a list of all the folders where it can find the DLLs. So, naturally, we need to add the path to the OpenCV library to this list. Now, why do we need to do all this? Well, the other option is to copy the required DLLs to the same folder as the application's executable file (the .exe file). This is an unnecessary overhead, especially when we are working with many different projects.

We need to edit the PATH variable in order to add it to this folder. You can use software such as Path Editor to do this. You can download it from https://patheditor2.codeplex.com. Once you install it, start it and add the following new entry (you can right-click on the path to insert a new item):

%OPENCV_DIR%\bin

Go ahead and save it to the registry. We are done!

Mac OS X

In this section, we will see how to install OpenCV on Mac OS X. Precompiled binaries are not available for Mac OS X, so we need to compile OpenCV from scratch. Before we proceed, we need to install CMake. If you don't have CMake already installed, you can download it from https://cmake.org/files/v3.3/cmake-3.3.2-Darwin-x86_64.dmg. It's a dmg file! So, once you download it, just run the installer.

Download the latest version of OpenCV from opencv.org. The current version is 3.0.0, and you can download it from https://github.com/Itseez/opencv/archive/3.0.0.zip.

Unzip the contents into a folder of your choice. OpenCV 3.0.0 also has a new package called opencv_contrib that contains user contributions that are not yet considered stable. One thing to keep in mind is that some of the algorithms in the opencv_contrib package are not freely available for commercial use. Also, installing this package is optional. OpenCV will work just fine if you don't install opencv_contrib. Since we are installing OpenCV anyway, it's good to install this package so that you can experiment with it later on (as opposed to going through the whole installation process again). This package is a great way to learn and play around with new algorithms. You can download it from https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip.

Unzip the contents of the ZIP file into a folder of your choice. For convenience, unzip it into the same folder, as mentioned earlier, so that the opencv-3.0.0 and opencv_contrib-3.0.0 folders are in the same main folder.

We are now ready to build OpenCV. Open your terminal and navigate to the folder where you unzipped the contents of OpenCV 3.0.0. Run the following commands after substituting the right paths in the commands:

$ cd /full/path/to/opencv-3.0.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/full/path/to/opencv-3.0.0/build -D INSTALL_C_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/full/path/to/opencv_contrib-3.0.0/modules ../

It's time to install OpenCV 3.0.0. Go inside the /full/path/to/opencv-3.0.0/build directory and run the following commands on your terminal:

$ make -j4
$ make install

In the preceding command, the -j4 flag indicates that it is using four cores to install it. It's faster this way! Now, let's set the library path. Open your ~/.profile file in your terminal using the vi ~/.profile command, and add the following line:

export DYLD_LIBRARY_PATH=/full/path/to/opencv-3.0.0/build/lib:$DYLD_LIBRARY_PATH

We need to copy the pkg-config file opencv.pc to /usr/local/lib/pkgconfig and name it opencv3.pc. This way, if you already have an existing OpenCV 2.4.x installation, there will be no conflicts. Let's go ahead and do this:

$ cp /full/path/to/opencv-3.0.0/build/lib/pkgconfig/opencv.pc /usr/local/lib/pkgconfig/opencv3.pc

We need to update our PKG_CONFIG_PATH variable as well. Open your ~/.profile file, and add the following line:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:$PKG_CONFIG_PATH

Reload your ~/.profile file using the following command:

$ source ~/.profile

We are done! Let's see if it's working:

$ cd /full/path/to/opencv-3.0.0/samples/cpp
$ g++ -ggdb 'pkg-config --cflags --libs opencv3' opencv_version.cpp -o /tmp/opencv_version && /tmp/opencv_version

If you see Welcome to OpenCV 3.0.0 printed on your terminal, you are good to go. We will be using CMake to build our OpenCV projects throughout this book. We will cover this in more detail in the next chapter.

Linux

Let's see how to install OpenCV on Ubuntu. We need to install some dependencies before we begin. Let's install them using the package manager by running the following command on your terminal:

$ sudo apt-get -y install libopencv-dev build-essential cmake libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils

Now that you have installed the dependencies, let's download, build, and install OpenCV:

$ wget "https://github.com/Itseez/opencv/archive/3.0.0.zip" -O opencv.zip
$ wget "https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip" -O opencv_contrib.zip
$ unzip opencv.zip –d .
$ unzip opencv_contrib.zip –d .
$ cd opencv-3.0.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/full/path/to/opencv-3.0.0/build -D INSTALL_C_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/full/path/to/opencv_contrib-3.0.0/modules ../
$ make –j4
$ sudo make install

Let's copy the pkg-config file's opencv.pc to /usr/local/lib/pkgconfig and name it opencv3.pc:

$ cp /full/path/to/opencv-3.0.0/build/lib/pkgconfig/opencv.pc /usr/local/lib/pkgconfig/opencv3.pc

We are done! We will now be able to use it to compile our OpenCV programs from the command line. Also, if you already have an existing OpenCV 2.4.x installation, there will be no conflicts. Let's check whether the installation is working properly:

$ cd /full/path/to/opencv-3.0.0/samples/cpp
$ g++ -ggdb 'pkg-config --cflags --libs opencv3' opencv_version.cpp -o /tmp/opencv_version && /tmp/opencv_version

If you see Welcome to OpenCV 3.0.0 printed on your terminal, you are good to go. In the following chapters, you will learn how to use CMake to build your OpenCV projects.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime