Pregunta

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.

¿Fue útil?

Solución

This code will find the position of all contours.

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));
        Moments p = mu.get(i);
        int x = (int) (p.get_m10() / p.get_m00());
        int y = (int) (p.get_m01() / p.get_m00());
        Core.circle(rgbaImage, new Point(x, y), 4, new Scalar(255,49,0,255));
    }

Otros consejos

Assuming you have the contour stored in a List<Point>, you can do this:

MatOfPoint mop = new MatOfPoint();
mop.fromList(contour);

Moments moments = Imgproc.moments(mop);

Point centroid = new Point();

centroid.x = moments.get_m10() / moments.get_m00();
centroid.y = moments.get_m01() / moments.get_m00();

Try

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

instead of

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

Since, mc is MathOfPoint2f type and its constructor needs Point type parameter.

NB: Though this question asked about 2years ago, I give this answer because I think this is the exact conversion of that c++ code. This works for me.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top