使用OpenCV进行Android光流计算

6
4个回答

1
看这个斯坦福的OpenCV光流链接。事情应该基本上是一样的,只是由于1.x对2.x的C vs C++ API问题,调用可能会稍有不同。
只需编辑CVCamera示例即可,而且应该很快。我在让CVCamera运行后一个小时内就制作了一个实时人脸检测应用程序。

我已经找到了,但是我的Java知识不足,无法在Android上运行.. =\ - thanos
这是第一步。我以为你已经从源代码构建了CVCamera示例并安装了它。请注意,这与仅下载.apk并进行操作非常不同。 - peakxu
我已经使用Cygwin Bash Shell构建了CVCamera,并在Eclipse中打开它,没有任何错误。还改变了分辨率,以便更快地运行CVCamera具有的特征检测。 - thanos
如果您已经成功构建和运行了CVCamera,那么阅读一份关于Java本地接口(JNI)的快速教程并不太难。我展示的光流链接也几乎完全使用C / C ++编写。 - peakxu

0
这段代码将帮助您获取光学向量,并跟踪它们。
@Override public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    if (mMOP2fptsPrev.rows() == 0) {

        //Log.d("Baz", "First time opflow");
        // first time through the loop so we need prev and this mats
        // plus prev points
        // get this mat
        Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);

        // copy that to prev mat
        matOpFlowThis.copyTo(matOpFlowPrev);

        // get prev corners
        Imgproc.goodFeaturesToTrack(matOpFlowPrev, MOPcorners, iGFFTMax, 0.05, 20);
        mMOP2fptsPrev.fromArray(MOPcorners.toArray());

        // get safe copy of this corners
        mMOP2fptsPrev.copyTo(mMOP2fptsSafe);
        }
    else
        {
        //Log.d("Baz", "Opflow");
        // we've been through before so
        // this mat is valid. Copy it to prev mat
        matOpFlowThis.copyTo(matOpFlowPrev);

        // get this mat
        Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);

        // get the corners for this mat
        Imgproc.goodFeaturesToTrack(matOpFlowThis, MOPcorners, iGFFTMax, 0.05, 20);
        mMOP2fptsThis.fromArray(MOPcorners.toArray());

        // retrieve the corners from the prev mat
        // (saves calculating them again)
        mMOP2fptsSafe.copyTo(mMOP2fptsPrev);

        // and save this corners for next time through

        mMOP2fptsThis.copyTo(mMOP2fptsSafe);
        }


    /*
    Parameters:
        prevImg first 8-bit input image
        nextImg second input image
        prevPts vector of 2D points for which the flow needs to be found; point coordinates must be single-precision floating-point numbers.
        nextPts output vector of 2D points (with single-precision floating-point coordinates) containing the calculated new positions of input features in the second image; when OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
        status output status vector (of unsigned chars); each element of the vector is set to 1 if the flow for the corresponding features has been found, otherwise, it is set to 0.
        err output vector of errors; each element of the vector is set to an error for the corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't found then the error is not defined (use the status parameter to find such cases).
    */
    Video.calcOpticalFlowPyrLK(matOpFlowPrev, matOpFlowThis, mMOP2fptsPrev, mMOP2fptsThis, mMOBStatus, mMOFerr);

    cornersPrev = mMOP2fptsPrev.toList();
    cornersThis = mMOP2fptsThis.toList();
    byteStatus = mMOBStatus.toList();

    y = byteStatus.size() - 1;

    for (x = 0; x < y; x++) {
        if (byteStatus.get(x) == 1) {
            pt = cornersThis.get(x);
            pt2 = cornersPrev.get(x);

            Core.circle(mRgba, pt, 5, colorRed, iLineThickness - 1);

            Core.line(mRgba, pt, pt2, colorRed, iLineThickness);
            }
        }

return mRgba;

    }

0

谢谢你的帮助!OpenCV 2.3.0中的光流示例在哪里?我已经下载了它,但是我找不到任何光流实现,我只发现如何使用FAST、SURF和STAR在CVCamera中检测特征。 - thanos
那就是这些例子吗?你需要使用(cv :: calcOpticalFlowPyrLK)编写自己的代码,并使用 NDK 重新构建[http://opencv.willowgarage.com/wiki/OpenCVAndroidBinariesBuild]。 - shernshiou
另一方面,您可以关注https://groups.google.com/group/android-opencv/browse_thread/thread/c0c011cb8e7f3e0c#,我猜这位家伙成功构建了一个光流应用程序。 - shernshiou

0

虽然我也在尝试做同样的事情,但现在OpenCV4Android中似乎有更多支持光流的支持。 看一下org.opencv.video中的API OpenCV Java文档 我看到了calcOpticalFlowPyrLK和calcOpticalFlowFarneback。 我成功地让calcOpticalFlowFarneback工作了(尽管结果似乎并不那么好,可能需要调整参数) calcOpticalFlowPyrLK正在变得棘手。我似乎无法将FeatureDetector类(MatOfKeyPoint)返回的关键点转换为calcOpticalFlowFarneback所需的点(MatOfPoint2f)其他线程


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接