It was not very long ago (6 weeks) that I went through the process of building OpenCV from source. I had used version 2.4.6 and recently 2.4.7 has been released. The release notes show some useful improvements and I want to take better notes for the purpose of this blog. So as much for my own benefit as yours, I am going to work through this extensive build once again.
There is a binary download of OpenCV available at the opencv.org web site. But this version is not compiled to use the OpenCL libraries which I just installed, and using OpenCL is necessary to get the benefits of GPU acceleration.
Help resources
My first task with such a complex endeavor is to find help. I found exactly what I needed on the AMD site in the form of a PDF by Dr. Harris Gasparakis. Dr. Gasparakis also contributes to the AMD blog. Two recent entries are http://developer.amd.com/community/blog/2013/07/09/opencv-cl-computer-vision-with-opencl-acceleration/ and http://developer.amd.com/community/blog/2013/11/05/opencv-cl-computer-vision-with-even-more-opencl-acceleration/
Another source of installation instructions is this page at opencv.org ("DOC" for now on). As I have two complete sets of instructions which seeminly differ from each other, the first think I am going to do is compare and combine them and create what I hope will be the best merged set of steps. Since the Dr. Gasparakis document ("PDF" for now on) is more recent, better organized, and targets the same computer configuration as my own, I will use that as my main guide.
Prerequisites
The last time I built OpenCV, I hadn't prepared my system to support the Python bindings. Python has become more interesting to me since I have been using it on a Raspberry Pi, so I desired to be able to program OpenCV with Python. The instructions at opencv.org cover that topic. Since I am on 64 bit windows and there are no 64 bit builds of Numpy at www.numpy.org, I choose to install Anaconda which provides both 32 and 64 bit installations. Also, Anaconda looks like an interesting product in its own right. Perhaps I will blog on that in the future.
Other prerequisites for the OpenCV build are Visual Studio 2012 and cmake - both of which I already have installed. The DOC lists (after "OpenCV may come in multiple flavors") many other tools and libraries. The only additional tool that I installed was Qt. I installed Qt because I suspect that some of the OpenCV samples use Qt for the user interface controls. It looks like everything else is optional - I guess I'll find out.
Following the PDF Step 1, I downloaded a zip of the latest release tag (2.4.7) from https://github.com/Itseez/opencv and unzipped it on my machine to E:-2.4.7. Thus for my installation:
OPENCV_ROOT = E:\programs\opencv-2.4.7
The folder structure is show below. Under "modules/ocl" is the OpenCL implementation. OPENCV_ROOT contains the c++ wrappers of OpenCL kernels. OPENCV_ROOT contains the OpenCL kernels.

OpenCV root folders
Following the PDF, Step 2 is to download APPML, the AMD math libraries. As this was recommended, I went ahead and installed these libraries. After doing so, I see they are listed in Programs and Features. Sorted by "Publisher" I see these three AMD entries:
- AMD APP SDK 2.9
- clAmdFft
- clAmdBlas
Step 3 in the PDF is to install cmake, which I had already done during my previous experiment with OpenCV.
The Steps
Now I am ready to apply the magic of CMake. CMake is a cross-platform, open-source build system. CMake builds make files which then build libraries and executables. I have to admit that I find CMake to be intimidating - mainly due to a complete lack of understanding in how it works. Another learning project for another day.
The outline of the process of building OpenCV is as follows:
- run cmake-gui
- specify where is source and where to build, then click "configure"
- select VS2012 as generator, then click "finish"
- select/correct the available packages
- again click "configure"
- for packages where were added but caused errors, tell cmake their location
- click "configure" again to see if the values were correct and eliminated the errors
- select what parts of OpenCV are to be built
- click "configure" again
- if no errors, tell CMake to create the project files by pushing the Generate button.
- Go to the build directory and open the created OpenCV solution.
- build both the Release and the Debug binaries.
- collect the header and the binary files by building the Install project
test your build by going into the Build/bin/Debug or Build/bin/Release directory and start a couple of applications like the contours.exe
set the OpenCV env var for your system for my machine it can be done by running setx -m OPENCV_DIR D:6411
if dynamic-link libraries (dlls) were built, then add the path to these libraries tot he system PATH env var
Execution of steps
In my installation:
OPENCV_BUILT = E:\programs\opencv-2.4.7-built
Clicking "Configure" runs for about a minute and generates several pages of output. I reviewed the output to see what can be gleaned from it. I find this:
OpenCL:
Version: dynamic
Include path: E:/programs/opencv-2.4.7/3rdparty/include/opencl/1.2 C:/Program Files (x86)/AMD/clAmdFft/include C:/Program Files (x86)/AMD/clAmdBlas/include
Use AMD FFT: YES
Use AMD BLAS: YES
Python:
Interpreter: e:/programs/Anaconda/python.exe (ver 2.7.5)
Libraries: e:/programs/Anaconda/libs/python27.lib (ver 2.7.5)
numpy: e:/programs/Anaconda/lib/site-packages/numpy/core/include (ver 1.7.1)
packages path: e:/programs/Anaconda/Lib/site-packages
Java:
ant: NO
JNI: C:/Program Files/Java/jdk1.7.0_40/include C:/Program Files/Java/jdk1.7.0_40/include/win32 C:/Program Files/Java/jdk1.7.0_40/include
Java tests: NO
I believe that "dynamic" relates to this (from http://code.opencv.org/projects/opencv/wiki/ChangeLog):
Dynamic dependency on OpenCL runtime (allows run-time branching between OCL and non-OCL implementation)
Note that Blas and Fft were found, as was Python, Numpy, and Java. That all looks correct.
Next step is to review available packages and check the checkboxes of those available.
The PDF says this:
Next steps:
- If appropriate, disable WITH_CUDA, WITH_CUFFT
- Make sure that WITH_OPENCL is enabled
WITH_OPENCL was already checked WITH_CUDA was not checked but WITH_CUFFT is checked
I don't know why WITH_CUFFT was checked by default. I unchecked it and then clicked "Configure" again.
Now I am wondering if I should install Ant. I plan on tring the Java OpenCV wrappers, and Ant may be necessary, so I am going ahead and installing it. I downloaded 1.9.2 from http://ant.apache.org
After installing, I set the environment variable ANT_HOME and added %ANT_HOME% to PATH. Running ant -version gave me the error
Unable to locate tools.jar. Expected to find it in C:Files6.jar
Investingating, I found that JAVA_HOME wasn't set to my lastest JDK. After fixing that I was able to run "ant -version"
I clicked "Configure" in cmake-gui again.
It still reports "ANT: no" so I assume that it did not reload the environment variables. I quit cmake-gui and restarted. Now after running "Configure" it is finding ANT.
I checked "INSTALL_PYTHON_EXAMPLES" because I am interested in trying Python.
Note that the PDF says to check "OPENCL_ROOT_DIR". This setting is no longer present. I am assuming that with dynamic OpenCL binding that it is no longer relevant.
As per PDF, I verified that WITH_OPENCLAMDBLAS and WITH_OPENCLAMDFFT where enabled.
I checked "BUILD_EXAMPLES" and "BUILD_PERF_TESTS" and clicked "Configure" again.
All looks good so I clicked "Generate".
After doing so, I find OpenCV.sln in opencv-2.4.7-built.
I used the VS command like to run msbuild OpenCV.sln /p:Configuration=Debug and msbuild OpenCV.sln /p:Configuration=Release
For the Debug build, I got an error saying:
LINK : fatal error LNK1104: cannot open file 'python27_d.lib'
This is caused by python.h trying to be too clever about library references. See this discussion: http://stackoverflow.com/questions/11311877/creating-a-dll-from-a-wrapped-cpp-file-with-swig. Anyway, my research tells me that I can ignore this link error
The Debug and Release builds took about ten minutes each on my computer. The PDF said to "look for opencv_ocl246d.lib in the Debug folder..." but I see no such files. Again, I am assuming that is due to the change in OpenCV using dynamic linking to OpenCL.
The next step listed in the PDF is to try running some samples. I started with the face detection sample by running:
ocl-example-facedetect.exe t=OPENCV_ROOT\data\haarcascades\haarcascade_frontalface_alt.xml i=OPENCV_ROOT\samples\cpp\lena.jpg
where OPENCV_ROOT is replaced with the install path
Success! It reported this information
average GPU time (noCamera) : 44.0714 ms
accuracy value: 0
Next I tried
cpp-tutorial-cannydetector_demo.exe OPENCV_ROOT.jpg
which also worked.
I went on to try all of the OpenCL (executables beginning with "ocl-" both to check that they worked and to see what they do. There was at least one that did not work. In a subsequent blog I will review the OpenCL samples.
Next steps
My next task on this project will be to both test and to review more of the samples. In addition to the OpenCL samples there are C and C++ samples and also Java and Python samples. What I intent to learn from these samples is basically a) what can OpenCV do, b) what are the API calls used to do it, and c) which language bindings provide a good balance of ease-of-use and runtime performance.
No comments:
Post a Comment