计算opencv android中的光流

时间:2013-11-08 03:59:21

标签: android opencv opticalflow

我是OpenCV的初学者,并尝试使用OpenCV的calcOpticalFlowFarneback方法计算光流。

请看一下我的实施:

package org.opencv.samples.tutorial1;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.video.Video;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;

public class Tutorial1Activity extends Activity implements CvCameraViewListener2 {
    private static final String TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;
    private boolean              mIsJavaCamera = true;
    private MenuItem             mItemSwitchCamera = null;

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    public Tutorial1Activity() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.tutorial1_surface_view);

        if (mIsJavaCamera)
            mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
        else
            mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);

        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);

        mOpenCvCameraView.setCvCameraViewListener(this);
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "called onCreateOptionsMenu");
        mItemSwitchCamera = menu.add("Toggle Native/Java camera");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String toastMesage = new String();
        Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);

        if (item == mItemSwitchCamera) {
            mOpenCvCameraView.setVisibility(SurfaceView.GONE);
            mIsJavaCamera = !mIsJavaCamera;

            if (mIsJavaCamera) {
                mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
                toastMesage = "Java Camera";
            } else {
                mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
                toastMesage = "Native Camera";
            }

            mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
            mOpenCvCameraView.setCvCameraViewListener(this);
            mOpenCvCameraView.enableView();
            Toast toast = Toast.makeText(this, toastMesage, Toast.LENGTH_LONG);
            toast.show();
        }

        return true;
    }



    public void onCameraViewStarted(int width, int height) {
    }

    public void onCameraViewStopped() {

    }

    Mat first_frame = null;
    Mat second_frame = null;
    boolean dumped = false;
    //this function is called on each frame.
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {



        // I wanted to calculate the optical flow like this.
        first_frame = second_frame;
        if(first_frame == null || second_frame == null){
            second_frame = inputFrame.rgba();
            return inputFrame.rgba();
        }

        //now we have two consecutive frame.
        Mat flow = new Mat(inputFrame.rgba().size(), CvType.CV_8UC1);

        Video.calcOpticalFlowFarneback(first_frame, second_frame, flow,0.5,1, 1, 1, 7,
                1.5,1);
        System.out.println("flow = "+flow.dump());

        return inputFrame.rgba();
    }
}

它给出错误,因为可能是我在参数中输入了错误的值。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

首先,otical flow非常慢。 使用cvCameraViewListener2时,我只能在不改变图像的情况下重新扫描大约10 fps。因此,您(可能)不会获得良好的帧速率。

第二次它现在超过一年前我最后用光流做了一些事情。 第二次调用onCameraFrame时设置first_frame = second_frame。 但是你没有把实际的帧放到你的second_frame中。您的first_frame仍然等于您的second_frame。

first_frame = second_frame;
    if(first_frame == null || second_frame == null){
        second_frame = inputFrame.rgba();
        return inputFrame.rgba();
    }
//ADD THIS:
second_frame = inputFrame.rgba();


    //now we have two consecutive frame.

答案 1 :(得分:0)

为了使calcOpticalFlowFarneback工作,第一帧和第二帧应该有。

channels() == 1

我不确定是否有办法转换它。