Moments have reading of 0.0 in Android Q 10.0

asked 2019-05-10 03:00:34 -0500

bow_one gravatar image

updated 2019-05-10 03:14:35 -0500

System information (version) OpenCV => 4.1.0 Operating System / Platform => Android Compiler => Android Studio

I am trying to get moments, but all reading reads 0.0 for all elements in the moments array. However, on Android P 9.0 and lower, I am able to get a proper reading of the same image.

Code where I am using moments to find the center point in an image based on some identifying feature in the middle:

Mat imgToTest = new Mat (image.getWidth(), image.getHeight(), CvType.CV_8UC3);
        Utils.bitmapToMat(imgBitmap, imgToTest);
        Imgproc.cvtColor(imgToTest, imgToTest, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(imgToTest, imgToTest, 0, 20, Imgproc.THRESH_BINARY_INV);
        ArrayList<MatOfPoint> ctrsTest = new ArrayList<MatOfPoint>();
        Imgproc.findContours(imgToTest, ctrsTest, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        MatOfPoint cnt = ctrsTest.get(0);
        Moments moments = Imgproc.moments(cnt);

    Log.d(TAG, "moments: " + moments.toString());

        centroid_x = (int) (moments.get_m10() / moments.get_m00());
        centroid_y = (int) (moments.get_m01() / moments.get_m00());

Below is the log where I am trying to determine moment in an image but returns reading of 0.0

moments: Moments [ 
    m10=0.0, m01=0.0, 
    m20=0.0, m11=0.0, m02=0.0, 
    m30=0.0, m21=0.0, m12=0.0, m03=0.0, 
    mu20=0.0, mu11=0.0, mu02=0.0, 
    mu30=0.0, mu21=0.0, mu12=0.0, mu03=0.0, 
    nu20=0.0, nu11=0.0, nu02=0.0, 
    nu30=0.0, nu21=0.0, nu12=0.0, nu03=0.0, 

Any assistance is appreciated. If this is a bug, then I will submit an issue on the Github repo.


edit retag flag offensive close merge delete


show (relevant piece of) code, please.

also, do you have an example image or contour ?

berak gravatar imageberak ( 2019-05-10 03:10:32 -0500 )edit

The image has a plain white background with a black dot in the center.

bow_one gravatar imagebow_one ( 2019-05-10 03:15:18 -0500 )edit

is there still something in your thresholded image ?

berak gravatar imageberak ( 2019-05-10 03:24:03 -0500 )edit

There is that black dot in the center. The code works and would give me the coordinates of the point using the same OpenCV version on older Android versions. But on Android 10, it just returns reading of 0.0.

bow_one gravatar imagebow_one ( 2019-05-10 03:25:36 -0500 )edit

no, i mean: did you really go and check, if the dot survives the thresholding (and that exactly 1 contour is left there) ?

or did you expect it to happpen ?

berak gravatar imageberak ( 2019-05-10 03:31:36 -0500 )edit

I will check more into this first and report my findings, thanks for the tip

bow_one gravatar imagebow_one ( 2019-05-10 04:53:15 -0500 )edit