1

Is there a function or a toolbox which allows for computation of Image Moment?

http://en.wikipedia.org/wiki/Image_moment

The type of data on which I want to apply this function is binary. It is basically a matrix filled with 0 and 1.

Data =

     1     0     0     0     0     0
     1     1     1     0     1     1
     0     1     1     1     1     0
     1     0     1     1     0     0
     0     1     1     0     0     0
     1     1     0     0     0     0
     0     0     0     0     0     0
     1     0     0     1     0     0

And I want to apply image moments on this type of data. Is there any optimal Matlab implementation for this type of data?

Simon
  • 4,999
  • 21
  • 69
  • 97

1 Answers1

6

In a previous answer of mine, I had written an implementation for a subset of the regionprops function. The goal was to find image orientation, which was derived from the image moments. Here is the part relevant to you:

function outmom = raw_moments(im,i,j)
    outmom = sum(sum( ((1:size(im,1))'.^j * (1:size(im,2)).^i) .* im ));
end

function cmom = central_moments(im,i,j)
    rawm00 = raw_moments(im,0,0);
    centroids = [raw_moments(im,1,0)/rawm00 , raw_moments(im,0,1)/rawm00];
    cmom = sum(sum( (([1:size(im,1)]-centroids(2))'.^j * ...
                     ([1:size(im,2)]-centroids(1)).^i) .* im ));
end

The code follows the equations from the Wikipedia article, so no additional explanation is needed..

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Thanks for the prompt and detailed answer. I have a brief question, regarding image moments. Do you think that I should first try to remove noise from the image before applying image moments or it doesn't matter that much. For example, would the 2 pixels from the last line from Data matrix, influence the overall result? – Simon Jul 29 '11 at 19:30
  • @Simon: I guess it depends on how you define noise in your application, do you mean small disconnected regions? If that's the case, MATLAB has a few functions for morphological operations that can help (dilation/erosion, BWAREAOPEN, etc..) – Amro Jul 29 '11 at 20:10