Compiling and using the OpenCV for Android SDK with the OpenCL module

Source: Internet
Author: User
<span id="Label3"></p><p><p>OpenCV the static, dynamic libraries provided in the Android SDK do not support OpenCL acceleration, if you call opencl-related functions in the program, compile without error, but Run-time Logcat will output the following information, indicating that the OpenCL function is not available</p></p><pre class="brush:cpp;gutter:true;"><pre class="brush:cpp;gutter:true;">08-11 11:43:07.140:e/cv::error () (18198): OpenCV error:unknown error code-6 (OpenCL function is not available: [CLGETPLA Tformids]) in void* opencl_check_fn (int), file/hdd2/buildbot/slaves/slave_ardbeg1/50-sdk/opencv/modules/ocl/src/cl _runtime/cl_runtime.cpp, line 8308-11 11:43:07.140:e/cv::error () (18198): OpenCV error:unknown error code-221 (OpenCL No T available) in static cv::ocl::contextimpl* cv::ocl::contextimpl::getcontext (), file/hdd2/buildbot/slaves/slave_ ardbeg1/50-sdk/opencv/modules/ocl/src/cl_context.cpp, Line 678</pre></pre><p><p>therefore, If you want to use the OCL module in OPENCV for android, you need to compile a set of OpenCV library files that support opencl, as well as an opencl-driven device. The entire process is described in detail below, the entire compilation process is based on the Windows platform, and the other platform processes are similar, but the commands at compile time are Different.</p></p><p><p></p></p><p><p>1. How to determine if the device supports OpenCL</p></p><p><p>You can download a piece of software called OPENCL-Z. After installation, it detects whether the GPU of the current device supports OPENCL and whether it has an OpenCL driver. The following are on Pea pods:</p></p><p><p>Http://www.wandoujia.com/apps/com.robertwgh.opencl_z_android</p></p><p><p></p></p><p><p>2. Compiling OPENCV for Android with OpenCL module</p></p><p><p>(1) first need to download the source code from the OPENCV official website, I am based on the 2.4.11 version of the source code compiled (3.x version of the overall structure of the project is too large, and a separate OCL module is integrated into other modules, I have not figured out how to use).</p></p><p><p>This URL can be downloaded to 2.4.11 version of Source: https://github.com/Itseez/opencv/releases</p></p><p><p>(2) next Configure the required tools, there are two main: cmake,android Ndk. Download & install, Configure the CMake bin directory in the Environment variable, and add the Ndk's root directory as the ANDROID_NDK Value.</p></p><p><p>(3) in the Opencv_path\modules\ocl\src\cl_runtime\cl_runtime.cpp file, make the following changes:</p></p><p><p>Line 48th, #if defined (__linux__) instead of #if defined (__linux__) &&!defined (__android__)</p></p><p><p>After line 70th, Add the following code:</p></p><p><p>  </p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#if</span></span>Defined (__android__)<span style="color: #000000;"><span style="color: #000000;">#include</span></span><dlfcn.h><span style="color: #000000;"><span style="color: #000000;">#include</span></span><sys/stat.h><span style="color: #0000ff;"><span style="color: #0000ff;">#if</span></span>Defined (__arm_arch_8a__) | | Defined (_x64_)<span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*default_so_paths[] =<span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/system/lib64/libopencl.so</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/system/vendor/lib64/libopencl.so</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/system/vendor/lib64/egl/libgles_mali.so</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;"> };</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*default_so_paths[] =<span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/system/lib/libopencl.so</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/system/vendor/lib/libopencl.so</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">/system/vendor/lib/egl/libgles_mali.so</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;"> };</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#endif</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Access_file (<span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*<span style="color: #000000;"><span style="color: #000000;">Filename) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">struct</span></span><span style="color: #000000;"><span style="color: #000000;">Stat buffer; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>(stat (filename, &buffer) = =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>* GetProcAddress (<span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*<span style="color: #000000;"><span style="color: #000000;">Name) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>* H =<span style="color: #000000;"><span style="color: #000000;">NULL; unsigned</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">i; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">H) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*<span style="color: #000000;"><span style="color: #000000;">name; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(i=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i< (<span style="color: #0000ff;"><span style="color: #0000ff;">sizeof</span></span>(default_so_paths)/<span style="color: #0000ff;"><span style="color: #0000ff;">sizeof</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*)); i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(access_file (default_so_paths[i])) {name</span></span>= (<span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>*<span style="color: #000000;"><span style="color: #000000;">) default_so_paths[i]; H</span></span>=<span style="color: #000000;"><span style="color: #000000;">Dlopen (name, rtld_lazy); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(h)<span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">H)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">NULL; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">Dlsym (h, name); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Cv_cl_get_proc_address (name) GetProcAddress (name)<span style="color: #0000ff;"><span style="color: #0000ff;">#endif</span></span></pre><p><p>(4) Compile Opencv. Open cmd, Switch The current directory to opencv_path\platforms\ below, and execute the following command:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #000000;">mkdir Build_opencl</span> <span style="color: #008080;">2</span> <span style="color: #000000;">CD Build_opencl</span> <span style="color: #008080;">3</span> <span style="color: #800000;">"</span> <span style="color: #800000;">MinGW makefiles</span> <span style="color: #800000;">"</span> -dcmake_build_with_install_rpath=on-dwith_eigen=off-dcmake_toolchain_file=. \android\android.toolchain.cmake-dcmake_make_program=<span style="color: #800000;">"</span><span style="color: #800000;">%android_ndk%\prebuilt\windows-x86_64\bin\ Make.exe</span><span style="color: #800000;">"</span> -dandroid_abi=armeabi. \..</pre></pre><p><p>Where ANDROID_NDK is the variable value set in the environment variable, for the ANDROID ndk root directory, Dandroid_abi can be selected as Needed. Once executed, execute the following command:</p></p><pre><pre>Cmake--build.</pre></pre><p><p>Notice the "." in the back of the BUILD. Not less.</p></p><p><p>(5) after compiling, \build_opencl\lib\armeabi the following libraries to the corresponding folders in the Sdk\native\libs directory in the OpenCV android Sdk.</p></p><p><p>(6) Clean the original project, then build Project. Run on the device, everything Ok.</p></p><p><p>Here is the source of the test project, you need to use your own image path to replace the code path, and then the app will generate a grayscale image in the same directory. For example: the input image is a.jpg under the device root, and agray.jpg grayscale image will be generated in the same directory after running.</p></p><p><p>Compiling and using the OpenCV for Android SDK with the OpenCL module</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.