Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Not only you are calculating moments here but you also perform threshold operation. Both oprations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(differnce,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you calculate centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

Not only you are calculating moments here but you also perform threshold operation. Both oprations operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(differnce,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you calculate centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

Not only you are calculating moments here but you also perform threshold operation. Both operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(differnce,Scalar(50),tmp,CMP_GE);
compare(difference,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you calculate centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

Not only you are calculating moments here but you also perform threshold operation. Both operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(difference,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you calculate are calculating centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

Not only you are calculating moments here but you also perform threshold operation. Both operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(difference,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you are calculating centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

And on other topic, you are traversing pixels of image column by column, intead row by row. This may incure huge penalty on your time performance (up to order of magnitude in some cases).

Not only you are calculating moments here but you also perform threshold operation. Both operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(difference,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you are calculating centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

And on other topic, you are traversing pixels of image column by column, intead of row by row. This may incure huge penalty on your time performance (up to order of magnitude in some cases).

Not only you are calculating moments here but you also perform threshold operation. Both operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(difference,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you are calculating centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

And on other topic, you are traversing pixels of image column by column, intead of row by row. This may incure incur huge penalty on your time performance (up to order of magnitude in some cases).

Not only you are calculating moments here but you also perform threshold operation. Both operations can be done by OpenCV. First by function compare() and second by function moments().

Your code will be:

Mat tmp;
compare(difference,Scalar(50),tmp,CMP_GE);
Moments m = moments(tmp,true);
Point p(m.m10/m.m00, m.m01/m.m00);

Note that you should use function moments() with flag 'true', i.e. each non-zero value in image should be treated as '1'. Otherwise moments() will use original values from image for calculation of moments. In this case it will result in factor of 255 for all moments. It is not a problem when you are calculating centroids because when you divide two values this factor will cancel itself, but if you use it for anything else you will get wrong results. For example m.m00 will be 255 times bigger than actual area.

And on other topic, you are traversing pixels of image column by column, intead of row by row. This may incur huge penalty on your time performance (up to order of magnitude in some cases).

Edit: Just thought that this can be done in even cleaner way:

Moments m = moments((difference>=50),true);
Point2d p(m.m10/m.m00, m.m01/m.m00);