有人试过在安卓上使用libccv吗?我在网上找不到任何示例代码,我想知道如何使用ccv在android应用程序中实现跟踪器。
这包括做以下事情:
-处理来自Android设备摄像头的图像
-在设备屏幕上显示ccv处理的图像

最佳答案

我最近也实现了类似的功能。为了实现这一点,我用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文件夹中:
android - 在Android设备上使用CCV-LMLPHP
这个设置非常有用,因为它允许您连接到opencv和ccv的功能中。希望这有帮助。

关于android - 在Android设备上使用CCV,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31864043/

10-11 21:35