Ask Your Question

Revision history [back]

Okay you have basically 3 questions. Let us start with the first one, calculating the average moment.

// Collect your contours
Mat binary_image;
vector< vector<Point> > countours;
findContours(binary_image, contours, CV_EXTR_EXTERNAL, CV_CHAIN_APPROX_NONE);
// Now calculate the Moments of all of your contours
vector< Moments > moments_data;
for(size_t i=0; i<contours.size(); i++){
    Moments temp = moments(contours[i]);
    moments_data.push_back(temp);
}
// If you now want the average of all moments, then you need to average
double amount =  (double)moments_data.size();
// Create average containers for all moments
double a_m00 = 0.0, a_m01 = 0.0, a_m10 = 0.0, a_m20 = 0.0, a_m11 = 0.0, a_m02 = 0.0, a_m30 = 0.0, a_m21 = 0.0, a_m12 = 0.0, a_m03 = 0.0;
for(size_t i=0; i<moments_data.size(); i++){
     a_m00 = a_m00 + moments_data[i].m00;
     a_m01 = a_m01 + moments_data[i].m01;
     a_m10 = a_m10 + moments_data[i].m10;
     a_m20 = a_m20 + moments_data[i].m20;
     a_m11 = a_m11 + moments_data[i].m11;
     a_m02 = a_m02 + moments_data[i].m02;
     a_m30 = a_m30 + moments_data[i].m30;
     a_m21 = a_m21 + moments_data[i].m21;
     a_m12 = a_m12 + moments_data[i].m12;
     a_m03 = a_m03 + moments_data[i].m03;
}
// Now divide by the total amount of measurements amount
a_m00 = a_m00 / amount;
a_m01 = a_m01 / amount;
a_m10 = a_m10 / amount;
a_m20 = a_m20 / amount;
a_m11 = a_m11 / amount;
a_m02 = a_m02 / amount;
a_m30 = a_m30 / amount;
a_m21 = a_m21 / amount;
a_m12 = a_m12 / amount;
a_m03 = a_m03 / amount;

Your second question is about the same for the arc lenght. Basically the code is identical but you should

  • Make a vector< double > lengths instead of vector< Moments > moments_data; to collect the arclength of all measurements
  • Then make again the sum of all values and divide by the total amount of measurements

As to your third question, yes you can calculate area by looping over all non zero pixels, but that is quite cumbersome and computational expensive. You are better using the builtin functions provided for contours.

Okay you have basically 3 questions. Let us start with the first one, one, calculating the average moment.

// Collect your contours
Mat binary_image;
vector< vector<Point> > countours;
findContours(binary_image, contours, CV_EXTR_EXTERNAL, CV_CHAIN_APPROX_NONE);
// Now calculate the Moments of all of your contours
vector< Moments > moments_data;
for(size_t i=0; i<contours.size(); i++){
    Moments temp = moments(contours[i]);
    moments_data.push_back(temp);
}
// If you now want the average of all moments, then you need to average
double amount =  (double)moments_data.size();
// Create average containers for all moments
double a_m00 = 0.0, a_m01 = 0.0, a_m10 = 0.0, a_m20 = 0.0, a_m11 = 0.0, a_m02 = 0.0, a_m30 = 0.0, a_m21 = 0.0, a_m12 = 0.0, a_m03 = 0.0;
for(size_t i=0; i<moments_data.size(); i++){
     a_m00 = a_m00 + moments_data[i].m00;
     a_m01 = a_m01 + moments_data[i].m01;
     a_m10 = a_m10 + moments_data[i].m10;
     a_m20 = a_m20 + moments_data[i].m20;
     a_m11 = a_m11 + moments_data[i].m11;
     a_m02 = a_m02 + moments_data[i].m02;
     a_m30 = a_m30 + moments_data[i].m30;
     a_m21 = a_m21 + moments_data[i].m21;
     a_m12 = a_m12 + moments_data[i].m12;
     a_m03 = a_m03 + moments_data[i].m03;
}
// Now divide by the total amount of measurements amount
a_m00 = a_m00 / amount;
a_m01 = a_m01 / amount;
a_m10 = a_m10 / amount;
a_m20 = a_m20 / amount;
a_m11 = a_m11 / amount;
a_m02 = a_m02 / amount;
a_m30 = a_m30 / amount;
a_m21 = a_m21 / amount;
a_m12 = a_m12 / amount;
a_m03 = a_m03 / amount;

Your second question question is about the same for the arc lenght. Basically the code is identical but you should

  • Make a vector< double > lengths instead of vector< Moments > moments_data; to collect the arclength of all measurements
  • Then make again the sum of all values and divide by the total amount of measurements

As to your third question, question, yes you can calculate area by looping over all non zero pixels, but that is quite cumbersome and computational expensive. You are better using the builtin functions provided for contours.