How to compile OpenCV to utilize multiple core processor in Linux


Hello there, today I am gonna tell you the story that how I was able to utilize my Core i7 (eight cores) successfully during Haartraining of samples. Usually, OpenCV can utilize only one processor core at a time which cause huge time(3-4 days, even more) to get the training results. The secret is that we are going to use OpenMP(Open Multi-Processing) instead of TBB(Threading Building Blocks) during OpenCV Compilation.

Prerequisite:

GCC and build-essentials are required to build the source.

Steps:

Step 1: Download OpenCV source from here and extract.
Step 2: Install CMake & CMake UI
For ubuntu and mint:

sudo apt-get install cmake cmake-qt-gui

For RedHat and Fedora:

sudo yum install cmake cmake-qt-gui

Step 3: Open Terminal and change Directory (cd) to OpenCV source path, create a new folder in which you want to save the configured source of OpenCV which we are going to build, for example, we have OpenCV 2.4.6.1 here extracted into ~/opencv-2.4.6.1/ and we are going to create a directory ~/opencv-2.4.6.1/build/ for the configured source

Step 4: Edit and replace the lines 49 to 58 in file ~/opencv-2.4.6.1/cmake/OpenCVFindLibsPerf.cmake, for other versions of OpenCV, open that file and search for the OpenMP part and replace the part with the following lines.

# — OpenMP —
if(NOT HAVE_TBB AND NOT HAVE_CSTRIPES)
include (FindOpenMP) # — since cmake version 2.6.3
if (OPENMP_FOUND)
set (HAVE_OPENMP TRUE)
set (CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}”)
set (CMAKE_C_FLAGS “${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}”)
else()
set ( HAVE_OPENMP FALSE)
endif()
else()
set(HAVE_OPENMP 0)
endif()

Step 5: Run CMake GUI, and Source and Build paths to ~/opencv-2.4.6.1/ and ~/opencv-2.4.6.1/build/ respectively, press Configure and check whether OpenMP is enabled or not. If OpenMP is enabled, press Generate to save the configured source to ~/opencv-2.4.6.1/build/ directory, else recheck which step you have missed.
OpenCV configuration using CMake GUI
Step 6: Now, we are going to build the configured source, cd to ~/opencv-2.4.6.1/build/, run make to build and sudo make install to install OpenCV Binaries to the system

In order to test that you can collect your own negative and positive data set, create samples and follow the tutorial here. For me, it works like wow 🙂

Utilizing all cores of a Core i7 processor during OpenCV Haar training

Thanks, bye 🙂

Advertisements