【问题标题】:Using CCV on Android Device在 Android 设备上使用 CCV
【发布时间】:2015-08-06 19:16:02
【问题描述】:

有人试过在 Android 上使用 libccv 吗?我在网上找不到任何示例代码,想知道如何使用 CCV 在 Android 应用中实现跟踪器。

这包括执行以下操作: - 处理来自 android 设备相机的图像 - 在设备屏幕上显示经过 CCV 处理的图像

【问题讨论】:

  • 我想在 Android 中使用 libccv。有什么解决办法吗?

标签: android computer-vision


【解决方案1】:

我最近实现了类似的东西。为此,我使用 OpenCV 建立了一个 Android JNI 项目,并使用 OpenCV 摄像头读取功能来存储帧。然后可以将指向帧数据的指针传递给 CCV 图像包装器,以便与 CCV 库函数一起使用。 CCV 具有最小的依赖关系,启动和运行的最简单方法是将所需模块的源代码包含在项目的 JNI 目录中。

要使用 OpenCV 设置项目,您可以关注 this tutorial. OpenCV SDK 有一个用于简单相机阅读器的示例项目。 Android GitHub 页面包含一个示例 HelloJNI 项目 here,它展示了如何使用 JNI 使用 Java 和 C/C++ 设置您的 Android 项目。然后可以将 CCV 源添加到 C/C++ 源目录中,以便您的 C/C++ 函数可以访问该库。

使用 OpenCV 库和 JNI 功能设置项目后,只需使用 OpenCV 保存帧数据并将其指针传递给 C 代码即可。将每个帧存储为 Mat 对象,然后可以将 Mat 对象传递给您的 C/C++ 代码,如下所示:(请注意,这只是显示所需关键代码段的摘录)

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;
    }
}

使用 HelloJNI 模板,调用 CCV 库函数之一的示例 C 文件(在本例中我们称之为 test.c)如下所示:

#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

项目的树形结构可能与此类似,所有 ccv 源代码都在 jni/ccv 文件夹中:

此设置很有用,因为它允许您连接 OpenCV 和 CCV 的功能。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 2020-08-28
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    相关资源
    最近更新 更多