1

我是 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();
    }
}

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

4

2 回答 2

0

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

channels() == 1

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

于 2014-05-06T17:54:21.113 回答
0

首先,耳流非常缓慢。使用 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.
于 2013-12-17T07:50:10.927 回答