OpenCV BackgroundSubtractor Android运动检测器错误

时间:2016-11-23 09:44:57

标签: java android opencv motion-detection background-subtraction

当我尝试使用openCV的BackgroundSubtractoeMOG2制作动作探测器应用程序时(应用程序将捕获摄像头并围绕检测到的动作绘制轮廓),我得到了Error while giving argument

MainActivity.java

package com.example.administrator.urapplication;

import java.util.ArrayList;
import java.util.List;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.BackgroundSubtractorMOG2;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.SeekBar;


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

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

private BackgroundSubtractorMOG2 sub;
private Mat mGray;
private Mat mRgb;
private Mat mFGMask;
private List<MatOfPoint> contours;
private double lRate = 0.5;

private SeekBar sb;

// Initialization required by apps using OpenCV Manager
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 MainActivity() {
    //Log.i(TAG, "Instantiated new " + this.getClass());
}

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

    setContentView(R.layout.show_camera);

    //sets up camera

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.show_camera_activity_java_surface_view);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

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

public void onResume() {
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
}

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

public void onCameraViewStarted(int width, int height) {
    //creates a new BackgroundSubtractorMOG class with the arguments
    sub = new BackgroundSubtractorMOG2(3, 4, 0.8, 0.5);

    //creates matrices to hold the different frames
    mRgb = new Mat();
    mFGMask = new Mat();
    mGray = new Mat();

    //arraylist to hold individual contours
    contours = new ArrayList<MatOfPoint>();

}

public void onCameraViewStopped() {
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    contours.clear();
    //gray frame because it requires less resource to process
    mGray = inputFrame.gray();

    //this function converts the gray frame into the correct RGB format for the BackgroundSubtractorMOG apply function
    Imgproc.cvtColor(mGray, mRgb, Imgproc.COLOR_GRAY2RGB);

    //apply detects objects moving and produces a foreground mask
    //the lRate updates dynamically dependent upon seekbar changes
    sub.apply(mRgb, mFGMask, lRate);

    //erode and dilate are used  to remove noise from the foreground mask
    Imgproc.erode(mFGMask, mFGMask, new Mat());
    Imgproc.dilate(mFGMask, mFGMask, new Mat());

    //drawing contours around the objects by first called findContours and then calling drawContours
    //RETR_EXTERNAL retrieves only external contours
    //CHAIN_APPROX_NONE detects all pixels for each contour
    Imgproc.findContours(mFGMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    //draws all the contours in red with thickness of 2
    Imgproc.drawContours(mRgb, contours, -1, new Scalar(255, 0, 0), 2);

    return mRgb;
}

}

show_camera.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<org.opencv.android.JavaCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    android:id="@+id/show_camera_activity_java_surface_view"
    opencv:show_fps="true"
    opencv:camera_id="any" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

OpenCV3中的BackgroundSubtractorMOG2没有这样的构造函数,你应该使用OpenCV2.4中的BackgroundSubtractorMOG