Stack Exchange
error in apply method in Background subtraction in opencv android
I want to do background subtraction on InputFrame, and want to return foreground mask.
I am facing error as bellow can any one help me to fix it 
Thanks in advance 
My Code:
package org.opencv.samples.tutorial1;

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.Point;
import org.opencv.core.Scalar;
import org.opencv.video.BackgroundSubtractorMOG;
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.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 = "Divyesh::Activity";

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

    public Point p1 = new Point(10, 2);
    public Point p2 = new Point(100, 20);
    public  Scalar scalar = new Scalar(255, 255, 255);
    public Mat fgMask;
    public Mat mRgba, mGray, mRgb;
    //public BackgroundSubtractor pMog, pMog2;
    BackgroundSubtractorMOG pMog;


    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) {
        mRgba = new Mat();
        mGray = new Mat();
        mRgb = new Mat();
        fgMask = new Mat();
        Log.i(TAG, "Before subtractor");
        pMog = new BackgroundSubtractorMOG();
    }

    public void onCameraViewStopped() {
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Log.i(TAG, "Before input frame");
        mRgba = inputFrame.rgba();
        //Imgproc.cvtColor(mRgba, mRgb, Imgproc.COLOR_RGBA2RGB);
        Log.i(TAG,"Before apply");
        pMog.apply(mRgba, fgMask);
        Log.i(TAG,"After apply");
        //Imgproc.cvtColor(fgMask, mRgba, Imgproc.COLOR_GRAY2RGBA);
        return fgMask;

    }
}

My error is at here:
/**
 * Computes a foreground mask.
 *
 * @param image Next video frame.
 * @param fgmask The output foreground mask as an 8-bit binary image.
 *
 * @see org.opencv.video.BackgroundSubtractor.operator()
 */
public  void apply(Mat image, Mat fgmask)
{

    apply_1(nativeObj, image.nativeObj, fgmask.nativeObj);

    return;
}

Thanx a lot if you can solve it or can give some instruction...........

now what error do you get exactly ?

I m getting blank screen at run time....
but frame are still receiving according to LOGs

that might be unpleasant, but not a real error. a model has to get trained there, you usually don't get the correct result in the 1st frame returned. play with params like history.

brother its not working
What could be there in History????

the number of frames used to average the mog. and the outcome would be a very thin outline of the moving parts in the foreground.

but I m not even getting frame output on screen ..............

I put parameters like(200, 2, 0.7)
it is not working yet
according to you what could be here??????

I would turn off bg subtraction and make sure that you are acquiring images correctly.

I haven't used or worked with Background Subtraction before but try this following edit in your code.
mRgba = inputFrame.rgba();
Imgproc.cvtColor(mRgba, mRgb, Imgproc.COLOR_RGBA2BGR,4);
Log.i(TAG,"Before apply");
pMog.apply(mRgb, fgMask);
Log.i(TAG,"After apply");
Imgproc.cvtColor(fgMask, mRgb, Imgproc.COLOR_GRAY2BGR,3);
Imgproc.cvtColor(mRgb, mRgba, Imgproc.COLOR_BGR2RGBA,4);
return fgMask;

Also check this link. I think it might help:http://www.codeproject.com/Articles/791145/Motion-Detection-in-Android-Howto

Hope this helps.
