SlideShare a Scribd company logo
©SIProp Project, 2006-2008 1 
How to Build & Use OpenCL 
on OpenCV & Android NDK 
Noritsuna Imamura 
noritsuna@siprop.org
©SIProp Project, 2006-2008 2 
!!!!!!ATTENTION!!!!!! 
This manual is “How to Build” manual. 
If you want to “Use OpenCL on OpenCV” ONLY, 
Please see 
http://github.com/noritsuna/OpenCVwithOpenCL4 
AndroidNDKSample
How to Build OpenCL on OpenCV 
©SIProp Project, 2006-2008 3 
for Android NDK
©SIProp Project, 2006-2008 4 
Download OpenCV & NDK tool 
› wget 
https://github.com/Itseez/opencv/archive/ 
2.4.10.1tar.gz 
› tar zxfv 2.4.10.1.tar.gz 
› mkdir ~/android/ 
› cd ~/android/ 
› wget http://dl.google.com/android/android-sdk_ 
r23.0.2-linux.tgz 
› wget 
http://dl.google.com/android/ndk/android-ndk- 
r10c-linux-x86_64.bin 
› tar zxfv android-sdk_r23.0.2-linux.tgz 
› ./android-ndk-r10c-linux-x86_64.bin
©SIProp Project, 2006-2008 5 
Edit Setup File 
./opencv- 
2.4.10.1/platforms/scripts/cmake_android_arm.s 
h 
Add WITH_EIGEN=off option. 
Currently version has some bugs. 
› cmake -DWITH_EIGEN=OFF - 
DCMAKE_BUILD_WITH_INSTALL_RPATH=ON - 
DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.c 
make $@ ../..
©SIProp Project, 2006-2008 6 
Set Path to SDK/NDK 
› export ANDROID_NDK=~/android/android-ndk- 
r10c 
› export ANDROID_SDK=~/android/android-sdk- 
linux
Add “SO loading“ func for Android 1/3 
Change “SO loading“ func for Linux to this func 
without Android. 
~/opencv- 
2.4.10.1/modules/ocl/src/cl_runtime/cl_runtime.cpp 
©SIProp Project, 2006-2008 7 
› #if defined(__linux__) 
↓ 
› #if defined(__linux__) 
&& !defined(__ANDROID__)
Add “SO loading“ func for Android 2/3 
Add “SO loading“ func for Android. (continue to 
next page) 
~/opencv- 
2.4.10.1/modules/ocl/src/cl_runtime/cl_runtime.cpp 
©SIProp Project, 2006-2008 8 
› #if defined(__ANDROID__) 
› #include <dlfcn.h> 
› #include <sys/stat.h> 
› #if defined(__ARM_ARCH_8A__) || defined(_X64_) 
› static const char *default_so_paths[] = { 
› "/system/lib64/libOpenCL.so", 
› "/system/vendor/lib64/libOpenCL.so", 
› "/system/vendor/lib64/egl/libGLES_mali.so" 
› }; 
› #else 
› static const char *default_so_paths[] = { 
› "/system/lib/libOpenCL.so", 
› "/system/vendor/lib/libOpenCL.so", 
› "/system/vendor/lib/egl/libGLES_mali.so" 
› }; 
› #endif 
›
Add “SO loading“ func for Android 3/3 
©SIProp Project, 2006-2008 9 
› static int access_file(const char *filename) 
› { 
› struct stat buffer; 
› return (stat(filename, &buffer) == 0); 
› } 
› static void* GetProcAddress (const char* name) 
› { 
› static void* h = NULL; 
› unsigned int i; 
› if (!h) 
› { 
› const char* name; 
› for(i=0; i<(sizeof(default_so_paths)/sizeof(char*)); i++) 
› { 
› if(access_file(default_so_paths[i])) { 
› name = (char *)default_so_paths[i]; 
› h = dlopen(name, RTLD_LAZY); 
› if (h) break; 
› } 
› } 
› if (!h) 
› return NULL; 
› } 
› return dlsym(h, name); 
› } 
› #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name) 
› #endif
› cd ~/opencv-2.4.10.1/platforms/scripts/ 
› ./cmake_android_arm.sh 
©SIProp Project, 2006-2008 10 
Execute Auto Config & make 
This Script make Makefile for OpenCV. 
In “~/opencv- 
2.4.10.1/platforms/scripts/build_android_arm.sh” 
Make !!! 
› cd ~/opencv- 
2.4.10.1/platforms/scripts/build_android_arm 
/ 
› make
©SIProp Project, 2006-2008 11 
Where are Made Files? 
OpenCV Libs. 
In “~/opencv- 
2.4.10.1/platforms/build_android_arm/lib” 
highgui 
core 
etc... 
3rd Party Libs for OpenCV. 
In “~/opencv- 
2.4.10.1/platforms/build_android_arm/3rdparty/lib” 
libIlmImf 
libjasper 
libjpeg 
libpng 
libtiff
©SIProp Project, 2006-2008 12 
Copy libs to sample program 
OpenCV Libs. 
cp ~/opencv- 
2.4.10.1/platforms/build_android_arm/lib/armeabi-v7a/* 
~/opencv-2.4.10.1/samples/android/native-activity/ 
libs_opencv/ 
3rd Party Libs for OpenCV. 
cp ~/opencv- 
2.4.10.1/platforms/build_android_arm/3rdparty/lib/ar 
meabi-v7a/* ~/opencv- 
2.4.10.1/samples/android/native-activity/ 
libs_opencv/
How to Use OpenCL on OpenCV 
for Android NDK(Native App) 
©SIProp Project, 2006-2008 13
©SIProp Project, 2006-2008 14 
Download Sample Source Code 
http://github.com/noritsuna/OpenCVwithOpenCL4Andr 
oidNDKSample 
Based OpenCV-2.4.10.1 & Android 4.4 
Made Android Native Activity. 
Convert RGB Color Image to Black&White Image.
Call Stack 
Call as C/C++ APIs 
Call as SysCall(C/ASM) 
©SIProp Project, 2006-2008 15 
Native Application 
NDK wo/ADT 
Standard Android 
Application for C/C++ 
Only C/C++ on Limited 
Library Layer 
Advantage 
Only C/C++ 
DirectCall C/C++ API 
Dis-Advantage 
Use a few Android Tools 
A few Docs from Google 
Developer Site & Blogs 
APK File(Your Application) 
(C/C++) 
Library Layer 
(C/C++) 
Kernel/Driver Layer 
(C/ASM)
How to Make this Sample Source Code 
from OpenCV’s Sample Project 
©SIProp Project, 2006-2008 16
©SIProp Project, 2006-2008 17 
Use NativeActivity of OpenCV 
This Sample uses Only C/C++ Code. 
~/opencv-2.4.10.1/samples/android/native-activity 
Structure of NativeActivity 
AndroidManifest.xml <- Property File for Android 
res <- Directory that has Resource file(Image,Voice) 
jni <- Directory that has files for NDK 
Android.mk <- make file of NDK 
Application.mk <- property file of NDK 
native.cpp <- Sample App 
Other Files/Dir, don’t use.
©SIProp Project, 2006-2008 18 
Edit AndroidManifest.xml 
Replace “application” Section 
1. <application android:label="@string/app_name" 
android:hasCode="false"> 
2. <activity android:name="android.app.NativeActivity" 
3. android:label="@string/app_name" 
4. 
android:configChanges="orientation|keyboardHidden"> 
5. <!-- Tell NativeActivity the name of or .so --> 
6. <meta-data android:name="android.app.lib_name" 
7. android:value="native_activity" /> 
8. <intent-filter> 
9. <action 
android:name="android.intent.action.MAIN" /> 
10. <category 
android:name="android.intent.category.LAUNCHER" /> 
11. </intent-filter> 
12. </activity> 
13. </application>
2. <uses-sdk android:minSdkVersion=“9" /> 
©SIProp Project, 2006-2008 19 
About AndroidManifest.xml 
API Level 
1. <!-- This is the platform API where 
NativeActivity was introduced. --> 
Permission 
1. <uses-permission 
android:name="android.permission.CAMERA 
"/>
©SIProp Project, 2006-2008 20 
Create jni/Android.mk 1/9 
1. Delete jni/Android.mk & Create the new 
jni/Android.mk 
2. Set LOCAL_PATH & include 
1. LOCAL_PATH := $(call my-dir) 
2. include $(CLEAR_VARS)
©SIProp Project, 2006-2008 21 
Create jni/Android.mk 2/9 
1. Set OpenCV’s Modules & Libs 
1. OPENCV_MODULES:=contrib legacy ml 
stitching superres objdetect ts videostab 
video photo calib3d features2d highgui 
imgproc flann ocl core androidcamera 
2. OPENCV_3RDPARTY_COMPONENTS:=asse 
tmanager tbb libjpeg libpng libtiff libjasper 
IlmImf 
3. OPENCV_CAMERA_MODULES:= 
native_camera_r2.2.0 native_camera_r2.3.3 
native_camera_r3.0.1 native_camera_r4.0.0 
native_camera_r4.0.3 native_camera_r4.1.1 
native_camera_r4.2.0 native_camera_r4.3.0 
native_camera_r4.4.0
LOCAL_SRC_FILES:=../libs_opencv/libopenc 
v_$1.a 
5. include $(PREBUILT_STATIC_LIBRARY) 
6. endef 
©SIProp Project, 2006-2008 22 
Create jni/Android.mk 3/9 
Set Loading OpenCV Libs Function 
1. define add_opencv_module 
2. include $(CLEAR_VARS) 
3. LOCAL_MODULE:=opencv_$1 
4. 
1. $(foreach 
module,$(OPENCV_MODULES),$(eval $(call 
add_opencv_module,$(module))))
1. define add_opencv_3rdparty_component 
2. include $(CLEAR_VARS) 
3. LOCAL_MODULE:=$1 
4. 
LOCAL_SRC_FILES:=../libs_opencv/lib$1.a 
5. include $(PREBUILT_STATIC_LIBRARY) 
6. endef 
©SIProp Project, 2006-2008 23 
Create jni/Android.mk 4/9 
Set Loading OpenCV Libs Function 
1. $(foreach 
module,$(OPENCV_3RDPARTY_COMPONEN 
TS),$(eval $(call 
add_opencv_3rdparty_component,$(module))))
LOCAL_SRC_FILES:=../libs_opencv/lib$1.so 
5. include $(PREBUILT_SHARED_LIBRARY) 
6. endef 
©SIProp Project, 2006-2008 24 
Create jni/Android.mk 5/9 
Set Loading AndroidCamera Libs Function 
1. define add_opencv_camera_module 
2. include $(CLEAR_VARS) 
3. LOCAL_MODULE:=$1 
4. 
1. $(foreach 
module,$(OPENCV_CAMERA_MODULES),$(e 
val $(call 
add_opencv_camera_module,$(module))))
©SIProp Project, 2006-2008 25 
Create jni/Android.mk 6/9 
Set loading info lib 
1. include $(CLEAR_VARS) 
2. LOCAL_MODULE:=opencv_info 
3. LOCAL_SRC_FILES:=../libs_opencv/libopenc 
v_info.so 
4. include $(PREBUILT_SHARED_LIBRARY)
©SIProp Project, 2006-2008 26 
Create jni/Android.mk 7/9 
Set Include Dir 
Set Module name & Source Code 
OpenCVwithOpenCL4AndroidNDKNativeActivity.cpp 
1. include $(CLEAR_VARS) 
2. LOCAL_C_INCLUDES+=./jni/include 
3. LOCAL_MODULE := OpenCV_CL 
4. LOCAL_SRC_FILES := 
OpenCVwithOpenCL4AndroidNDKNativeActi 
vity.cpp
©SIProp Project, 2006-2008 27 
Create jni/Android.mk 8/9 
Set Loading Lib & OpenCV Lib 
LOCAL_LDLIBS 
-lm -llog -lc -ldl -lz -landroid 
LOCAL_STATIC_LIBRARIES 
$(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) 
$(OPENCV_3RDPARTY_COMPONENTS) 
1. LOCAL_LDLIBS += -lm -llog -lc -ldl -lz - 
landroid 
2. LOCAL_STATIC_LIBRARIES := 
android_native_app_glue $(foreach mod, 
$(OPENCV_MODULES), opencv_$(mod)) 
$(OPENCV_3RDPARTY_COMPONENTS)
©SIProp Project, 2006-2008 28 
Create jni/Android.mk 9/9 
Set Library Type 
Make SO lib 
BUILD_SHARED_LIBRARY 
Set Loading Android module 
Use “native_app_glue” module 
$(call import-module,android/native_app_glue) 
1. include $(BUILD_SHARED_LIBRARY) 
2. $(call import-module, 
android/native_app_glue)
©SIProp Project, 2006-2008 29 
Setup/Make Build.xml 1/2 
Making Setting File for Android using ANT 
Today, Development on CUI Only!!! 
Don’t use Eclipse! 
› android list target 
› Available Android targets: 
› ---------- 
› id: 1 or "android-9" 
› Name: Android 2.3.1 
› Type: Platform 
› API level: 9 
› Revision: 2 
› Skins: WQVGA400, QVGA, HVGA, WVGA800 (default), WVGA854 
› ABIs : armeabi 
› ---------- 
› id: 2 or "android-18" 
› Name: Android 4.3 
› Type: Platform 
› API level: 18 
› Revision: 2 
› Skins: WXGA800, WQVGA400, QVGA, WQVGA432, WXGA800-7in, WVGA800 
(default), HVGA, WXGA720, WSVGA, WVGA854 
› ABIs : armeabi-v7a 
› ----------
©SIProp Project, 2006-2008 30 
Setup/Make Build.xml 2/2 
Make Build.xml 
› android update project --path . --target 2
©SIProp Project, 2006-2008 31 
Build NativeActivity 
Build C/C++ Source Codes 
› ndk-build [clean all] 
Make APK File 
› ant [debug|release]
› adb install bin/NativeActivity-debug.apk 
› adb uninstall com.example.native_activity 
©SIProp Project, 2006-2008 32 
Install NativeActivity.apk 
Install APK File 
UnInstall APK File 
Show Log 
› adb logcat
©SIProp Project, 2006-2008 33 
adb(Android Debug Bridge) 
Functions 
Shell on Android Device 
adb shell 
Show Connected Android Devices 
adb devices 
Install/Uninstall APK File 
adb install/uninstall [App Name]/[Package Name] 
Send/Get File to/from Android Device 
adb push/pull [from File Path] [to File Path] 
Show Log (DDMS) 
adb logcat
©SIProp Project, 2006-2008 34 
Quick Explanation to OpenCL 
1. void android_main(android_app* app) { 
2. (snip) 
3. cv::Mat drawing_frame; 
4. (snip) 
5. colorSampleImage = imread(file_path); 
6. (snip) 
7. // Create Mat Object for OpenCL 
8. cv::ocl::oclMat oclIn(colorSampleImage), oclOut; 
9. // Change Color RGB->Black&White on OpenCL 
10. cv::ocl::cvtColor(oclIn, oclOut, cv::COLOR_BGR2GRAY); 
11. // Download Mat Object for OpenCL to Mat for CPU 
12. oclOut.download(drawing_frame); 
13. 
14. engine_draw_frame(&engine, drawing_frame);
How to Set up OpenCL Driver 
adreno-3xx: Qualcomm's Adreno 3xx GPUs 
©SIProp Project, 2006-2008 35 
mali-t6xx: ARM's Mali-T6xx GPUs
©SIProp Project, 2006-2008 36 
Download OpenCL Drivers 
OpenCL Drivers was deleted since 4.2 
adreno-3xx: Qualcomm's Adreno 3xx GPUs 
mali-t6xx: ARM's Mali-T6xx GPUs 
Download from: 
https://github.com/madeye/opencl-android-proprietary
©SIProp Project, 2006-2008 37 
Set up Drivers to your Android 
Ex. adreno-3xx: Qualcomm's Adreno 3xx GPUs 
1. $ adb push kitkat/system/vendor/lib /sdcard/ 
2. $ adb shell 
3. $ su 
4. # mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 
/system 
5. # cp /sdcard/libOpenCL.so /system/vendor/lib/ 
6. # cp /sdcard/libllvm-a3xx.so /system/vendor/lib/ 
7. # chmod 644 /system/vendor/lib/libOpenCL.so 
8. # chmod 644 /system/vendor/lib/libllvm-a3xx.so

More Related Content

What's hot (20)

PPTX
Introduction Linux Device Drivers
NEEVEE Technologies
 
PDF
Design and Concepts of Android Graphics
National Cheng Kung University
 
PDF
Character drivers
pradeep_tewani
 
PDF
Flutter101
인수 장
 
PDF
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
Shinya Takamaeda-Y
 
PDF
LCU14 302- How to port OP-TEE to another platform
Linaro
 
PDF
X86 assembly nasm syntax
Francesco DiFusco
 
PDF
from Binary to Binary: How Qemu Works
Zhen Wei
 
PPTX
Static partitioning virtualization on RISC-V
RISC-V International
 
PDF
Embedded Android : System Development - Part I
Emertxe Information Technologies Pvt Ltd
 
PDF
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Opersys inc.
 
PPTX
epoll() - The I/O Hero
Mohsin Hijazee
 
TXT
OPTEE on QEMU - Build Tutorial
Dalton Valadares
 
PPTX
Introduction to Qt
Puja Pramudya
 
PDF
Ninja Build: Simple Guide for Beginners
Chang W. Doh
 
PDF
AndroidとSELinux
android sola
 
PPT
Android booting sequece and setup and debugging
Utkarsh Mankad
 
PDF
Embedded Android : System Development - Part II (HAL)
Emertxe Information Technologies Pvt Ltd
 
PDF
Pune Flutter Presents - Flutter 101
Arif Amirani
 
Introduction Linux Device Drivers
NEEVEE Technologies
 
Design and Concepts of Android Graphics
National Cheng Kung University
 
Character drivers
pradeep_tewani
 
Flutter101
인수 장
 
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
Shinya Takamaeda-Y
 
LCU14 302- How to port OP-TEE to another platform
Linaro
 
X86 assembly nasm syntax
Francesco DiFusco
 
from Binary to Binary: How Qemu Works
Zhen Wei
 
Static partitioning virtualization on RISC-V
RISC-V International
 
Embedded Android : System Development - Part I
Emertxe Information Technologies Pvt Ltd
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Opersys inc.
 
epoll() - The I/O Hero
Mohsin Hijazee
 
OPTEE on QEMU - Build Tutorial
Dalton Valadares
 
Introduction to Qt
Puja Pramudya
 
Ninja Build: Simple Guide for Beginners
Chang W. Doh
 
AndroidとSELinux
android sola
 
Android booting sequece and setup and debugging
Utkarsh Mankad
 
Embedded Android : System Development - Part II (HAL)
Emertxe Information Technologies Pvt Ltd
 
Pune Flutter Presents - Flutter 101
Arif Amirani
 

Viewers also liked (20)

PPTX
OpenCV acceleration battle:OpenCL on Firefly-RK3288(MALI-T764) vs. FPGA on Ze...
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PDF
Introduction to OpenCV
Luigi De Russis
 
PDF
Looking into the past - feature extraction from historic maps using Python, O...
James Crone
 
PDF
Introduction to OpenCV with python (at taichung.py)
Max Lai
 
PPTX
OpenCV 에서 OpenCL 살짝 써보기
Seunghwa Song
 
PDF
Using openCV 3.1.0 with vs2015
Wei-Wen Hsu
 
PDF
Using openCV 2.0 with Dev C++
Wei-Wen Hsu
 
PDF
OpenCV 3.0 - Latest news and the Roadmap
Eugene Khvedchenya
 
PDF
Face Recognition with OpenCV and scikit-learn
Shiqiao Du
 
PDF
OpenCV Workshop
Lentin Joseph
 
PPTX
PYNQ 祭り: Pmod のプログラミング
ryos36
 
PDF
OpenCV Introduction
Zachary Blair
 
PPTX
PYNQで○○してみた!
aster_ism
 
PDF
PYNQ祭り
Mr. Vengineer
 
PDF
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
Edge AI and Vision Alliance
 
ODP
Image Processing with OpenCV
debayanin
 
PPTX
Computer Vision, Deep Learning, OpenCV
Farshid Pirahansiah
 
PDF
Pynqでカメラ画像をリアルタイムfastx コーナー検出
marsee101
 
OpenCV acceleration battle:OpenCL on Firefly-RK3288(MALI-T764) vs. FPGA on Ze...
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
Introduction to OpenCV
Luigi De Russis
 
Looking into the past - feature extraction from historic maps using Python, O...
James Crone
 
Introduction to OpenCV with python (at taichung.py)
Max Lai
 
OpenCV 에서 OpenCL 살짝 써보기
Seunghwa Song
 
Using openCV 3.1.0 with vs2015
Wei-Wen Hsu
 
Using openCV 2.0 with Dev C++
Wei-Wen Hsu
 
OpenCV 3.0 - Latest news and the Roadmap
Eugene Khvedchenya
 
Face Recognition with OpenCV and scikit-learn
Shiqiao Du
 
OpenCV Workshop
Lentin Joseph
 
PYNQ 祭り: Pmod のプログラミング
ryos36
 
OpenCV Introduction
Zachary Blair
 
PYNQで○○してみた!
aster_ism
 
PYNQ祭り
Mr. Vengineer
 
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
Edge AI and Vision Alliance
 
Image Processing with OpenCV
debayanin
 
Computer Vision, Deep Learning, OpenCV
Farshid Pirahansiah
 
Pynqでカメラ画像をリアルタイムfastx コーナー検出
marsee101
 
Ad

Similar to How to Build & Use OpenCL on OpenCV & Android NDK (20)

PPTX
OpenCV @ Droidcon 2012
Wingston
 
PDF
android_project
Adit Ghosh
 
ODP
OpenCV 2.2.0 for Android
Picker Weng
 
DOC
Video optimization for android - Part I
Yoss Cohen
 
PPTX
Using the android ndk - DroidCon Paris 2014
Paris Android User Group
 
PDF
NDK Programming in Android
Arvind Devaraj
 
PPTX
Android NDK Intro
Giles Payne
 
PDF
Android ndk
Kuban Dzhakipov
 
PDF
Android Native Development Kit
Peter R. Egli
 
PPTX
Getting started with the NDK
Kirill Kounik
 
PDF
Introduction to the Android NDK
BeMyApp
 
PPTX
Guide: How to Build OpenCV 3.0.0
André Moreira
 
PDF
Using the Android Native Development Kit (NDK)
DroidConTLV
 
PPTX
Android ndk - Introduction
Rakesh Jha
 
PDF
Using the Android Native Development Kit (NDK)
Xavier Hallade
 
PDF
Running native code on Android #OSDCfr 2012
Cédric Deltheil
 
PDF
Lecture02web 140phpapp01
letuan9999
 
PPTX
Android ndk
Sentinel Solutions Ltd
 
PDF
An Introduction To Android
natdefreitas
 
PDF
DLL Design with Building Blocks
Max Kleiner
 
OpenCV @ Droidcon 2012
Wingston
 
android_project
Adit Ghosh
 
OpenCV 2.2.0 for Android
Picker Weng
 
Video optimization for android - Part I
Yoss Cohen
 
Using the android ndk - DroidCon Paris 2014
Paris Android User Group
 
NDK Programming in Android
Arvind Devaraj
 
Android NDK Intro
Giles Payne
 
Android ndk
Kuban Dzhakipov
 
Android Native Development Kit
Peter R. Egli
 
Getting started with the NDK
Kirill Kounik
 
Introduction to the Android NDK
BeMyApp
 
Guide: How to Build OpenCV 3.0.0
André Moreira
 
Using the Android Native Development Kit (NDK)
DroidConTLV
 
Android ndk - Introduction
Rakesh Jha
 
Using the Android Native Development Kit (NDK)
Xavier Hallade
 
Running native code on Android #OSDCfr 2012
Cédric Deltheil
 
Lecture02web 140phpapp01
letuan9999
 
An Introduction To Android
natdefreitas
 
DLL Design with Building Blocks
Max Kleiner
 
Ad

More from Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院) (20)

PPTX
オープンソース界隈の利用者や技術者から見たオープンソースEDAとは? What is open source EDA from the perspecti...
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
ラズパイを使って作品を作ったらラズパイコンテストでKSY賞を貰って、さらに、文化庁メディア芸術祭で審査員推薦作品に選ばれてしまった件〜自作チップでラズパイ...
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
The world of open source semiconductors has arrived
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
What is the world where you can make your own semiconductors?
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
半導体製造(TinyTapeout)に挑戦しよう!
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
Kernel/VMレイヤーを自分色に染める!By ISHI会
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
Principle Representation of The 8 Qubits Quantum Computer by RaspberryPi
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
The Self-Contained SDR Satellite Grand Station with Raspberry Pi 3
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
Self‐Contained SDR Grand Station with Raspberry Pi 3
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
衛星追尾用パラボラアンテナ建設記
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
All list of the measuring machines for microwave
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
PPTX
5000円で誰でも作れる新世代衛星地上局
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
オープンソース界隈の利用者や技術者から見たオープンソースEDAとは? What is open source EDA from the perspecti...
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
ラズパイを使って作品を作ったらラズパイコンテストでKSY賞を貰って、さらに、文化庁メディア芸術祭で審査員推薦作品に選ばれてしまった件〜自作チップでラズパイ...
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
What is the world where you can make your own semiconductors?
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
Kernel/VMレイヤーを自分色に染める!By ISHI会
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
Principle Representation of The 8 Qubits Quantum Computer by RaspberryPi
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
The Self-Contained SDR Satellite Grand Station with Raspberry Pi 3
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
Self‐Contained SDR Grand Station with Raspberry Pi 3
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 
5000円で誰でも作れる新世代衛星地上局
Industrial Technology Research Institute (ITRI)(工業技術研究院, 工研院)
 

Recently uploaded (20)

PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
July Patch Tuesday
Ivanti
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
July Patch Tuesday
Ivanti
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 

How to Build & Use OpenCL on OpenCV & Android NDK

  • 1. ©SIProp Project, 2006-2008 1 How to Build & Use OpenCL on OpenCV & Android NDK Noritsuna Imamura [email protected]
  • 2. ©SIProp Project, 2006-2008 2 !!!!!!ATTENTION!!!!!! This manual is “How to Build” manual. If you want to “Use OpenCL on OpenCV” ONLY, Please see http://github.com/noritsuna/OpenCVwithOpenCL4 AndroidNDKSample
  • 3. How to Build OpenCL on OpenCV ©SIProp Project, 2006-2008 3 for Android NDK
  • 4. ©SIProp Project, 2006-2008 4 Download OpenCV & NDK tool › wget https://github.com/Itseez/opencv/archive/ 2.4.10.1tar.gz › tar zxfv 2.4.10.1.tar.gz › mkdir ~/android/ › cd ~/android/ › wget http://dl.google.com/android/android-sdk_ r23.0.2-linux.tgz › wget http://dl.google.com/android/ndk/android-ndk- r10c-linux-x86_64.bin › tar zxfv android-sdk_r23.0.2-linux.tgz › ./android-ndk-r10c-linux-x86_64.bin
  • 5. ©SIProp Project, 2006-2008 5 Edit Setup File ./opencv- 2.4.10.1/platforms/scripts/cmake_android_arm.s h Add WITH_EIGEN=off option. Currently version has some bugs. › cmake -DWITH_EIGEN=OFF - DCMAKE_BUILD_WITH_INSTALL_RPATH=ON - DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.c make $@ ../..
  • 6. ©SIProp Project, 2006-2008 6 Set Path to SDK/NDK › export ANDROID_NDK=~/android/android-ndk- r10c › export ANDROID_SDK=~/android/android-sdk- linux
  • 7. Add “SO loading“ func for Android 1/3 Change “SO loading“ func for Linux to this func without Android. ~/opencv- 2.4.10.1/modules/ocl/src/cl_runtime/cl_runtime.cpp ©SIProp Project, 2006-2008 7 › #if defined(__linux__) ↓ › #if defined(__linux__) && !defined(__ANDROID__)
  • 8. Add “SO loading“ func for Android 2/3 Add “SO loading“ func for Android. (continue to next page) ~/opencv- 2.4.10.1/modules/ocl/src/cl_runtime/cl_runtime.cpp ©SIProp Project, 2006-2008 8 › #if defined(__ANDROID__) › #include <dlfcn.h> › #include <sys/stat.h> › #if defined(__ARM_ARCH_8A__) || defined(_X64_) › static const char *default_so_paths[] = { › "/system/lib64/libOpenCL.so", › "/system/vendor/lib64/libOpenCL.so", › "/system/vendor/lib64/egl/libGLES_mali.so" › }; › #else › static const char *default_so_paths[] = { › "/system/lib/libOpenCL.so", › "/system/vendor/lib/libOpenCL.so", › "/system/vendor/lib/egl/libGLES_mali.so" › }; › #endif ›
  • 9. Add “SO loading“ func for Android 3/3 ©SIProp Project, 2006-2008 9 › static int access_file(const char *filename) › { › struct stat buffer; › return (stat(filename, &buffer) == 0); › } › static void* GetProcAddress (const char* name) › { › static void* h = NULL; › unsigned int i; › if (!h) › { › const char* name; › for(i=0; i<(sizeof(default_so_paths)/sizeof(char*)); i++) › { › if(access_file(default_so_paths[i])) { › name = (char *)default_so_paths[i]; › h = dlopen(name, RTLD_LAZY); › if (h) break; › } › } › if (!h) › return NULL; › } › return dlsym(h, name); › } › #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name) › #endif
  • 10. › cd ~/opencv-2.4.10.1/platforms/scripts/ › ./cmake_android_arm.sh ©SIProp Project, 2006-2008 10 Execute Auto Config & make This Script make Makefile for OpenCV. In “~/opencv- 2.4.10.1/platforms/scripts/build_android_arm.sh” Make !!! › cd ~/opencv- 2.4.10.1/platforms/scripts/build_android_arm / › make
  • 11. ©SIProp Project, 2006-2008 11 Where are Made Files? OpenCV Libs. In “~/opencv- 2.4.10.1/platforms/build_android_arm/lib” highgui core etc... 3rd Party Libs for OpenCV. In “~/opencv- 2.4.10.1/platforms/build_android_arm/3rdparty/lib” libIlmImf libjasper libjpeg libpng libtiff
  • 12. ©SIProp Project, 2006-2008 12 Copy libs to sample program OpenCV Libs. cp ~/opencv- 2.4.10.1/platforms/build_android_arm/lib/armeabi-v7a/* ~/opencv-2.4.10.1/samples/android/native-activity/ libs_opencv/ 3rd Party Libs for OpenCV. cp ~/opencv- 2.4.10.1/platforms/build_android_arm/3rdparty/lib/ar meabi-v7a/* ~/opencv- 2.4.10.1/samples/android/native-activity/ libs_opencv/
  • 13. How to Use OpenCL on OpenCV for Android NDK(Native App) ©SIProp Project, 2006-2008 13
  • 14. ©SIProp Project, 2006-2008 14 Download Sample Source Code http://github.com/noritsuna/OpenCVwithOpenCL4Andr oidNDKSample Based OpenCV-2.4.10.1 & Android 4.4 Made Android Native Activity. Convert RGB Color Image to Black&White Image.
  • 15. Call Stack Call as C/C++ APIs Call as SysCall(C/ASM) ©SIProp Project, 2006-2008 15 Native Application NDK wo/ADT Standard Android Application for C/C++ Only C/C++ on Limited Library Layer Advantage Only C/C++ DirectCall C/C++ API Dis-Advantage Use a few Android Tools A few Docs from Google Developer Site & Blogs APK File(Your Application) (C/C++) Library Layer (C/C++) Kernel/Driver Layer (C/ASM)
  • 16. How to Make this Sample Source Code from OpenCV’s Sample Project ©SIProp Project, 2006-2008 16
  • 17. ©SIProp Project, 2006-2008 17 Use NativeActivity of OpenCV This Sample uses Only C/C++ Code. ~/opencv-2.4.10.1/samples/android/native-activity Structure of NativeActivity AndroidManifest.xml <- Property File for Android res <- Directory that has Resource file(Image,Voice) jni <- Directory that has files for NDK Android.mk <- make file of NDK Application.mk <- property file of NDK native.cpp <- Sample App Other Files/Dir, don’t use.
  • 18. ©SIProp Project, 2006-2008 18 Edit AndroidManifest.xml Replace “application” Section 1. <application android:label="@string/app_name" android:hasCode="false"> 2. <activity android:name="android.app.NativeActivity" 3. android:label="@string/app_name" 4. android:configChanges="orientation|keyboardHidden"> 5. <!-- Tell NativeActivity the name of or .so --> 6. <meta-data android:name="android.app.lib_name" 7. android:value="native_activity" /> 8. <intent-filter> 9. <action android:name="android.intent.action.MAIN" /> 10. <category android:name="android.intent.category.LAUNCHER" /> 11. </intent-filter> 12. </activity> 13. </application>
  • 19. 2. <uses-sdk android:minSdkVersion=“9" /> ©SIProp Project, 2006-2008 19 About AndroidManifest.xml API Level 1. <!-- This is the platform API where NativeActivity was introduced. --> Permission 1. <uses-permission android:name="android.permission.CAMERA "/>
  • 20. ©SIProp Project, 2006-2008 20 Create jni/Android.mk 1/9 1. Delete jni/Android.mk & Create the new jni/Android.mk 2. Set LOCAL_PATH & include 1. LOCAL_PATH := $(call my-dir) 2. include $(CLEAR_VARS)
  • 21. ©SIProp Project, 2006-2008 21 Create jni/Android.mk 2/9 1. Set OpenCV’s Modules & Libs 1. OPENCV_MODULES:=contrib legacy ml stitching superres objdetect ts videostab video photo calib3d features2d highgui imgproc flann ocl core androidcamera 2. OPENCV_3RDPARTY_COMPONENTS:=asse tmanager tbb libjpeg libpng libtiff libjasper IlmImf 3. OPENCV_CAMERA_MODULES:= native_camera_r2.2.0 native_camera_r2.3.3 native_camera_r3.0.1 native_camera_r4.0.0 native_camera_r4.0.3 native_camera_r4.1.1 native_camera_r4.2.0 native_camera_r4.3.0 native_camera_r4.4.0
  • 22. LOCAL_SRC_FILES:=../libs_opencv/libopenc v_$1.a 5. include $(PREBUILT_STATIC_LIBRARY) 6. endef ©SIProp Project, 2006-2008 22 Create jni/Android.mk 3/9 Set Loading OpenCV Libs Function 1. define add_opencv_module 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=opencv_$1 4. 1. $(foreach module,$(OPENCV_MODULES),$(eval $(call add_opencv_module,$(module))))
  • 23. 1. define add_opencv_3rdparty_component 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=$1 4. LOCAL_SRC_FILES:=../libs_opencv/lib$1.a 5. include $(PREBUILT_STATIC_LIBRARY) 6. endef ©SIProp Project, 2006-2008 23 Create jni/Android.mk 4/9 Set Loading OpenCV Libs Function 1. $(foreach module,$(OPENCV_3RDPARTY_COMPONEN TS),$(eval $(call add_opencv_3rdparty_component,$(module))))
  • 24. LOCAL_SRC_FILES:=../libs_opencv/lib$1.so 5. include $(PREBUILT_SHARED_LIBRARY) 6. endef ©SIProp Project, 2006-2008 24 Create jni/Android.mk 5/9 Set Loading AndroidCamera Libs Function 1. define add_opencv_camera_module 2. include $(CLEAR_VARS) 3. LOCAL_MODULE:=$1 4. 1. $(foreach module,$(OPENCV_CAMERA_MODULES),$(e val $(call add_opencv_camera_module,$(module))))
  • 25. ©SIProp Project, 2006-2008 25 Create jni/Android.mk 6/9 Set loading info lib 1. include $(CLEAR_VARS) 2. LOCAL_MODULE:=opencv_info 3. LOCAL_SRC_FILES:=../libs_opencv/libopenc v_info.so 4. include $(PREBUILT_SHARED_LIBRARY)
  • 26. ©SIProp Project, 2006-2008 26 Create jni/Android.mk 7/9 Set Include Dir Set Module name & Source Code OpenCVwithOpenCL4AndroidNDKNativeActivity.cpp 1. include $(CLEAR_VARS) 2. LOCAL_C_INCLUDES+=./jni/include 3. LOCAL_MODULE := OpenCV_CL 4. LOCAL_SRC_FILES := OpenCVwithOpenCL4AndroidNDKNativeActi vity.cpp
  • 27. ©SIProp Project, 2006-2008 27 Create jni/Android.mk 8/9 Set Loading Lib & OpenCV Lib LOCAL_LDLIBS -lm -llog -lc -ldl -lz -landroid LOCAL_STATIC_LIBRARIES $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS) 1. LOCAL_LDLIBS += -lm -llog -lc -ldl -lz - landroid 2. LOCAL_STATIC_LIBRARIES := android_native_app_glue $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) $(OPENCV_3RDPARTY_COMPONENTS)
  • 28. ©SIProp Project, 2006-2008 28 Create jni/Android.mk 9/9 Set Library Type Make SO lib BUILD_SHARED_LIBRARY Set Loading Android module Use “native_app_glue” module $(call import-module,android/native_app_glue) 1. include $(BUILD_SHARED_LIBRARY) 2. $(call import-module, android/native_app_glue)
  • 29. ©SIProp Project, 2006-2008 29 Setup/Make Build.xml 1/2 Making Setting File for Android using ANT Today, Development on CUI Only!!! Don’t use Eclipse! › android list target › Available Android targets: › ---------- › id: 1 or "android-9" › Name: Android 2.3.1 › Type: Platform › API level: 9 › Revision: 2 › Skins: WQVGA400, QVGA, HVGA, WVGA800 (default), WVGA854 › ABIs : armeabi › ---------- › id: 2 or "android-18" › Name: Android 4.3 › Type: Platform › API level: 18 › Revision: 2 › Skins: WXGA800, WQVGA400, QVGA, WQVGA432, WXGA800-7in, WVGA800 (default), HVGA, WXGA720, WSVGA, WVGA854 › ABIs : armeabi-v7a › ----------
  • 30. ©SIProp Project, 2006-2008 30 Setup/Make Build.xml 2/2 Make Build.xml › android update project --path . --target 2
  • 31. ©SIProp Project, 2006-2008 31 Build NativeActivity Build C/C++ Source Codes › ndk-build [clean all] Make APK File › ant [debug|release]
  • 32. › adb install bin/NativeActivity-debug.apk › adb uninstall com.example.native_activity ©SIProp Project, 2006-2008 32 Install NativeActivity.apk Install APK File UnInstall APK File Show Log › adb logcat
  • 33. ©SIProp Project, 2006-2008 33 adb(Android Debug Bridge) Functions Shell on Android Device adb shell Show Connected Android Devices adb devices Install/Uninstall APK File adb install/uninstall [App Name]/[Package Name] Send/Get File to/from Android Device adb push/pull [from File Path] [to File Path] Show Log (DDMS) adb logcat
  • 34. ©SIProp Project, 2006-2008 34 Quick Explanation to OpenCL 1. void android_main(android_app* app) { 2. (snip) 3. cv::Mat drawing_frame; 4. (snip) 5. colorSampleImage = imread(file_path); 6. (snip) 7. // Create Mat Object for OpenCL 8. cv::ocl::oclMat oclIn(colorSampleImage), oclOut; 9. // Change Color RGB->Black&White on OpenCL 10. cv::ocl::cvtColor(oclIn, oclOut, cv::COLOR_BGR2GRAY); 11. // Download Mat Object for OpenCL to Mat for CPU 12. oclOut.download(drawing_frame); 13. 14. engine_draw_frame(&engine, drawing_frame);
  • 35. How to Set up OpenCL Driver adreno-3xx: Qualcomm's Adreno 3xx GPUs ©SIProp Project, 2006-2008 35 mali-t6xx: ARM's Mali-T6xx GPUs
  • 36. ©SIProp Project, 2006-2008 36 Download OpenCL Drivers OpenCL Drivers was deleted since 4.2 adreno-3xx: Qualcomm's Adreno 3xx GPUs mali-t6xx: ARM's Mali-T6xx GPUs Download from: https://github.com/madeye/opencl-android-proprietary
  • 37. ©SIProp Project, 2006-2008 37 Set up Drivers to your Android Ex. adreno-3xx: Qualcomm's Adreno 3xx GPUs 1. $ adb push kitkat/system/vendor/lib /sdcard/ 2. $ adb shell 3. $ su 4. # mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system 5. # cp /sdcard/libOpenCL.so /system/vendor/lib/ 6. # cp /sdcard/libllvm-a3xx.so /system/vendor/lib/ 7. # chmod 644 /system/vendor/lib/libOpenCL.so 8. # chmod 644 /system/vendor/lib/libllvm-a3xx.so