Ask Your Question
0

How to get the mass center of a contour ? Android opencv.

asked 2013-08-20 17:08:20 -0500

Peliks gravatar image

updated 2013-08-21 01:29:52 -0500

berak gravatar image

This is the c++ code.

vector<Moments> mu(contours.size() );
      for( int i = 0; i < contours.size(); i++ ){
          mu[i] = moments( contours[i], false );
      }

//Mass center
vector<Point2f> mc( contours.size() );
    for( int i = 0; i < contours.size(); i++ ){ 
        mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); 
    }

This is my code so far in android. i can't convert the mass center to android.

    //moments
List<Moments> mu = new ArrayList<Moments>(contours.size());
    for (int i = 0; i < contours.size(); i++) {
        mu.add(i, Imgproc.moments(contours.get(i), false));
    }

//mass center
    List<MatOfPoint2f> mc = new ArrayList<MatOfPoint2f>(contours.size()); 
    for( int i = 0; i < contours.size(); i++ ){
        mc.add((mu.get(i).get_m10() / mu.get(i).get_m00() , mu.get(i).get_m01()/mu.get(i).get_m00()));
    }

Error in this line :

mc.add((mu.get(i).get_m10() / mu.get(i).get_m00() , mu.get(i).get_m01()/mu.get(i).get_m00()));

Thanks in advance.

edit retag flag offensive close merge delete

Comments

Is it a logical error or do you have a real error message?

Moster gravatar imageMoster ( 2013-08-21 01:09:00 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-08-21 01:27:45 -0500

berak gravatar image

it gets easier to see, if you first construct a point from your moments:

for( int i = 0; i < contours.size(); i++ ) {
     Point2f p = new Point2f( mu.get(i).get_m10() / mu.get(i).get_m00() , mu.get(i).get_m01()/mu.get(i).get_m00() )
     mc.add(i, p);
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-08-20 17:08:20 -0500

Seen: 1,628 times

Last updated: Aug 21 '13