Skip to content

Instantly share code, notes, and snippets.

@iago-suarez
Last active March 12, 2024 02:24
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save iago-suarez/13c82b416ce6b07a93b5b6eee6bd29f3 to your computer and use it in GitHub Desktop.
Save iago-suarez/13c82b416ce6b07a93b5b6eee6bd29f3 to your computer and use it in GitHub Desktop.
Setting Up OpenCL for OpenCV on Android, the full story

Setting Up OpenCL for OpenCV on Android, the full story

The tutorial Use OpenCL in Android camera preview based CV application show us how we can use the Transparent API to dramatically increase the performance of some expensive operations.

The first step in order to be able to execute the tutorial example is to re-compile opencv with the correct flags:

# Export your NDK, it will be looked for OpenCV to compile your project. In my case
export ANDROID_NDK=~/graffter/libs/android-ndk-r10d/

# Download the necessary code
cd graffter/libs/
mkdir contrib_release_armv7a && contrib_release_armv7a
git clone git@github.com:opencv/opencv.git
mv opencv opencv-3.1.0
git clone git@github.com:opencv/opencv_contrib.git

# Creating our custom OpenCL SDK
export OPENCL_SDK=~/graffter/libs/MotoX_OpenCL_SDK
mkdir $OPENCL_SDK && cd $OPENCL_SDK
mkdir lib include
# Copy the OpenCL headers used to compile OpenCV 
cp -r ~/graffter/libs/opencv-3.1.0/3rdparty/include/opencl/1.2/CL/ include/
# Download the multiplattaform version of the C++ OpenCL Wrapper
cd include/CL/ && wget https://www.khronos.org/registry/cl/api/1.2/cl.hpp && cd ../..
# Connect you OpenCL supported android device and use adb to get the library 
adb pull /system/vendor/lib/libOpenCL.so lib
# Set the folder where we are going to compile opencv, it will be used when we run the ndk-build of the project
export OPENCV_ANDROID_SDK=~/graffter/libs/contrib_release_armv7a
cd $OPENCV_ANDROID_SDK

# Now configure and compile opencv
cmake -Wno-dev \
-DCMAKE_TOOLCHAIN_FILE=../opencv-3.1.0/platforms/android/android.toolchain.cmake \
-DANDROID_ABI="armeabi-v7a with NEON" \
-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
-DBUILD_ANDROID_EXAMPLES=ON \
-DINSTALL_ANDROID_EXAMPLES=ON \
-DWITH_OPENCL=YES \
-DANDROID_OPENCL_SDK=$OPENCL_SDK \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-DANDROID_NATIVE_API_LEVEL=14 \
-DANDROID_SDK_TARGET=24 \
../opencv-3.1.0
make -j8

# Now you would be able to install the generated APK with:
adb install samples/android/tutorial-4-opencl/.build/bin/example-tutorial-4-opencl-debug.apk

Notes:

  • Here I'm compiling with the contrib modules, but if you don't need them just remove the line "-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules "
  • Here I'm only building the library for ARMv7a devices with NEON support, but you should add other architectures in the string. The full list is:

armeabi-v7a; armeabi; armeabi-v7a with NEON; armeabi-v7a-hard with NEON; armeabi-v7a with VFPV3; armeabi-v6 with VFP; arm64-v8a x86 x86_64 mips mips64"

  • -DANDROID_NATIVE_API_LEVEL=14 Is necessary because lower API_LEVEL will cause problems compiling the tutorial-4-opencl here.
@omair18
Copy link

omair18 commented May 10, 2017

Hey iago. Thank you for such a detailed and nice tutorial. I was able to compile OpenCV with OpenCL support and also tested OpenCL sample (#4). But i'm unable to port this sample on Android Studio and compile the native libraries properly. Can you share any Android Studio project which used OpenCV & OpenCL ? Or if you can guide me through this process? I'll be very thankful to you. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment