Skip to content

Instantly share code, notes, and snippets.

@Red-Eyed
Forked from iago-suarez/opencv-opencl-android.md
Created September 13, 2022 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Red-Eyed/215da1a51a78b334c59d2e4aa9573219 to your computer and use it in GitHub Desktop.
Save Red-Eyed/215da1a51a78b334c59d2e4aa9573219 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment