Using CCV on an Android device

Has anyone tried using libccv on Android? I cannot find any example code on the internet and would like to know how to implement a tracker in an Android application using CCV.

This includes: -Changing the camera image of the Android device -Opening the CCV-processed image on the device screen.

+3


source to share


1 answer


I recently implemented something similar. To do this, I created an Android JNI project with OpenCV and used OpenCV's image reading capabilities to store frames. The frame data pointer can then be passed to the CCV Image wrapper for use with the CCV library functions. CCV has minimal dependencies and the easiest way to get up and running is to include the source code of the modules you need in the project's JNI directory.

To set up your project using OpenCV, you can follow this tutorial. The OpenCV SDK has a sample project for a simple camera reader. The Android GitHub page contains a sample HelloJNI project here showing how to set up an Android project with Java and C / C ++ using JNI. The CCV source can then be added to the C / C ++ source directory so that your C / C ++ functions can access the library.

After you have created a project with OpenCV libraries and JNI functionality, you need to save the frame data using OpenCV and pass a pointer to C code. Store each frame as an object Mat

, then the object Mat

can be passed to your C / C ++ code like this: (Note that this is only an extract showing the required key code segments)

package your.namespace.here;

import org.opencv.core.Core;
import org.opencv.core.Mat;

public class MainActivity{

    // Store frames in this object for later processing
    Mat frame;

    static {
        // Load the c file name with JNI bindings, e.g. here we load test.c
        System.loadLibrary("test");
    }

    // Declare the JNI function wrapper
    public native int ccvTest( long input, long output);

    // OpenCV methods here to store the frame, see 
    // OpenCV4Android - tutorial-1-camerapreview for full 
    // code description.
    //...

    // This function to be called after each frame is stored.
    // output can then be converted to Bitmap and displayed in ImageView
    // or used for further processing with OpenCV.
    public Mat processFrame(){
        Mat output = new Mat();
        ccvTest(frame.getNativeObjAddr(), output.getNativeObjAddr());
        return output;
    }
}

      

Using the HelloJNI template, a sample C file (for this example we call it test.c) by calling one of the CCV library functions would look like this:



#include <string.h>
#include <jni.h>

#ifdef __cplusplus
extern "C" {
#endif
// ccv files to include should be compiled using c compiler
#include "ccv/lib/ccv.h"
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL
Java_your_namespace_here_MainActivity_ccvTest( JNIEnv* env,
                                              jobject thiz,
                                              jlong input, jlong output)
{

    Mat* in_p  = (Mat*)input;
    Mat* out_p  = (Mat*)output;
    Mat &rgb = *in_p;
    ccv_dense_matrix_t* image = 0;

    // Pass the Mat data to the CCV Image wrapper
    ccv_read(rgb.data, &image, CCV_IO_BGR_RAW | CCV_IO_ANY_RAW |     CCV_IO_GRAY, rgb.rows, rgb.cols, rgb.step[0]);

    // Now the Mat is in ccv image format, you can pass
    // the image pointer to any ccv function you like.

    //
    // Put your calls to CCV library here..
    //

}
#ifdef __cplusplus
}
#endif

      

The tree structure of the project might look something like this, with the entire ccv source in the jni / ccv folder:

enter image description here

This setting is useful as it allows you to connect to both OpenCV and CCV features. Hope this helps.

+1


source







All Articles