Ask Your Question
0

What are the correct data types for contours and moments?

asked 2018-02-15 16:49:55 -0600

Marko5280 gravatar image

updated 2018-02-15 17:13:58 -0600

I have a running program for the Lego in C++. Now, I find out that LeJos for the Mindstorm EV3 only has an engine for Java for Eclipse. So, I need to convert my running C++ program to Java in Eclipse to have a link to the EV3. But, I am lost in the data structures. List, Array, Array-of-List, Mat, Array-of-Mat, Mat-of-Point, whatever... I have converted most of the code, but am stuck on the following...

Someone must know how to do this. THANK YOU

  //            vector<Moments> mu(  contours.size()  );                //  C++
   List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
   ArrayList mu = new ArrayList<contours.size()>  ();
   Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY);
   Imgproc.threshold(dst, dst, 0, 255, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU);
   Imgproc.findContours(
     dst,                          //  Image              
     contours,                     //  Contours      
     hierarchy,                    //  Hierarchy                    
     Imgproc.RETR_TREE,     //int - Contour retrieval mode: RETR_LIST, RETR_CCOMP
     Imgproc.CHAIN_APPROX_NONE);//int -ContourapproximantionMethod:CHAIN_APPROX_SIMPLE, CHAIN_APPROX_NONE
    HighGui.imshow("After findContours.   Press any key ...", dst);
    HighGui.waitKey();
    int SumOfMoments = 0;
    int Mass = 0;

    for (int i = 0; i < contours.size(); i++)      //  From C++
    {                                              //  From C++
        mu[i] = moments(contours[i], false);       //  From C++
        SumOfMoments += mu[i].m10;                 //  From C++
        Mass += mu[i].m00;                         //  From  C++
    }
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2018-02-15 18:43:14 -0600

updated 2018-02-15 18:50:18 -0600

Well, according to their documentation you can calculate the moment using

Imgproc.moments(Mat array)

or

Imgproc.moments(Mat array, boolean binaryImage)

This returns a Moment type.

I have not used the Java API but after glancing through their documentation, the relative Java code is

List<Moments> contMoments = new ArrayList<Moments>();

// I made this a double instead of an int because of (1)
double sumOfMoments = 0, mass = 0;

for(int i = 0; i < contors.size(); ++i)
{
    Moments thisMoment = Improc.moments(contours[i]);
    sumOfMoments += thisMoment.get_m10();
    mass += thisMoment.get_m00();
    // (1) .get_m00() and .get_m10() both return doubles

    contMoments.add(thisMoment); // finally add it to your collection
}

Here's the moments class

Always refer to the documentation.

Happy coding,

Cheers :)

edit flag offensive delete link more
0

answered 2018-02-18 07:35:58 -0600

Marko5280 gravatar image

updated 2018-02-18 07:38:47 -0600

And it also seems to work this way ...

for(MatOfPoint cont : contours)            
 {            
    Count += 1;
    Moments mu = Imgproc.moments(cont, false);
    SumOfMoments += mu.get_m10();
    Mass += mu.get_m00();
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-02-15 16:49:55 -0600

Seen: 1,007 times

Last updated: Feb 18 '18