OpenCV BackgroundSubtractor Android 运动检测器错误

Posted

技术标签:

【中文标题】OpenCV BackgroundSubtractor Android 运动检测器错误【英文标题】:OpenCV BackgroundSubtractor Android Motion Detector Error 【发布时间】:2016-11-23 09:44:57 【问题描述】:

当我尝试使用 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_
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_>
<org.opencv.android.JavaCameraView
    android:layout_
    android:layout_
    android:visibility="gone"
    android:id="@+id/show_camera_activity_java_surface_view"
    opencv:show_fps="true"
    opencv:camera_id="any" />
    </LinearLayout>

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于OpenCV BackgroundSubtractor Android 运动检测器错误的主要内容,如果未能解决你的问题,请参考以下文章

cmake错误:opencv2/opencv.hpp:opencv2/opencv.hpp:没有这样的文件或目录

opencv相机标定

opencv4opencv视频教程 C++(opencv教程)1opencv介绍和环境搭建

如何使用opencv实现图像匹配

OpenCV 简介 携手走进 OpenCV 的世界

如何利用openCV函数查看opencv版本