title: Image processing and moments
body: <p>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 :</p>
  
  <blockquote>
    <p>f = imread(Is);</p>
  </blockquote>
  
  <pre><code>%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;
  </code></pre>
  
  <p>Original image (in png, i use pgm in matlab):</p>
  
  <p><img src="https://i.stack.imgur.com/RgZ6q.png" alt="enter image description here"></p>
  
  <p>The edge (which I assume is the contour):</p>
  
  <p><img src="https://i.stack.imgur.com/XAhgc.png" alt="enter image description here"></p>
  
  <p>The plot with the Image and the centroid
  <img src="https://i.stack.imgur.com/AA1zi.png" alt="enter image description here"></p>
  
  <p>When I compare this to matlabs built in centroid calculation it is pretty close.</p>
  
  <p>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 , <strong>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 ?</strong></p>
  
  <p>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 <a href="http://www.aishack.in/2011/06/image-moments/" rel="nofollow noreferrer">here</a> is that the 1st order central moment = the centroid. <strong>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 ?</strong></p>
  
  <p>I find these moments very confusing </p>