Ask Your Question

Problem with Background Subtraction KNN in android studio

asked 2019-10-06 00:51:14 -0500

wade wang gravatar image

I am trying to do background subtraction using android phone camera with android studio. I installed OpenCV4Android 3.4.7 and try to use Backgorund Subtraction KNN. I give the camera permission in manifests and can launch the camera correctly. However, I only got white(or blank) screen after run this App on my mobilephone. Here is my part of code:

    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        Mat frame = inputFrame.rgba();
        Mat fgMask = new Mat();
        BackgroundSubtractorKNN backSub = Video.createBackgroundSubtractorKNN();
        Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2GRAY);
        backSub.apply(frame, fgMask,-1);
        return fgMask;}

Anyone can help me ? Many thanks !

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2019-10-06 01:11:48 -0500

berak gravatar image

updated 2019-10-06 01:15:58 -0500

background subtraction needs MANY frames to learn from, you should not construct a new instance every time you want to use it. this line:

    BackgroundSubtractorKNN backSub = Video.createBackgroundSubtractorKNN();

should be moved into a "run once" function, like onCameraViewStarted()

have a look here, there''s a java sample, too !

edit flag offensive delete link more



Wow, you are my saviour !I modified the code according to what you said, it really works now ! You are so nice and so awesome ! I am so grateful for your help ! Hehe !

wade wang gravatar imagewade wang ( 2019-10-06 03:41:44 -0500 )edit

Question Tools

1 follower


Asked: 2019-10-06 00:51:14 -0500

Seen: 335 times

Last updated: Oct 06 '19