Ask Your Question
0

Android: Why can't I initialize BackgroundSubtractor with Video.createBackgroundSubtractorMOG2()?

asked 2019-03-27 08:51:10 -0600

I implemented my class with CameraBridgeViewBase.CvCameraViewListener2. And here is my onCameraFrame() method that does the processing after frame grabbing before rendering the frame on screen:

   @Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat frame=(Mat)inputFrame;
    Mat fgMask=new Mat();
    BackgroundSubtractor bs=Video.createBackgroundSubtractorMOG2();
    bs.apply(frame,fgMask);
    //return 4 channel as rgba or 1 channel as gray scale
    return inputFrame.rgba();
}

I learn the syntax from here.

The problem is that Android Studio says 'Cannot resolve method createBackgroundSubtractorMOG2()' after I imported the Video class.

edit retag flag offensive close merge delete

Comments

code runs fine on desktop java, so the problem unfortunately is between you and your ide (and there's not much we can do from here) ;(

apart from that,you have another problem: it does not make any sense, to create a new bgsubtractor per frame (onCameraFrame is the wrong place to create one).

berak gravatar imageberak ( 2019-03-28 00:46:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-03-29 11:24:44 -0600

I did it in onCameraFrame():

    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mRgb, Imgproc.COLOR_RGBA2RGB); //the apply function will throw the above error if you don't feed it an RGB image
    sub.apply(mRgb, mFGMask, -1); //apply() exports a gray image by definition
    Imgproc.cvtColor(mFGMask, mRgba, Imgproc.COLOR_GRAY2RGBA);

    return mRgba;
}

The global vars needed:

 private BackgroundSubtractorMOG2 sub = new BackgroundSubtractorMOG2(10, 25, false);
private Mat mRgb=new Mat();
private Mat mFGMask=new Mat();
private Mat mRgba=new Mat();

Credits to Austin from Stack Overflow.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-03-27 08:51:10 -0600

Seen: 425 times

Last updated: Mar 29 '19