[Tutor] What Centroid Algorithm Is This?
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
> 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
Look at first formula
- result is m
- slopes is x-component of r
- slopes is y-component of r
> xresult = result * slopes
> yresult = result * slopes
> 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
In :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 :distances = indices(weights.shape)
Out:array([[0, 1, 2, 3, 4]])
In :moments = weights * distances
Out:array([[ 0., 0., 2., 3., 4.]])
In :tot_weight = sum(weights)
In :tot_moment = sum(moments)
In :center = tot_moment/tot_weight
#Yay, this is the correct result!
More information about the Tutor