Ask Your Question

Averaging Moment and arch length value for Binary image

asked 2015-08-04 23:26:44 -0500

zms gravatar image

updated 2015-08-05 00:23:00 -0500

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 flag offensive close merge delete



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

LBerger gravatar imageLBerger ( 2015-08-05 02:33:30 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-08-05 02:41:14 -0500

updated 2015-08-05 02:41:35 -0500

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]);
// 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.

edit flag offensive delete link more


@StevenPuttemans Why did you do a_m00 = a_m00 / amount;?

LBerger gravatar imageLBerger ( 2015-08-05 02:55:35 -0500 )edit

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-05 03:10:05 -0500 )edit

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

LBerger gravatar imageLBerger ( 2015-08-05 03:21:30 -0500 )edit

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

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-05 03:31:28 -0500 )edit

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

zms gravatar imagezms ( 2015-08-05 22:37:48 -0500 )edit

Question Tools

1 follower


Asked: 2015-08-04 23:26:44 -0500

Seen: 180 times

Last updated: Aug 05 '15