12
1 Step-by-step: Building OpenCL-enabled OpenCV from source OpenCV version: 2.4.6 Dr. Harris Gasparakis, [email protected] 1 This tutorial is a very introductory, step-by-step guide to obtaining, configuring, and building openCV with openCL enabled. While we are explicitly using a 64 bit Windows platform (Windows® 7), and Visual Studio (Visual Studio 2010), most of the steps are common on all supported platforms and compilers. We are using AMD’s OpenCL implementation (i.e. SDK) for reference, however the steps have general applicability (unless otherwise noted). Step 1: Get the source code OpenCV source can be found in github: https://github.com/Itseez/opencv Note that by default you are browsing the “master” branch. Clicking on the dropdown, 1 Dr. Harris Gasparakis is the OpenCV-CL project manager, technical lead, and evangelist. His postings are his own opinions and may not represent AMD’s positions, strategies or opinions. Links to third party sites, and references to third party trademarks, are provided for convenience and illustrative purposes only. Unless explicitly stated, AMD is not responsible for the contents of such links, and no third party endorsement of AMD or any of its products is implied.

Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

  • Upload
    lamhanh

  • View
    281

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

1

Step-by-step: Building OpenCL-enabled OpenCV from source

OpenCV version: 2.4.6

Dr. Harris Gasparakis, [email protected]

1

This tutorial is a very introductory, step-by-step guide to obtaining, configuring, and building

openCV with openCL enabled. While we are explicitly using a 64 bit Windows platform

(Windows® 7), and Visual Studio (Visual Studio 2010), most of the steps are common on all

supported platforms and compilers. We are using AMD’s OpenCL implementation (i.e. SDK)

for reference, however the steps have general applicability (unless otherwise noted).

Step 1: Get the source code

OpenCV source can be found in github: https://github.com/Itseez/opencv

Note that by default you are browsing the “master” branch. Clicking on the dropdown,

1 Dr. Harris Gasparakis is the OpenCV-CL project manager, technical lead, and evangelist. His postings are his

own opinions and may not represent AMD’s positions, strategies or opinions. Links to third party sites, and

references to third party trademarks, are provided for convenience and illustrative purposes only. Unless explicitly

stated, AMD is not responsible for the contents of such links, and no third party endorsement of AMD or any of its

products is implied.

Page 2: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

2

Exposes the option to change branches and tags:

Select tag 2.4.6.1 (the last stable release, as of this writing):

After the selecting the branch/tag you want, the easiest is to just click the “zip” button there. It

will make a zip file, and download it for you. The default name for the master branch is “opencv-

Page 3: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

3

master.zip.” The default name for tag 2.4.6.1 is opencv-2.4.6.1.zip. Extract the zip file at a

folder of your choice. This folder will be referred to as OPENCV_ROOT. In the current

example, we have chosen this directory to be:

OPENCV_ROOT=D:\opencv-2.4.6.1

The source code is platform independent, meaning that one more step is needed to create

makefiles for any particular platform (e.g. Windows or Linux). The step uses cmake, and it will

be explained later.

The directory structure below OPENCV_ROOT looks like:

Under “modules,” look for “ocl.” This is where the ocl implementation is placed. Under “src”

you will see multiple cpp files, wrapping kernel invocations. There is also a “opencl” directory

under “src,” containing the openCL kernels.

Page 4: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

4

Step 2 [optional, but recommended]: Download APPML

On AMD platforms you can (and should) enable use of clAmdBlas and clAmdFFT, which are

part of AMD’s APPML (ML stands for Math Libraries). Those are optional, you can configure,

compile and run the openCV-CL without them.

They can be downloaded from:

http://developer.amd.com/tools/heterogeneous-computing/amd-accelerated-parallel-processing-

math-libraries/

As of this writing, the last available version is 1.10.274.

It is recommended that you choose default installation paths and that you write them down for

easy reference. Also, you should ensure that the path is modified to include the APP ML dlls,

with default location, for the 64 OS, in:

C:\Program Files (x86)\AMD\clAmdBlas\bin64

C:\Program Files (x86)\AMD\clAmdFft\bin64

Page 5: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

5

Step 3: Download cmake

Download and install cmake version 2.8.9 or later from

http://www.cmake.org/

cmake controls the openCV build process, and can work on a variety of platforms and compilers.

In particular, it creates the appropriate makefiles for your platform/compiler. On Windows, with

Visual Studio installed (and selected), it will create a Visual Studio solution, and associated

projects.

Step 3: Run cmake to create makefiles specific to your OS/platform.

Run cmake. On a windows platform with visual studio installed, cmake will create visual studio

solutions and projects for you.

In the cmake gui, input the OPENCV_ROOT from above in the “where is the source code” field.

Page 6: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

6

Also, invent a directory for “where to put the binaries”. We will refer to this directory as

OPENCV_BUILT. This is where the visual studio solution will be placed.

We have selected:

OPENCV_BUILT= D:/opencv-2.4.6.1-built

Click “Configure”. After it is done (in a few secs), you will likely still have some red lines. In

cmake, you fix issues (red lines), and then you click “Configure” again, until there are no more

issues left to fix. After you are done with “Configure” you click “Generate” to actually create the

makefiles.

Next steps:

If appropriate, disable WITH_CUDA, WITH_CUFFT.

Make sure that WITH_OPENCL is enabled (which is the default in 2.4.6.1).

Press “Configure”

Page 7: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

7

Look for OPENCL_ROOT_DIR. If this line is red, then set the path to your OpenCL

SDK’s root (e.g. C:/Program Files (x86)/AMD APP), and observe that the OpenCL

include and libraries are found:

Page 8: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

8

.

Note: Older versions would allow you to set OPENCL_INCLUDE_DIR, and

OPENCL_LIBRARY directly. For example:

OPENCL_INCLUDE_DIR: C:/Program Files (x86)/AMD APP/include

OPENCL_LIBRARY: C:/Program Files (x86)/AMD APP/lib/x86_64/OpenCL.lib

Verify that, in the output window (below “Configure”), if you scroll down to “other third

party libraries”, the “use opencl” should say “yes”.

If you have installed AMD’s math libraries, make sure that WITH_OPENCLAMDBLAS,

WITH_OPENCLAMDFFT is enabled, and that the include and directory paths are set

correctly (and they are not red). If they are red, fix them appropriately and press

“Configure.”

Page 9: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

9

Finally, enable BUILD_EXAMPLES and BUILD_TESTS. Click “Configure” again.

If everything is fine, click “Generate”. You are done! Your OPENCV_BUILT directory

should now be populated, and should look like:

Page 10: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

10

Step 4: Build the source

On windows, with cmake using Visual Studio to generate code, please find openCV.sln, which

you can now open and build. Select your target (e.g. Debug build, 64 bit), and press F7, to build

the solution. Binaries and dlls are placed under OPENCV_BUILT\bin. You will see Release or

Debug subfolders in there, depending on your built configuration.

In particular, look for opencv_ocl246d.dll in the Debug built, and/or opencv_ocl246.dll in the

Release built. The size of the ocl dll is less than 5 MB.

Also, please note the OPENCV_BUILT\lib, with Debug and/or Release subdirectories. You can

find import libraries, in case you are creating projects that depend on openCV.

Page 11: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

11

In particular, look for opencv_ocl246d.lib in the Debug folder, and/or opencv_ocl246.lib in the

Release folder. The size of the ocl lib is less than 0.5 MB.

Step 5: Run some sample programs:

In Visual Studio, you can find the OpenCL samples in:

Page 12: Step-by-step: Building OpenCL-enabled OpenCV from …developer.amd.com/wordpress/media/2013/07/opencv-cl_instructions... · Step-by-step: Building OpenCL-enabled OpenCV from source

12

You can find the binaries in OPENCV_BUILT\bin\[Debug or Release]. Usually, when

you run a sample with a “-h” option, you will get a description of the command line

options needed (such as how to specify an image file, or a feature descriptor file). As an

example, run the Viola-Jones face detection sample as follows:

ocl_example-facedetect.exe

t=[OPENCV_ROOT]\data\haarcascades\haarcascade_frontalface_alt.xml

i=[OPENCV_ROOT]\samples\cpp\lena.jpg