The python class is designed to calculate the Mean and the Standard
Deviation of a collection of elements.
def ElementStatistics():
import math
class ElementStat:
def __init__(self):
self.Reset()
def Add(self,x):
print ' x ', x
self.n = self.n +1
delta = x - self.mean
self.mean = self.mean + delta / self.n
self.S = self.S + delta*(x - self.mean)
def N(self):
return self.n
def Mean(self):
return self.mean
def Variance(self):
return self.S / (self.n - 1)
def Std(self):
return math.sqrt(self.Variance())
def Reset(self):
self.n = 0
self.mean = 0
self.S = 0
return ElementStat()
Example 1: Using Element Stat |
obp = GetTool('obp')
mref = GetTool('mref')
img = GetImageMatr('1')
points = GetResultValue('polygons.Polygon[1]')
statX = ElementStatistics()
statY = ElementStatistics()
statZ = ElementStatistics()
for pos in points:
mref.setConfigValue('OrigoX',pos[0],0)
mref.setConfigValue('OrigoY',pos[1])
mref.execute(img)
obp.execute(img)
x = obp.getFloatValue('Position_x')
y = obp.getFloatValue('Position_y')
z = obp.getFloatValue('Position_z')
dev = obp.getFloatValue('Deviation')
if 0.0 < dev < 1.0 :
statX.Add(x)
statY.Add(y)
statZ.Add(z)
DrawMarker3D('1-3DRef',statX.Mean(),statY.Mean(),statZ.Mean(),'Red',6)
DrawText3D('1-3DRef',statX.Mean(),statY.Mean(),statZ.Mean(),' %.0f,%.0f,%.0f' % (statZ.Mean(),statZ.Std(),statZ.N()), 'Red',16,'Arial')
ore information: http://en.wikipedia.org/wiki/Standard_deviation
|
|