我试图切换到新的OpenCV 3.0,因为它隐式调用OpenCL以便更快地对矩阵进行操作。我遵循了Sony提供的一个很好的教程,并且能够集成所需的OpenCV文件以进行编译。 我只是简单地读取图像检测边缘。在CPU和GPU方面也做了同样的事情。但我认为我错过了一些重要的东西,因为在两种情况下处理所需的时间都是一样的。代码片段如下:
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) ;
和"纯粹"本机代码:
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) ;
在OpenCV / OpenCL / Android教程here中,已经提到过需要使用选项
编译它。-DWITH_OPENCL = YES
这有必要吗?应用程序编译并运行没有任何错误,但我认为不会在GPU上运行。任何帮助非常感谢!非常感谢!
更新
这是 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)
答案 0 :(得分:2)
好的,我能够让它工作......有些方面。我重新编译了OpenCV,并使用以下方法将OCL / OpenCL部分包含在构建中:
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
与CPU版本的代码相比,计算时间没有减少。
另一个更普遍的问题: Xperia Z1(Snapdragon Adreno 330)最初在KitKat中使用OpenCL 1.1。相反,当使用棒棒糖时,是否会引起严重问题?日志说,OCL版本的OpenCV已经成功初始化。程序运行正常,但速度很慢!