OpenCV Android Background Subtraction

前端 未结 3 1581
醉梦人生
醉梦人生 2021-01-02 11:43

I am working on a robotics project using an Android phone as the main processor and the camera to detect movement. I got the Android binary package from OpenCV and got it co

相关标签:
3条回答
  • 2021-01-02 12:27

    Also with latest openCV you need to initialize with:

    private BackgroundSubtractorMOG2 Sub = Video.createBackgroundSubtractorMOG2();

    0 讨论(0)
  • 2021-01-02 12:41

    Thanks you guys so much! And for future viewers who come to this page, you might have to tweak this knowledge to get things working. In SDK v2.4.4 I applied this in the onCameraFrame method. Recall that the method takes in an input frame from the camera. You use the input and return the frame that is to be displayed on the screen of your android device. Here's an example:

    //Assume appropriate imports    
    private BackgroundSubtractorMOG sub = new BackgroundSubtractorMOG(3, 4, 0.8);
    private Mat mGray = new Mat();
    private Mat mRgb = new Mat();
    private Mat mFGMask = new Mat();
    
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mGray = inputFrame.gray(); //I chose the gray frame because it should require less resources to process
        Imgproc.cvtColor(mGray, mRgb, Imgproc.COLOR_GRAY2RGB); //the apply function will throw the above error if you don't feed it an RGB image
        sub.apply(mRgb, mFGMask, learningRate); //apply() exports a gray image by definition
    
        return mFGMask;
    }
    

    To get across my point about the gray image that comes out of apply(), if you wanted to do a RGBA version, you might have to use a cvtcolor after the call to apply():

    private Mat mRgba = new Mat();
    
    public Mat onCameraFrame(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, learningRate); //apply() exports a gray image by definition
        Imgproc.cvtColor(mFGMask, mRgba, Imgproc.COLOR_GRAY2RGBA);
    
        return mRgba;
    }
    
    0 讨论(0)
  • 2021-01-02 12:49

    Have you tried using cvtColor with CV_RGB2RGBA and CV_RGBA2RGB. So, maybe try converting frame RGBA to RGB, then do background subtraction. Something like this:

    protected void processFrame(VideoCapture capture) {
        capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
        Mat rgb;
        Imgproc.cvtColor(mRgba, rgb, Imgproc.COLOR_RGBA2RGB);
        mBGSub.apply(rgb, mFGMask);
    }
    

    EDIT : You might check out the OpenCV unit-test for BackgroundSubtractorMOG located here. However, the test has fail("Not yet implemented"); in the main test case.

    I'm not sure if that means the test isn't complete, or the support for BackgroundSubtractorMOG is not implemented. You might try running the code contained in this unit-test to see if it actually works.

    Also, the C++ sample segment_objects.cpp might be helpful as a usage example.

    Hope that helps! :)

    0 讨论(0)
提交回复
热议问题