android - 使用OpenCV在Android上进行多对象检测

标签 android opencv android-ndk java-native-interface object-detection

我正在尝试开发一种可检测某些物体(例如人,车辆和树木)的应用程序。首先,我尝试移植OpenCV pedestrian sample,但是帧速率很低,但误报很多。

由于我刚开始使用OpenCV并且对C++也不太了解,所以我可能做出了错误的解释和昂贵的计算。

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    Mat frame = inputFrame.rgba();

    float minScale = 0.4f, maxScale = 5;    /* minimum and maximum scale to detect */
    int totalScales = 55;                   /* preferred number of scales between min and max */
    int threshold = -1;                     /* detections with score less then threshold will be ignored */

    HOGDescriptor hog = new HOGDescriptor();
    hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());

    MatOfRect foundLocations = new MatOfRect();
    MatOfDouble foundWeights = new MatOfDouble();

    Mat tempMat = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3);
    Imgproc.cvtColor(frame, tempMat, Imgproc.COLOR_RGBA2RGB);

    hog.detectMultiScale(tempMat, foundLocations, foundWeights, 1.5, new Size(8,8),
            new Size(32, 32), 1.05, 2, false);

    Vector<Rect> foundLocationsFilteredList = new Vector<Rect>();
    filterRects(foundLocations.toList(), foundLocationsFilteredList);

    for (Rect foundLocation : foundLocationsFilteredList) {
        Core.rectangle(frame, foundLocation.tl(), foundLocation.br(), new Scalar(0, 255, 0), 3);
    }

    tempMat.release();
    return frame;
}

private final void filterRects(List<Rect> candidates, List<Rect> objects) {
    for (int i = 0; i < candidates.size(); ++i) {
        Rect r = candidates.get(i);

        int j;
        for (j = 0; j < candidates.size(); ++j) {
            if (j != i && r.equals(candidates.get(j)))
                break;
        }

        if (j == candidates.size())
            objects.add(r);
    }
}
  • 如果使用JNI完成帧处理会更快吗?还是采用其他方法会更好?
  • 我应该如何进行多对象检测
  • 最佳答案

    JNI无疑加快了这一进程。在OpenCV4Android bundle 包中有一个JNI示例。我已经在Android上使用OpenCV和JNI编写了一些程序,对我来说效果很好。这是面部检测示例。用您自己的物体探测器更换探测器。

    对于各种检测器,在参数中指定检测多个或单个对象。转到文档。大多数情况下,检测器默认会返回多个结果。

    编辑

    这是使用OpenCV的JNI的示例。最初的跟踪器是C++,我将其改编成Android。欢迎叉。

    Link to Project

    关于android - 使用OpenCV在Android上进行多对象检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303553/

    相关文章:

    Python OpenCV - 检测手

    安卓NDK : How to get a Class Loader With a NativeActivity and native_app_glue?

    android - 使用 NDK 捕获标准输出/标准错误

    android - 我无法从 adb 版本 1.0.29 升级

    android - 发布与生成的不同的 apk Facebook 哈希 key

    android - 自定义导航动画

    android - 错误 : unknown type name 'class' NDK CDT JNI

    android - 如何: get the port number of AVD and send a message to itself

    c++ - 尽管有新的视频数据,OpenCV 直方图不会更新

    Python - 特征匹配关键点与 OpenCV 之间的距离