# Averaging Moment and arch length value for Binary image

Hello, I have an image which have multiple white area after the thresholding process. Can the average of the moment could be calculated? Can it be done to arc length too?

Another thing, is it true that, the calculation of white pixel after thresholding using function countNonZero(thresholded_image); could also describe the area and the moment of the area?

edit retag close merge delete

2

About moment you can have a look here You can sum moment of individual surface. countNonZero give you area of white pixels it's moment M00

Sort by » oldest newest most voted

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.

more

Because he wants an average moment calculation over all moments that he retrieves using findContours. This means that you need to average out each single moment value. Averaging is summing everything and then dividing it by the amount of measurements.

I didn't understand question like this. I thought that it was he wants moments of a surface composed of multiple surface.

I have an image which have multiple white area after the thresholding process. Can the average of the moment could be calculated? This is exactly what he asked for :D

1

@LBerger & @StevenPuttemans, anyway, both of you had given me direct and indirect answers. :) thanks!! will read, try to understand and test the coding... TQ

Official site

GitHub

Wiki

Documentation