Moments have reading of 0.0 in Android Q 10.0
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 [
m00=0.0,
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.
Thanks
show (relevant piece of) code, please.
also, do you have an example image or contour ?
The image has a plain white background with a black dot in the center.
is there still something in your thresholded image ?
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.
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 ?
I will check more into this first and report my findings, thanks for the tip