Nematode7
Nematode7

Reputation: 127

Using CCV on Android Device

Has anybody tried using libccv on Android? I can't find any example code online and would like to know how implement a tracker in an Android app using CCV.

This includes doing things like: -Processing an image from the android device camera -Displaying an image processed by CCV on the device screen

Upvotes: 3

Views: 636

Answers (1)

sparkitny
sparkitny

Reputation: 1533

I've recently implemented something similar. To achieve this, I set up an Android JNI project with OpenCV and use the OpenCV camera reading capability to store the frames. A pointer to the frame data 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 require in your JNI directory of the project.

To set up a project with 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 which shows how to set up your Android project with both 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 have access to the library.

Once you have the project set up with OpenCV libraries and JNI functionality, it is a matter of saving the frame data with OpenCV and passing a pointer of it to the C code. Store each frame as a Mat object, the Mat object can then be passed to your C/C++ code like this: (Note this is only an extract showing the key code segments required)

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) with a call to 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 may look similar to this, with all of the ccv source in the jni/ccv folder:

enter image description here

This set up is useful as it allows you to hook into the functionality of both OpenCV and CCV. Hope this helps.

Upvotes: 1

Related Questions