Ask Your Question

Moment Value & Area validation manually not the same

asked 2015-08-03 22:49:58 -0500

zms gravatar image

Hello, I had tried to check using and a very simple image ( inside the image there is a square black and white). The dimension of the image is 100X100 pixel. The square point coordinate in the image are (11,11),(84,11),(11,84),(84,84). With the size of the square should be at 73 square = 5329 pixels after thresholding. But running the moment code return me moment & area size = 5843 which is different. I didn't do blur in the image. Attached is the image I had created for validation.C:\fakepath\Imagebox.jpg The coding that I refer is here for MOMENT

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-08-04 01:40:53 -0500

LBerger gravatar image

updated 2015-08-04 06:38:01 -0500

Your surface is (84-11+1)*(84-11+1)=5476. When I use threshold and connectedComposantWith stats it gives me good result. Becarefull your link is jpg image so you have got some pixels which are not white or black.

Results are

***********connectedComponentsWithStats Area :4524

Area :5476

***********With contour

Perimeter : 292

Area : 5329

With this program :

int main(int argc, char **argv)
Mat     statComposante; /*< */
Mat     centreGComposante; 

Mat mTest,mThresh,mConnected;
mTest=imread("143866016312554.jpg", CV_LOAD_IMAGE_GRAYSCALE);
threshold( mTest, mThresh, 128,255,cv::THRESH_BINARY_INV);
cout << "***********************************connectedComponentsWithStats\n";
connectedComponentsWithStats(mThresh, mConnected,statComposante,centreGComposante, 4, CV_32S);
for (int i = 0; i < statComposante.rows; i++)
    cout <<"Area :"<<<int>(i,cv::CC_STAT_AREA)<<"\n";
// with contours
cout << "***********************************With contour\n";
Mat mClone = mThresh.clone();
vector<vector<cv::Point> > contours;

findContours( mClone,  contours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE);   
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
    cout <<"Perimeter : "<< arcLength( contours[i], true ) << "\n";
    cout <<"Area : "<< contourArea( contours[i], false ) << "\n";
return 0;

your image is with a random palette image description

edit flag offensive delete link more


JPG could cause issue? I tried PNG and still giving me the same result. Any suggestion on picture format? Can I know how much area that you had obtained? just to compare the result that I have.

BTW i'm using OTSU thresholding. threshold(src_gray, src_gray , 0 // the value doesn't matter for Otsu thresholding , 255 // we could choose any non-zero value. 255 (white) makes it easy to see the binary image , cv::THRESH_OTSU | cv::THRESH_BINARY);

zms gravatar imagezms ( 2015-08-04 04:31:50 -0500 )edit

Question Tools

1 follower


Asked: 2015-08-03 22:49:58 -0500

Seen: 711 times

Last updated: Aug 04 '15