Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image processing and moments

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):

enter image description here

The edge (which I assume is the contour):

enter image description here

The plot with the Image and the centroid enter image description here

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

like image 486
Olivier_s_j Avatar asked Feb 19 '23 20:02

Olivier_s_j


1 Answers

There is a difference between moments of filled area or its contour. Think about the following case:

enter image description here

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".

like image 87
Andrey Rubshtein Avatar answered Feb 28 '23 15:02

Andrey Rubshtein