beniroquai
beniroquai

Reputation: 166

Use T-API (Opencv 3.0) with OpenCL in Android Studio No Speed improvements

I was trying to switch to the new OpenCV 3.0, as it implicitly calls OpenCL for faster operations on matrices. I followed a nice Tutorial provided by Sony and was able to integrate the required OpenCV files to make it compiling. I'm simply reading an image detect edges. The same thing is done on CPU and GPU side. But I think I'm missing something big, because the time, that it takes to process is the same in both cases. The Code snippets are the following:

clock_t startTimer1, stopTimer1;

cv::ocl::setUseOpenCL(true);

String path = "/storage/emulated/0/DCIM/100ANDRO/";
String filename = "DSC_0581.JPG";
String filename_result = "DSC_0581_processed.JPG";

ocl::setUseOpenCL(true);
Mat gpuFrame;
UMat gpuBW;
UMat gpuBlur;
UMat gpuEdges;

gpuFrame = imread(path+filename, IMREAD_COLOR);
gpuBW = gpuFrame.getUMat(cv::ACCESS_READ);

startTimer1=clock();
cvtColor(gpuBW, gpuBW, COLOR_BGR2GRAY);
GaussianBlur(gpuBW, gpuBlur, Size(1,1), 1.5, 1.5);
Canny(gpuBlur, gpuEdges, 0, 30, 3);
cv::ocl::finish();
stopTimer1 = clock();

imwrite(path+filename_result, gpuEdges);

double elapse = 1000.0* (double)(stopTimer1 - startTimer1)/(double)CLOCKS_PER_SEC;
info[2] = (int)elapse;
LOGI("OpenCL code on the GPU took %g ms\n\n", 1000.0* (double)(stopTimer1 - startTimer1)/(double)CLOCKS_PER_SEC) ;

and the "pure" native code:

ocl::setUseOpenCL(false);
Mat cpuFrame;
String path = "/storage/emulated/0/DCIM/100ANDRO/";
String filename = "DSC_0581.JPG";
String filename_result = "DSC_0581_cpu_processed.JPG";

Mat cpuBW;
Mat cpuBlur;
Mat cpuEdges;

cpuFrame = imread(path+filename);
startTimer=clock();
cvtColor(cpuFrame, cpuBW, COLOR_BGR2GRAY);
GaussianBlur(cpuBW, cpuBlur, Size(1, 1), 1.5, 1.5);
Canny(cpuBlur, cpuEdges, 0, 30, 3);
stopTimer = clock();
imwrite(path+filename_result, cpuEdges);

double elapse = 1000.0* (double)(stopTimer - startTimer)/(double)CLOCKS_PER_SEC;
info[2] = (int)elapse;

LOGI("C++ code on the CPU took %g ms\n\n", 1000.0* (double)(stopTimer - startTimer)/(double)CLOCKS_PER_SEC) ;

In the OpenCV/OpenCL/Android Tutorial here, it has been mentioned, that it is required to compile it with option

-DWITH_OPENCL=YES

Is that necessary? The application compiles and runs without any error, but I think doesn't run on GPU though. Any help much appreciated! Thanks a lot!

UPDATE

This is the Android.mk

LOCAL_PATH      := $(call my-dir)
LOCAL_PATH_EXT  := $(call my-dir)/../extra_libs/
include $(CLEAR_VARS)

#opencv
OPENCVROOT:= C:/Android/opencva3
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
-DWITH_OPENCL=YES

include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_ARM_MODE  := arm

LOCAL_MODULE    := openclexample1

LOCAL_CFLAGS    += -DANDROID_CL -DWITH_OPENCL
LOCAL_CFLAGS    += -O3 -ffast-math

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include

LOCAL_SRC_FILES := sonyOpenCLexample1.cpp openCLNR.cpp refNR.cpp #ocdft.cpp

LOCAL_LDLIBS    +=  -ldl -llog -ljnigraphics
LOCAL_LDLIBS    += -lGLESv2 -lEGL
LOCAL_LDLIBS    += $(LOCAL_PATH_EXT)libOpenCL.so  

include $(BUILD_SHARED_LIBRARY)

Upvotes: 2

Views: 1580

Answers (1)

beniroquai
beniroquai

Reputation: 166

Ok, I was able to make it work..Somehow. I recompiled OpenCV and inlcuded the OCL/OpenCL part into the build using this:

set PATH=%PATH%; PATH/TO/NINJA/ninja.exe
mkdir OpenCVCL3
cd OpenCVCL3
cmake -GNinja -DCMAKE_MAKE_PROGRAM="PATH/TO/NINJA//ninja.exe" -DCMAKE_TOOLCHAIN_FILE=PATH/TO/OPENCV3/platforms/android/android.toolchain.cmake -DANDROID_ABI="armeabi-v7a with NEON" -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DWITH_OPENCL=YES PATH/TO/OPENCV3
path/to/ninja.exe install/strip

Computation Time hasn't decreased compared to CPU-Version of code.

Another more general question: The Xperia Z1 (Snapdragon Adreno 330) uses OpenCL 1.1 in KitKat originally. Does it cause major problems, when using Lollipop instead? The log says, that OCL-Version of OpenCV has been initialized succesfully. Program works ok, but is slow!

Upvotes: 2

Related Questions