1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge branch 4.x

This commit is contained in:
Alexander Smorkalov
2024-01-15 17:23:10 +03:00
531 changed files with 20900 additions and 12934 deletions
@@ -1,12 +1,4 @@
set(sample example-tutorial-4-opencl)
if(NOT DEFINED ANDROID_OPENCL_SDK)
message(STATUS "Sample ${sample} is disabled, because ANDROID_OPENCL_SDK is not specified")
return()
endif()
if(ANDROID_NATIVE_API_LEVEL LESS 14)
message(STATUS "Sample ${sample} is disabled, because ANDROID_NATIVE_API_LEVEL < 14")
return()
endif()
if(BUILD_FAT_JAVA_LIB)
set(native_deps opencv_java)
@@ -14,12 +6,10 @@ else()
set(native_deps opencv_imgproc)
endif()
include_directories(${ANDROID_OPENCL_SDK}/include)
link_directories(${ANDROID_OPENCL_SDK}/lib/${ANDROID_NDK_ABI_NAME})
add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}"
LIBRARY_DEPS "${OPENCV_ANDROID_LIB_DIR}"
SDK_TARGET 21 "${ANDROID_SDK_TARGET}"
NATIVE_DEPS ${native_deps} -lGLESv2 -lEGL -lOpenCL
NATIVE_DEPS ${native_deps} -lGLESv2 -lEGL
COPY_LIBS YES
)
if(TARGET ${sample})
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.tutorial4'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.tutorial4"
@@ -11,7 +12,16 @@ android {
externalNativeBuild {
cmake {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DOPENCV_FROM_SDK=TRUE",
"-DANDROID_OPENCL_SDK=@ANDROID_OPENCL_SDK@" @OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DOPENCV_FROM_SDK=FALSE",
"-DANDROID_OPENCL_SDK=@ANDROID_OPENCL_SDK@" @OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "JNIpart"
}
}
@@ -35,9 +45,20 @@ android {
path '@ANDROID_SAMPLE_JNI_PATH@/CMakeLists.txt'
}
}
buildFeatures {
if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
prefab true
}
}
}
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}
@@ -1,27 +0,0 @@
LOCAL_PATH := $(call my-dir)
# add OpenCV
include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
ifdef OPENCV_ANDROID_SDK
ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
include ${OPENCV_ANDROID_SDK}/OpenCV.mk
else
include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
endif
else
include ../../sdk/native/jni/OpenCV.mk
endif
ifndef OPENCL_SDK
$(error Specify OPENCL_SDK to Android OpenCL SDK location)
endif
# add OpenCL
LOCAL_C_INCLUDES += $(OPENCL_SDK)/include
LOCAL_LDLIBS += -L$(OPENCL_SDK)/lib/$(TARGET_ARCH_ABI) -lOpenCL
LOCAL_MODULE := JNIpart
LOCAL_SRC_FILES := jni.c CLprocessor.cpp
LOCAL_LDLIBS += -llog -lGLESv2 -lEGL
include $(BUILD_SHARED_LIBRARY)
@@ -1,4 +0,0 @@
APP_STL := gnustl_static
APP_GNUSTL_FORCE_CPP_FEATURES := exceptions rtti
APP_ABI := armeabi-v7a
APP_PLATFORM := android-14
@@ -1,6 +1,8 @@
#ifdef OPENCL_FOUND
#define __CL_ENABLE_EXCEPTIONS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS /*let's give a chance for OpenCL 1.1 devices*/
#include <CL/cl.hpp>
#include <CL/opencl.hpp>
#endif
#include <GLES2/gl2.h>
#include <EGL/egl.h>
@@ -10,7 +12,9 @@
#include <opencv2/core/ocl.hpp>
#include "common.hpp"
#include "CLprocessor.hpp"
#ifdef OPENCL_FOUND
const char oclProgB2B[] = "// clBuffer to clBuffer";
const char oclProgI2B[] = "// clImage to clBuffer";
const char oclProgI2I[] = \
@@ -33,7 +37,7 @@ const char oclProgI2I[] = \
" write_imagef(imgOut, pos, sum*10); \n" \
"} \n";
void dumpCLinfo()
static void dumpCLinfo()
{
LOGD("*** OpenCL info ***");
try
@@ -83,10 +87,11 @@ cl::CommandQueue theQueue;
cl::Program theProgB2B, theProgI2B, theProgI2I;
bool haveOpenCL = false;
extern "C" void initCL()
//![init_opencl]
int initCL()
{
dumpCLinfo();
LOGE("initCL: start initCL");
EGLDisplay mEglDisplay = eglGetCurrentDisplay();
if (mEglDisplay == EGL_NO_DISPLAY)
LOGE("initCL: eglGetCurrentDisplay() returned 'EGL_NO_DISPLAY', error = %x", eglGetError());
@@ -133,21 +138,26 @@ extern "C" void initCL()
catch(const cl::Error& e)
{
LOGE("cl::Error: %s (%d)", e.what(), e.err());
return 1;
}
catch(const std::exception& e)
{
LOGE("std::exception: %s", e.what());
return 2;
}
catch(...)
{
LOGE( "OpenCL info: unknown error while initializing OpenCL stuff" );
return 3;
}
LOGD("initCL completed");
}
extern "C" void closeCL()
{
if (haveOpenCL)
return 0;
else
return 4;
}
//![init_opencl]
#define GL_TEXTURE_2D 0x0DE1
void procOCL_I2I(int texIn, int texOut, int w, int h)
@@ -160,6 +170,7 @@ void procOCL_I2I(int texIn, int texOut, int w, int h)
}
LOGD("procOCL_I2I(%d, %d, %d, %d)", texIn, texOut, w, h);
//![process_pure_opencl]
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texOut);
std::vector < cl::Memory > images;
@@ -187,6 +198,7 @@ void procOCL_I2I(int texIn, int texOut, int w, int h)
theQueue.enqueueReleaseGLObjects(&images);
theQueue.finish();
LOGD("enqueueReleaseGLObjects() costs %d ms", getTimeInterval(t));
//![process_pure_opencl]
}
void procOCL_OCV(int texIn, int texOut, int w, int h)
@@ -198,6 +210,7 @@ void procOCL_OCV(int texIn, int texOut, int w, int h)
return;
}
//![process_tapi]
int64_t t = getTimeMs();
cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, texIn);
std::vector < cl::Memory > images(1, imgIn);
@@ -224,11 +237,22 @@ void procOCL_OCV(int texIn, int texOut, int w, int h)
cl_command_queue q = (cl_command_queue)cv::ocl::Queue::getDefault().ptr();
size_t offset = 0;
size_t origin[3] = { 0, 0, 0 };
size_t region[3] = { w, h, 1 };
size_t region[3] = { (size_t)w, (size_t)h, 1 };
CV_Assert(clEnqueueCopyBufferToImage (q, clBuffer, imgOut(), offset, origin, region, 0, NULL, NULL) == CL_SUCCESS);
theQueue.enqueueReleaseGLObjects(&images);
cv::ocl::finish();
LOGD("uploading results to texture costs %d ms", getTimeInterval(t));
//![process_tapi]
}
#else
int initCL()
{
return 5;
}
#endif
void closeCL()
{
}
void drawFrameProcCPU(int w, int h, int texOut)
@@ -263,7 +287,7 @@ void drawFrameProcCPU(int w, int h, int texOut)
enum ProcMode {PROC_MODE_NO_PROC=0, PROC_MODE_CPU=1, PROC_MODE_OCL_DIRECT=2, PROC_MODE_OCL_OCV=3};
extern "C" void processFrame(int tex1, int tex2, int w, int h, int mode)
void processFrame(int tex1, int tex2, int w, int h, int mode)
{
switch(mode)
{
@@ -271,12 +295,14 @@ extern "C" void processFrame(int tex1, int tex2, int w, int h, int mode)
case PROC_MODE_CPU:
drawFrameProcCPU(w, h, tex2);
break;
#ifdef OPENCL_FOUND
case PROC_MODE_OCL_DIRECT:
procOCL_I2I(tex1, tex2, w, h);
break;
case PROC_MODE_OCL_OCV:
procOCL_OCV(tex1, tex2, w, h);
break;
#endif
default:
LOGE("Unexpected processing mode: %d", mode);
}
@@ -0,0 +1,8 @@
#ifndef __CL_PROCESSOR_HPP__
#define __CL_PROCESSOR_HPP__
int initCL();
void closeCL();
void processFrame(int tex1, int tex2, int w, int h, int mode);
#endif
@@ -1,15 +1,42 @@
cmake_minimum_required(VERSION 3.6)
set(target mixed_sample)
set(target JNIpart)
project(${target} CXX)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
if (OPENCV_FROM_SDK)
message(STATUS "Using OpenCV from local SDK")
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
message(STATUS "Using OpenCV from AAR (Maven repo)")
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
find_package(OpenCL QUIET)
file(GLOB srcs *.cpp *.c)
file(GLOB hdrs *.hpp *.h)
include_directories("${CMAKE_CURRENT_LIST_DIR}")
add_library(${target} SHARED ${srcs} ${hdrs})
target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS} -lGLESv2 -lEGL -lOpenCL)
target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS} -lGLESv2 -lEGL -llog)
if(OpenCL_FOUND)
include_directories(${OpenCL_INCLUDE_DIRS})
target_link_libraries(${target} ${OpenCL_LIBRARIES})
add_definitions("-DOPENCL_FOUND")
elseif(NOT ("${ANDROID_OPENCL_SDK}" STREQUAL ""))
include_directories(${ANDROID_OPENCL_SDK}/include)
link_directories(${ANDROID_OPENCL_SDK}/lib)
target_link_directories(${target} PRIVATE ${ANDROID_OPENCL_SDK}/lib)
set_target_properties(${target} PROPERTIES LINK_FLAGS "-Wl,--allow-shlib-undefined")
target_link_libraries(${target} -lOpenCL)
add_definitions("-DOPENCL_FOUND")
add_definitions("-DCL_HPP_MINIMUM_OPENCL_VERSION=120")
add_definitions("-DCL_HPP_TARGET_OPENCL_VERSION=120")
add_definitions("-DCL_HPP_ENABLE_PROGRAM_CONSTRUCTION_FROM_ARRAY_COMPATIBILITY")
endif()
@@ -1,20 +0,0 @@
#include <jni.h>
int initCL();
void closeCL();
void processFrame(int tex1, int tex2, int w, int h, int mode);
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativePart_initCL(JNIEnv * env, jclass cls)
{
return initCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_closeCL(JNIEnv * env, jclass cls)
{
closeCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_processFrame(JNIEnv * env, jclass cls, jint tex1, jint tex2, jint w, jint h, jint mode)
{
processFrame(tex1, tex2, w, h, mode);
}
@@ -0,0 +1,36 @@
#include <jni.h>
#include "CLprocessor.hpp"
extern "C" {
JNIEXPORT jboolean JNICALL Java_org_opencv_samples_tutorial4_NativePart_builtWithOpenCL(JNIEnv * env, jclass cls);
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativePart_initCL(JNIEnv * env, jclass cls);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_closeCL(JNIEnv * env, jclass cls);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_processFrame(JNIEnv * env, jclass cls, jint tex1, jint tex2, jint w, jint h, jint mode);
JNIEXPORT jboolean JNICALL Java_org_opencv_samples_tutorial4_NativePart_builtWithOpenCL(JNIEnv * env, jclass cls)
{
#ifdef OPENCL_FOUND
return JNI_TRUE;
#else
return JNI_FALSE;
#endif
}
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial4_NativePart_initCL(JNIEnv * env, jclass cls)
{
return initCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_closeCL(JNIEnv * env, jclass cls)
{
closeCL();
}
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_NativePart_processFrame(JNIEnv * env, jclass cls, jint tex1, jint tex2, jint w, jint h, jint mode)
{
processFrame(tex1, tex2, w, h, mode);
}
} // extern "C"
@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item android:id="@+id/no_proc" android:title="No processing" />
<item android:id="@+id/cpu" android:title="Use CPU code" />
<item android:id="@+id/ocl_direct" android:title="Use OpenCL direct" />
<item android:id="@+id/ocl_ocv" android:title="Use OpenCL via OpenCV" />
</group>
</menu>
@@ -13,6 +13,7 @@ import android.view.SurfaceHolder;
import android.widget.TextView;
import android.widget.Toast;
//![minimal_surface_view]
public class MyGLSurfaceView extends CameraGLSurfaceView implements CameraGLSurfaceView.CameraTextureListener {
static final String LOGTAG = "MyGLSurfaceView";
@@ -65,7 +66,8 @@ public class MyGLSurfaceView extends CameraGLSurfaceView implements CameraGLSurf
Toast.makeText(getContext(), "onCameraViewStarted", Toast.LENGTH_SHORT).show();
}
});
NativePart.initCL();
if (NativePart.builtWithOpenCL())
NativePart.initCL();
frameCounter = 0;
lastNanoTime = System.nanoTime();
}
@@ -110,3 +112,4 @@ public class MyGLSurfaceView extends CameraGLSurfaceView implements CameraGLSurf
return true;
}
}
//![minimal_surface_view]
@@ -1,9 +1,10 @@
package org.opencv.samples.tutorial4;
//![native_part]
public class NativePart {
static
{
System.loadLibrary("opencv_java4");
System.loadLibrary("opencv_java5");
System.loadLibrary("JNIpart");
}
@@ -12,7 +13,9 @@ public class NativePart {
public static final int PROCESSING_MODE_OCL_DIRECT = 2;
public static final int PROCESSING_MODE_OCL_OCV = 3;
public static native boolean builtWithOpenCL();
public static native int initCL();
public static native void closeCL();
public static native void processFrame(int tex1, int tex2, int w, int h, int mode);
}
//![native_part]
@@ -1,6 +1,5 @@
package org.opencv.samples.tutorial4;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
@@ -10,11 +9,20 @@ import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class Tutorial4Activity extends Activity {
import org.opencv.android.CameraActivity;
public class Tutorial4Activity extends CameraActivity {
private MyGLSurfaceView mView;
private TextView mProcMode;
private boolean builtWithOpenCL = false;
private MenuItem mItemNoProc;
private MenuItem mItemCpu;
private MenuItem mItemOclDirect;
private MenuItem mItemOclOpenCV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -38,6 +46,7 @@ public class Tutorial4Activity extends Activity {
}
});
builtWithOpenCL = NativePart.builtWithOpenCL();
mView.setProcessingMode(NativePart.PROCESSING_MODE_NO_PROCESSING);
}
@@ -55,48 +64,37 @@ public class Tutorial4Activity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
mItemNoProc = menu.add("No processing");
mItemCpu = menu.add("Use CPU code");
if (builtWithOpenCL) {
mItemOclOpenCV = menu.add("Use OpenCL via OpenCV");
mItemOclDirect = menu.add("Use OpenCL direct");
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.no_proc:
runOnUiThread(new Runnable() {
public void run() {
mProcMode.setText("Processing mode: No Processing");
}
});
mView.setProcessingMode(NativePart.PROCESSING_MODE_NO_PROCESSING);
return true;
case R.id.cpu:
runOnUiThread(new Runnable() {
public void run() {
mProcMode.setText("Processing mode: CPU");
}
});
mView.setProcessingMode(NativePart.PROCESSING_MODE_CPU);
return true;
case R.id.ocl_direct:
runOnUiThread(new Runnable() {
public void run() {
mProcMode.setText("Processing mode: OpenCL direct");
}
});
mView.setProcessingMode(NativePart.PROCESSING_MODE_OCL_DIRECT);
return true;
case R.id.ocl_ocv:
runOnUiThread(new Runnable() {
public void run() {
mProcMode.setText("Processing mode: OpenCL via OpenCV (TAPI)");
}
});
mView.setProcessingMode(NativePart.PROCESSING_MODE_OCL_OCV);
return true;
default:
return false;
String procName = "Not selected";
int procMode = NativePart.PROCESSING_MODE_NO_PROCESSING;
if (item == mItemNoProc) {
procMode = NativePart.PROCESSING_MODE_NO_PROCESSING;
procName = "Processing mode: No Processing";
} else if (item == mItemCpu) {
procMode = NativePart.PROCESSING_MODE_CPU;
procName = "Processing mode: CPU";
} else if (item == mItemOclOpenCV && builtWithOpenCL) {
procMode = NativePart.PROCESSING_MODE_OCL_OCV;
procName = "Processing mode: OpenCL via OpenCV (TAPI)";
} else if (item == mItemOclDirect && builtWithOpenCL) {
procMode = NativePart.PROCESSING_MODE_OCL_DIRECT;
procName = "Processing mode: OpenCL direct";
}
mView.setProcessingMode(procMode);
mProcMode.setText(procName);
return true;
}
}
}