OpenCV Android Background Subtraction

前端 未结 3 1667
醉梦人生
醉梦人生 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: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;
    }
    

提交回复
热议问题