Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Android absdiff not working

Hello,

I want to create a motion detector in a OpenCV Android app.

First I accumulate the background with this class:

private Mat                    mRgba;
private Mat                    mGray;
private Mat                    mBackground;
private Mat                    mDifference;

public Mat accumulateImageBG(Mat img, int type){
    if (accuImg==null){//code snipit from berak's answer
        accuImg = Mat.zeros(img.size(), type);
    }

    if (mDifference==null){//code snipit from berak's answer
        mDifference = Mat.zeros(img.size(), CvType.CV_8UC1);
    }

    img.convertTo(img, type);
    Imgproc.accumulateWeighted(img, accuImg, 0.1);
    Core.convertScaleAbs(accuImg, img);
    return img;
}

And then I want to calculate the difference between the background and the camera frame. I tried this code:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();//in:CV_8UC4 out:CV_32FC4

    //be sure to specify conversion type with matching # of channels as input image!
    mBackground = accumulateImageBG(mGray, CvType.CV_32F);

    // Subtract the work image frame from the scaled image average
    Core.absdiff(mBackground, mGray, mDifference);

    return mDifference;
}

But it's always returning a blank frame. I think there is a problem with my absdiff code. Does anybody know if I did something wrong? I already tested with 3.1.0, 3.0.0 and 2.4.11 versions.

Any tip will be very helpful,

Thanks

OpenCV Android absdiff not working

Hello,

I want to create a motion detector in a OpenCV Android app.

First I accumulate the background with this class:

private Mat                    mRgba;
private Mat                    mGray;
private Mat                    mBackground;
private Mat                    mDifference;

public Mat accumulateImageBG(Mat img, int type){
    if (accuImg==null){//code snipit from berak's answer
        accuImg = Mat.zeros(img.size(), type);
    }

    if (mDifference==null){//code snipit from berak's answer
        mDifference = Mat.zeros(img.size(), CvType.CV_8UC1);
    }

    img.convertTo(img, type);
    Imgproc.accumulateWeighted(img, accuImg, 0.1);
    Core.convertScaleAbs(accuImg, img);
    return img;
}

And then I want to calculate the difference between the background and the camera frame. I tried this code:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();//in:CV_8UC4 out:CV_32FC4
    mGray = inputFrame.gray();//in:CV_8UC1 out:CV_32F

    //be sure to specify conversion type with matching # of channels as input image!
    mBackground = accumulateImageBG(mGray, CvType.CV_32F);

    // Subtract the work image frame from the scaled image average
    Core.absdiff(mBackground, mGray, mDifference);

    return mDifference;
}

But it's always returning a blank frame. I think there is a problem with my absdiff code. Does anybody know if I did something wrong? I already tested with 3.1.0, 3.0.0 and 2.4.11 versions.

Any tip will be very helpful,

Thanks