In a project I'm currently working on I have to calculate the 5 moments of the contour of an image. Which I can then for example use to get the centroid. To do this I used matlab :
f = imread(Is);
%Edge detection with prewitt
contourImage = edge(f,'prewitt');
% Morphological operation to close the open spaces
se = strel('disk',2);
closecontourImage = imclose(contourImage,se);
imshow(closecontourImage);
%Find the x y positions of all the nonzero elements of the edge
[row,col] = find(closecontourImage);
% 3 moments
m10= 0;
m00= 0;
m01= 0;
mu00 =0;
% Calculate the 3 moments based on the given paper
for r=1:length(row)
for c=1:length(col)
m10 = m10 + ((row(r)^1)*(col(c)^0));
m00 = m00 + ((row(r)^0)*(col(c)^0));
m01 = m01 + ((row(r)^0)*(col(c)^1));
end
end
% Calculate centroid (zwaartepunt) based on the given formulas
x = m10/m00;
y= m01/m00;
Original image (in png, i use pgm in matlab):
The edge (which I assume is the contour):
The plot with the Image and the centroid
When I compare this to matlabs built in centroid calculation it is pretty close.
Though my problem is concerning the area calculation. I read that the 0th moment = area. though my m00 is not the same as the area. Which is logical because the 0th moment is a summation of all the white pixels ... which only represent the edge of the image, therefore this couldn't result in the area. My question is now , is there a difference in contour moments and moments on the entire image ? And is it possible to get the area based on the contour in this representation ?
In my assignment they explicitly say that the moments of the contour should be calculated and that the 1ste moment is equal to the centroid (which is also not the case in my algorithm). But what I read here is that the 1st order central moment = the centroid. So does this mean that the contour moments are the same as the central moments ? And a more general question, can i use this edge as a contour ?
I find these moments very confusing
There is a difference between moments of filled area or its contour. Think about the following case:
The contour is the exactly the same for both of these objects. Nevertheless, it is obvious that the center of weight in the right square is biased to the right, since it is "more full to the right".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With