opencv: imgproc.Moments missing in OpenCV 3.0 for Android

Transferred from http://code.opencv.org/issues/4404

|| Karim Virani on 2015-06-13 15:44
|| Priority: High
|| Affected: branch 'master' (3.0-dev)
|| Category: android
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: Any / Android

imgproc.Moments missing in OpenCV 3.0 for Android

imgproc.Moments seems to have disappeared in openCVLibrary300 for java.

Looking in Improc.java it says this:

// Return type 'Moments' is not supported, skipping the function
Same for huMoments

These were supported as of 2.4.11

In the 3.0 android distribution, the whole Moments.java file seems to be missing from the imgproc folder and bindings seem removed from Imgproc.java

This breaks a lot of working code.

History

Maksim Shabunin on 2015-06-15 12:25
-   Target version changed from 3.0 to 3.1
-   Assignee changed from Alexander Smorkalov to Maksim Shabunin

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 21 (2 by maintainers)

Most upvoted comments

For who need it now. Add this into Imgproc.java
public static Moments contourMoments( MatOfPoint contour ) { Moments m = new Moments(); int lpt = contour.checkVector(2); boolean is_float = true;//(contour.depth() == CvType.CV_32F); Point[] ptsi = contour.toArray(); //PointF[] ptsf = contour.toArray();

                    //CV_Assert( contour.depth() == CV_32S || contour.depth() == CV_32F );

                    if( lpt == 0 )
                            return m;

                    double a00 = 0, a10 = 0, a01 = 0, a20 = 0, a11 = 0, a02 = 0, a30 = 0, a21 = 0, a12 = 0, a03 = 0;
                    double xi, yi, xi2, yi2, xi_1, yi_1, xi_12, yi_12, dxy, xii_1, yii_1;


                    {
                            xi_1 = ptsi[lpt-1].x;
                            yi_1 = ptsi[lpt-1].y;
                    }

                    xi_12 = xi_1 * xi_1;
                    yi_12 = yi_1 * yi_1;

                    for( int i = 0; i < lpt; i++ )
                    {

                            {
                                    xi = ptsi[i].x;
                                    yi = ptsi[i].y;
                            }

                            xi2 = xi * xi;
                            yi2 = yi * yi;
                            dxy = xi_1 * yi - xi * yi_1;
                            xii_1 = xi_1 + xi;
                            yii_1 = yi_1 + yi;

                            a00 += dxy;
                            a10 += dxy * xii_1;
                            a01 += dxy * yii_1;
                            a20 += dxy * (xi_1 * xii_1 + xi2);
                            a11 += dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi));
                            a02 += dxy * (yi_1 * yii_1 + yi2);
                            a30 += dxy * xii_1 * (xi_12 + xi2);
                            a03 += dxy * yii_1 * (yi_12 + yi2);
                            a21 += dxy * (xi_12 * (3 * yi_1 + yi) + 2 * xi * xi_1 * yii_1 +
                                    xi2 * (yi_1 + 3 * yi));
                            a12 += dxy * (yi_12 * (3 * xi_1 + xi) + 2 * yi * yi_1 * xii_1 +
                                    yi2 * (xi_1 + 3 * xi));
                            xi_1 = xi;
                            yi_1 = yi;
                            xi_12 = xi2;
                            yi_12 = yi2;
                    }
                    float FLT_EPSILON = 1.19209e-07f;
                    if( Math.abs(a00) > FLT_EPSILON )
                    {
                            double db1_2, db1_6, db1_12, db1_24, db1_20, db1_60;

                            if( a00 > 0 )
                            {
                                    db1_2 = 0.5;
                                    db1_6 = 0.16666666666666666666666666666667;
                                    db1_12 = 0.083333333333333333333333333333333;
                                    db1_24 = 0.041666666666666666666666666666667;
                                    db1_20 = 0.05;
                                    db1_60 = 0.016666666666666666666666666666667;
                            }
                            else
                            {
                                    db1_2 = -0.5;
                                    db1_6 = -0.16666666666666666666666666666667;
                                    db1_12 = -0.083333333333333333333333333333333;
                                    db1_24 = -0.041666666666666666666666666666667;
                                    db1_20 = -0.05;
                                    db1_60 = -0.016666666666666666666666666666667;
                            }

                            // spatial moments
                            m.m00 = a00 * db1_2;
                            m.m10 = a10 * db1_6;
                            m.m01 = a01 * db1_6;
                            m.m20 = a20 * db1_12;
                            m.m11 = a11 * db1_24;
                            m.m02 = a02 * db1_12;
                            m.m30 = a30 * db1_20;
                            m.m21 = a21 * db1_60;
                            m.m12 = a12 * db1_60;
                            m.m03 = a03 * db1_20;

                           m.completeState();
                    }
                    return m;
            }

PepperX,

Prior to the workaround, to get the moments, you’d call the regular moments method in Imgproc, namely

Imgproc.moment(Mat array);

That method doesn’t work. Whatisor’s workaround is the new methods he presents for Imgproc.

Imgproc.contourMoments(MapOfPoint contour);

I copied that into the version of Imgproc under the OpenCVLibrary310 module in my project, and used that with a MatOfPoint contour to get the moments.

Hope that helps,

tonyc

On Tue, Oct 25, 2016 at 1:42 AM, PepperX notifications@github.com wrote:

@tonyc10 https://github.com/tonyc10

What is the right name/signature of the existing method ?

my declarations of MatOfPoint looks like:

List<MatOfPoint> contours = new ArrayList<>(); then with findcontours() and drawcontours() works well.

But with List<Moments> mu = new ArrayList<>(contours.size());

The problem begins.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/opencv/opencv/issues/5017#issuecomment-255950393, or mute the thread https://github.com/notifications/unsubscribe-auth/ADjrbYrXbt0V1q2x1elyZlceN9JjjgSYks5q3aTrgaJpZM4FgVhM .

PepperX – I think you took the right steps. The Moments should all be calculated correctly using whatisor’s code.
Note that whatinor’s method has a slightly different name/signature than the existing one. Also, are you passing a valid contour (MatOfPoint) to the method? What do the moments look like in the resulting Moment object?

Thanks @whatisor ,but i still have a problem after add the public static on imgproc.java ,i don’t know what should i do after that , my syntax Moments still red ,someone can help me please ? hopefully someone can help me 😃 Thanks
moments red