[Tutor] What Centroid Algorithm Is This?

Eike Welk eike.welk at gmx.net
Mon Jan 26 21:20:55 CET 2009


On Monday 26 January 2009, Wayne Watson wrote:
> the three def stmts below. Maybe someone can hazard a guess? It
> looks like someone had fun doing this.

First bookmark this webpage. It explains every important function in 
Numpy:
http://www.scipy.org/Numpy_Example_List_With_Doc

>      def Centroid( self, ref, curr, mask ):
ref is maybe dark frame
>          slopes = indices((488,722),int32)
>          nada   = zeros((488,722),uint8)
>
Do discrimination; areas that are not covered by the meteor are 0
>          result = where(curr > ref, curr-ref, nada)
>          result = where(result > mask, result-mask, nada)
>

Compute center of mass
http://en.wikipedia.org/wiki/Center_of_mass#Definition
Look at first formula 
- result is m
- slopes[1] is x-component of r
- slopes[0] is y-component of r
>          xresult = result * slopes[1]
>          yresult = result * slopes[0]
>          total   = sum(ravel(asarray(result,int32)))
>          count   = sum(ravel(result > 0))
>          if total == 0:
>              return 360,240,0,0
>
>          xpos = sum(ravel(xresult))/total
>          ypos = sum(ravel(yresult))/total
>
>          return xpos,ypos,total,count


Example algorithm done in Ipython invoked with
ipython --pylab
----
In [20]:weights = array([0,0,1,1,1],'d')
#These are three weights of mass 1. 
#They are located at the positions 2, 3, 4. 
#The center of gravity is therefore at position 3.

In [21]:distances = indices(weights.shape)

In [22]:distances
Out[22]:array([[0, 1, 2, 3, 4]])

In [23]:moments = weights * distances

In [24]:moments
Out[24]:array([[ 0.,  0.,  2.,  3.,  4.]])

In [25]:tot_weight = sum(weights)

In [26]:tot_moment = sum(moments)

In [27]:center = tot_moment/tot_weight

In [28]:center
Out[28]:3.0
#Yay, this is the correct result!


Kind regards,
Eike.


More information about the Tutor mailing list