OpenCV Tutorial 2: Mixed Processing


This example shows how to pre-process the camera preview frames with both Java and C++ calls to OpenCV. It has a similar structure to Tutorial 1, with the exception that it processes the input camera frame before displaying it as:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

     final int viewMode = mViewMode;
     switch (viewMode) {
     case VIEW_MODE_GRAY:
          // input frame has gray scale format
          Imgproc.cvtColor( inputFrame.gray(), mRgba,
                            Imgproc.COLOR_GRAY2RGBA, 4 );
          break;
     case VIEW_MODE_RGBA:
          // input frame has RBGA format
          mRgba = inputFrame.rgba();
          break;

     case VIEW_MODE_CANNY:
          // input frame has gray scale format
          mRgba = inputFrame.rgba();
          Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100);
          Imgproc.cvtColor( mIntermediateMat, mRgba,
                            Imgproc.COLOR_GRAY2RGBA, 4 );
          break;

     case VIEW_MODE_FEATURES:
          // input frame has RGBA format
          mRgba = inputFrame.rgba();
          mGray = inputFrame.gray();
          FindFeatures( mGray.getNativeObjAddr(),
                        mRgba.getNativeObjAddr() );
          break;
     }
     return mRgba;
}

Note that the FindFeatures() function is implemented at the native level in C++. Furthermore, it is declared in Java as a native method:

public native void FindFeatures(long matAddrGr, long matAddrRgba);

The C/C++ code for this example resides in the jni directory of the project. The Android.mk and Application.mk makefiles in the jni directory are required by the Android NDK compiler to build the native library (libnative_sample.so) located at libs/armeabi-v7a/.

The native FindFeatures function is defined in the file jni/jni_part.cpp. Observe the following special features of the jni_part.cpp file that implement the FindFeatures function’s JNI interface.

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(
     JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{
     Mat& mGr = *(Mat*)addrGray;
     Mat& mRgb = *(Mat*)addrRgba;
     vector<KeyPoint> v; 
     FastFeatureDetector detector(50);
     detector.detect(mGr, v);
     for( unsigned int i = 0; i < v.size(); i++ )
     {
          const KeyPoint& kp = v[i];
          circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255,0,0,255));
     }
}

Note the include statement in the jni/Android.mk file:

include ../../sdk/native/jni/OpenCV-tegra3.mk

Note: If you wish to add and build a native library to your existing Eclipse Android Java projects that calls OpenCV native functions to your existing Eclipse Android Java projects, follow the steps below.

  1. Add a jni source folder to your Eclipse Android project by right clicking on the project in Project Explorer > New > Source Folder > Folder name: jni > Finish.
  2. Add the native source .cpp and .h files containing the correctly defined native JNI functions to the jni folder. Right-click on the jni folder in Project Explorer > New > Source File or Header File > foo.cpp or foo.h > Finish.
  3. Add appropriate Android.mk and Application.mk makefiles to the jni folders for building the native shared library. If native OpenCV functions are called in the native library, include the OpenCV libraries by adding the following line in the Android.mk file immediately after the include $(CLEAR_VARS) statement, as shown below:
    include <path to TADP directory>/OpenCV-2.4.8.2-tegra-sdk/sdk/native/jni/OpenCV-tegra3.mk 
    #For Tegra 3, 4i, 4 and K1 devices 
    #OR
    include <path to TADP directory>/OpenCV-2.4.8.2-tegra-sdk/sdk/native/jni/OpenCV.mk
    #For Tegra 2 devices
    

    You can additionally add a flag just before this include statement to specify how the OpenCV libraries should be linked into your native code as the following:
    OPENCV_LIB_TYPE:=SHARED #for dynamic linking, which is also the default behavior 
    #OR 
    OPENCV_LIB_TYPE:=STATIC #for static linking

    The Android.mk file usually has the following structure:
    LOCAL_PATH := $(call my-dir)				
    include $(CLEAR_VARS)
    LOCAL_MODULE := <module_name>
    LOCAL_SRC_FILES := <list of .c and .cpp project files>
    <some variable name> := <some variable value> ...
    <some variable name> := <some variable value>
    include $(BUILD_SHARED_LIBRARY)

    This is the minimal Android.mk file, which builds the native source files of an Android application. Note that the first two lines and the last line are mandatory for any Android.mk file.

    Usually the file Application.mk is optional, but in case a project uses OpenCV, STL and exceptions are used in C++ and the Application.mk should be written with the following statements:
    APP_STL := gnustl_static
    APP_CPPFLAGS := -frtti -fexceptions
    APP_ABI := armeabi-v7a

    See the Android.mk and Application.mk files for OpenCV Tutorial 2 and OpenCV Sample – face-detection for more examples.
  4. Add a C/C++ nature to the native Android project. Right-click on the project in Project Explorer > New > Other > Convert to a C/C++ Project (Adds C/C++ nature) > Next > Select your Android project from the list > Select Project Type: Makefile Project and Toolchain: (other) > Finish.
  5. Add the folders libs and obj to your Android project by right-clicking on the project in Project Explorer > New > Folder > Folder name: libs or obj > Finish.
    An a symbol should appear above these folders in the Project Explorer window, if Android ADT correctly recognizes them.
  6. Make sure that the builders for the project are correct. Right-click on the project in Project Explorer > Properties > Builders > verify that CDT builder is on top.
    If an Invalid External Tool Builder with a cross symbol above it is present, remove it and click OK.
  7. Configure the C/C++ builder by right-clicking on project in Project Explorer > Properties > C/C++ Build > uncheck Use default build command and enter the following Build Command:
    ${CYGWIN_HOME}/bin/bash ${NDKROOT}/ndk-build NDK_DEBUG=1 V=1

    (Note that this command is valid for Windows, Ubuntu and OS X operating systems.)

    Uncheck Generate Makefiles Automatically and enter the following Build directory:
    ${workspace_loc:/<Eclipse project name>} 

    Then click Apply and OK.
  8. To build and run, right-click on the project in Project Explorer > Run as > 1 Android Application. This should build the native library, which will be located in the folder libs/armeabi-v7a.

For more details see: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/android_dev_intro.html#native-development-in-c

http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#native-c

 

 

 

 


NVIDIA® GameWorks™ Documentation Rev. 1.0.220830 ©2014-2022. NVIDIA Corporation and affiliates. All Rights Reserved.